router.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ListRule() []Route
  17. }
  18. // Route is the routing result of Router feature.
  19. //
  20. // xray:api:stable
  21. type Route interface {
  22. // A Route is also a routing context.
  23. Context
  24. // GetOutboundGroupTags returns the detoured outbound group tags in sequence before a final outbound is chosen.
  25. GetOutboundGroupTags() []string
  26. // GetOutboundTag returns the tag of the outbound the connection was dispatched to.
  27. GetOutboundTag() string
  28. // GetRuleTag returns the matching rule tag for debugging if exists
  29. GetRuleTag() string
  30. }
  31. // RouterType return the type of Router interface. Can be used to implement common.HasType.
  32. //
  33. // xray:api:stable
  34. func RouterType() interface{} {
  35. return (*Router)(nil)
  36. }
  37. // DefaultRouter is an implementation of Router, which always returns ErrNoClue for routing decisions.
  38. type DefaultRouter struct{}
  39. // Type implements common.HasType.
  40. func (DefaultRouter) Type() interface{} {
  41. return RouterType()
  42. }
  43. // PickRoute implements Router.
  44. func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
  45. return nil, common.ErrNoClue
  46. }
  47. // AddRule implements Router.
  48. func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
  49. return common.ErrNoClue
  50. }
  51. // RemoveRule implements Router.
  52. func (DefaultRouter) RemoveRule(tag string) error {
  53. return common.ErrNoClue
  54. }
  55. // ListRule implements Router.
  56. func (DefaultRouter) ListRule() []Route {
  57. return nil
  58. }
  59. // Start implements common.Runnable.
  60. func (DefaultRouter) Start() error {
  61. return nil
  62. }
  63. // Close implements common.Closable.
  64. func (DefaultRouter) Close() error {
  65. return nil
  66. }