router.go 3.6 KB

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