router.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package routing
  2. import (
  3. "github.com/xtls/xray-core/common"
  4. "github.com/xtls/xray-core/common/serial"
  5. "github.com/xtls/xray-core/features"
  6. )
  7. // Router is a feature to choose an outbound tag for the given request.
  8. //
  9. // xray:api:stable
  10. type Router interface {
  11. features.Feature
  12. // PickRoute returns a route decision based on the given routing context.
  13. PickRoute(ctx Context) (Route, error)
  14. AddRule(config *serial.TypedMessage, shouldAppend bool) error
  15. RemoveRule(tag string) error
  16. }
  17. // Route is the routing result of Router feature.
  18. //
  19. // xray:api:stable
  20. type Route interface {
  21. // A Route is also a routing context.
  22. Context
  23. // GetOutboundGroupTags returns the detoured outbound group tags in sequence before a final outbound is chosen.
  24. GetOutboundGroupTags() []string
  25. // GetOutboundTag returns the tag of the outbound the connection was dispatched to.
  26. GetOutboundTag() string
  27. }
  28. // RouterType return the type of Router interface. Can be used to implement common.HasType.
  29. //
  30. // xray:api:stable
  31. func RouterType() interface{} {
  32. return (*Router)(nil)
  33. }
  34. // DefaultRouter is an implementation of Router, which always returns ErrNoClue for routing decisions.
  35. type DefaultRouter struct{}
  36. // Type implements common.HasType.
  37. func (DefaultRouter) Type() interface{} {
  38. return RouterType()
  39. }
  40. // PickRoute implements Router.
  41. func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
  42. return nil, common.ErrNoClue
  43. }
  44. // AddRule implements Router.
  45. func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
  46. return common.ErrNoClue
  47. }
  48. // RemoveRule implements Router.
  49. func (DefaultRouter) RemoveRule(tag string) error {
  50. return common.ErrNoClue
  51. }
  52. // Start implements common.Runnable.
  53. func (DefaultRouter) Start() error {
  54. return nil
  55. }
  56. // Close implements common.Closable.
  57. func (DefaultRouter) Close() error {
  58. return nil
  59. }