router.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // +build !confonly
  2. package router
  3. //go:generate errorgen
  4. import (
  5. "context"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/features/dns"
  9. "v2ray.com/core/features/outbound"
  10. "v2ray.com/core/features/routing"
  11. routing_dns "v2ray.com/core/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(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. r.balancers[rule.Tag] = balancer
  37. }
  38. r.rules = make([]*Rule, 0, len(config.Rule))
  39. for _, rule := range config.Rule {
  40. cond, err := rule.BuildCondition()
  41. if err != nil {
  42. return err
  43. }
  44. rr := &Rule{
  45. Condition: cond,
  46. Tag: rule.GetTag(),
  47. }
  48. btag := rule.GetBalancingTag()
  49. if len(btag) > 0 {
  50. brule, found := r.balancers[btag]
  51. if !found {
  52. return newError("balancer ", btag, " not found")
  53. }
  54. rr.Balancer = brule
  55. }
  56. r.rules = append(r.rules, rr)
  57. }
  58. return nil
  59. }
  60. // PickRoute implements routing.Router.
  61. func (r *Router) PickRoute(ctx routing.Context) (routing.Route, error) {
  62. rule, ctx, err := r.pickRouteInternal(ctx)
  63. if err != nil {
  64. return nil, err
  65. }
  66. tag, err := rule.GetTag()
  67. if err != nil {
  68. return nil, err
  69. }
  70. return &Route{Context: ctx, outboundTag: tag}, nil
  71. }
  72. func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
  73. if r.domainStrategy == Config_IpOnDemand {
  74. ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
  75. }
  76. for _, rule := range r.rules {
  77. if rule.Apply(ctx) {
  78. return rule, ctx, nil
  79. }
  80. }
  81. if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 {
  82. return nil, ctx, common.ErrNoClue
  83. }
  84. ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
  85. // Try applying rules again if we have IPs.
  86. for _, rule := range r.rules {
  87. if rule.Apply(ctx) {
  88. return rule, ctx, nil
  89. }
  90. }
  91. return nil, ctx, common.ErrNoClue
  92. }
  93. // Start implements common.Runnable.
  94. func (*Router) Start() error {
  95. return nil
  96. }
  97. // Close implements common.Closable.
  98. func (*Router) Close() error {
  99. return nil
  100. }
  101. // Type implement common.HasType.
  102. func (*Router) Type() interface{} {
  103. return routing.RouterType()
  104. }
  105. // GetOutboundGroupTags implements routing.Route.
  106. func (r *Route) GetOutboundGroupTags() []string {
  107. return r.outboundGroupTags
  108. }
  109. // GetOutboundTag implements routing.Route.
  110. func (r *Route) GetOutboundTag() string {
  111. return r.outboundTag
  112. }
  113. func init() {
  114. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  115. r := new(Router)
  116. if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
  117. return r.Init(config.(*Config), d, ohm)
  118. }); err != nil {
  119. return nil, err
  120. }
  121. return r, nil
  122. }))
  123. }