router.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package router
  2. //go:generate go run github.com/xtls/xray-core/common/errors/errorgen
  3. import (
  4. "context"
  5. "github.com/xtls/xray-core/common"
  6. "github.com/xtls/xray-core/core"
  7. "github.com/xtls/xray-core/features/dns"
  8. "github.com/xtls/xray-core/features/outbound"
  9. "github.com/xtls/xray-core/features/routing"
  10. routing_dns "github.com/xtls/xray-core/features/routing/dns"
  11. )
  12. // Router is an implementation of routing.Router.
  13. type Router struct {
  14. domainStrategy Config_DomainStrategy
  15. rules []*Rule
  16. balancers map[string]*Balancer
  17. dns dns.Client
  18. }
  19. // Route is an implementation of routing.Route.
  20. type Route struct {
  21. routing.Context
  22. outboundGroupTags []string
  23. outboundTag string
  24. }
  25. // Init initializes the Router.
  26. func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
  27. r.domainStrategy = config.DomainStrategy
  28. r.dns = d
  29. r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
  30. for _, rule := range config.BalancingRule {
  31. balancer, err := rule.Build(ohm)
  32. if err != nil {
  33. return err
  34. }
  35. r.balancers[rule.Tag] = balancer
  36. }
  37. r.rules = make([]*Rule, 0, len(config.Rule))
  38. for _, rule := range config.Rule {
  39. cond, err := rule.BuildCondition()
  40. if err != nil {
  41. return err
  42. }
  43. rr := &Rule{
  44. Condition: cond,
  45. Tag: rule.GetTag(),
  46. }
  47. btag := rule.GetBalancingTag()
  48. if len(btag) > 0 {
  49. brule, found := r.balancers[btag]
  50. if !found {
  51. return newError("balancer ", btag, " not found")
  52. }
  53. rr.Balancer = brule
  54. }
  55. r.rules = append(r.rules, rr)
  56. }
  57. return nil
  58. }
  59. // PickRoute implements routing.Router.
  60. func (r *Router) PickRoute(ctx routing.Context) (routing.Route, error) {
  61. rule, ctx, err := r.pickRouteInternal(ctx)
  62. if err != nil {
  63. return nil, err
  64. }
  65. tag, err := rule.GetTag()
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &Route{Context: ctx, outboundTag: tag}, nil
  70. }
  71. func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
  72. if r.domainStrategy == Config_IpOnDemand {
  73. ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
  74. }
  75. for _, rule := range r.rules {
  76. if rule.Apply(ctx) {
  77. return rule, ctx, nil
  78. }
  79. }
  80. if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 {
  81. return nil, ctx, common.ErrNoClue
  82. }
  83. ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
  84. // Try applying rules again if we have IPs.
  85. for _, rule := range r.rules {
  86. if rule.Apply(ctx) {
  87. return rule, ctx, nil
  88. }
  89. }
  90. return nil, ctx, common.ErrNoClue
  91. }
  92. // Start implements common.Runnable.
  93. func (*Router) Start() error {
  94. return nil
  95. }
  96. // Close implements common.Closable.
  97. func (*Router) Close() error {
  98. return nil
  99. }
  100. // Type implement common.HasType.
  101. func (*Router) Type() interface{} {
  102. return routing.RouterType()
  103. }
  104. // GetOutboundGroupTags implements routing.Route.
  105. func (r *Route) GetOutboundGroupTags() []string {
  106. return r.outboundGroupTags
  107. }
  108. // GetOutboundTag implements routing.Route.
  109. func (r *Route) GetOutboundTag() string {
  110. return r.outboundTag
  111. }
  112. func init() {
  113. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  114. r := new(Router)
  115. if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
  116. return r.Init(config.(*Config), d, ohm)
  117. }); err != nil {
  118. return nil, err
  119. }
  120. return r, nil
  121. }))
  122. }