1
0

router.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // GetRuleTag returns the matching rule tag for debugging if exists
  28. GetRuleTag() string
  29. }
  30. // RouterType return the type of Router interface. Can be used to implement common.HasType.
  31. //
  32. // xray:api:stable
  33. func RouterType() interface{} {
  34. return (*Router)(nil)
  35. }
  36. // DefaultRouter is an implementation of Router, which always returns ErrNoClue for routing decisions.
  37. type DefaultRouter struct{}
  38. // Type implements common.HasType.
  39. func (DefaultRouter) Type() interface{} {
  40. return RouterType()
  41. }
  42. // PickRoute implements Router.
  43. func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
  44. return nil, common.ErrNoClue
  45. }
  46. // AddRule implements Router.
  47. func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
  48. return common.ErrNoClue
  49. }
  50. // RemoveRule implements Router.
  51. func (DefaultRouter) RemoveRule(tag string) error {
  52. return common.ErrNoClue
  53. }
  54. // Start implements common.Runnable.
  55. func (DefaultRouter) Start() error {
  56. return nil
  57. }
  58. // Close implements common.Closable.
  59. func (DefaultRouter) Close() error {
  60. return nil
  61. }