router.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package router
  2. //go:generate errorgen
  3. import (
  4. "context"
  5. "v2ray.com/core"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/session"
  9. "v2ray.com/core/features/dns"
  10. "v2ray.com/core/features/outbound"
  11. "v2ray.com/core/features/routing"
  12. )
  13. type key uint32
  14. const (
  15. resolvedIPsKey key = iota
  16. )
  17. type IPResolver interface {
  18. Resolve() []net.Address
  19. }
  20. func ContextWithResolveIPs(ctx context.Context, f IPResolver) context.Context {
  21. return context.WithValue(ctx, resolvedIPsKey, f)
  22. }
  23. func ResolvedIPsFromContext(ctx context.Context) (IPResolver, bool) {
  24. ips, ok := ctx.Value(resolvedIPsKey).(IPResolver)
  25. return ips, ok
  26. }
  27. func init() {
  28. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  29. r := new(Router)
  30. if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
  31. return r.Init(config.(*Config), d, ohm)
  32. }); err != nil {
  33. return nil, err
  34. }
  35. return r, nil
  36. }))
  37. }
  38. // Router is an implementation of routing.Router.
  39. type Router struct {
  40. domainStrategy Config_DomainStrategy
  41. rules []*Rule
  42. balancers map[string]*Balancer
  43. dns dns.Client
  44. }
  45. // Init initializes the Router.
  46. func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
  47. r.domainStrategy = config.DomainStrategy
  48. r.dns = d
  49. r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
  50. for _, rule := range config.BalancingRule {
  51. balancer, err := rule.Build(ohm)
  52. if err != nil {
  53. return err
  54. }
  55. r.balancers[rule.Tag] = balancer
  56. }
  57. r.rules = make([]*Rule, 0, len(config.Rule))
  58. for _, rule := range config.Rule {
  59. cond, err := rule.BuildCondition()
  60. if err != nil {
  61. return err
  62. }
  63. rr := &Rule{
  64. Condition: cond,
  65. Tag: rule.GetTag(),
  66. }
  67. btag := rule.GetBalancingTag()
  68. if len(btag) > 0 {
  69. brule, found := r.balancers[btag]
  70. if !found {
  71. return newError("balancer ", btag, " not found")
  72. }
  73. rr.Balancer = brule
  74. }
  75. r.rules = append(r.rules, rr)
  76. }
  77. return nil
  78. }
  79. type ipResolver struct {
  80. dns dns.Client
  81. ip []net.Address
  82. domain string
  83. resolved bool
  84. }
  85. func (r *ipResolver) Resolve() []net.Address {
  86. if r.resolved {
  87. return r.ip
  88. }
  89. newError("looking for IP for domain: ", r.domain).WriteToLog()
  90. r.resolved = true
  91. ips, err := r.dns.LookupIP(r.domain)
  92. if err != nil {
  93. newError("failed to get IP address").Base(err).WriteToLog()
  94. }
  95. if len(ips) == 0 {
  96. return nil
  97. }
  98. r.ip = make([]net.Address, len(ips))
  99. for i, ip := range ips {
  100. r.ip[i] = net.IPAddress(ip)
  101. }
  102. return r.ip
  103. }
  104. func (r *Router) PickRoute(ctx context.Context) (string, error) {
  105. rule, err := r.pickRouteInternal(ctx)
  106. if err != nil {
  107. return "", err
  108. }
  109. return rule.GetTag()
  110. }
  111. // PickRoute implements routing.Router.
  112. func (r *Router) pickRouteInternal(ctx context.Context) (*Rule, error) {
  113. resolver := &ipResolver{
  114. dns: r.dns,
  115. }
  116. outbound := session.OutboundFromContext(ctx)
  117. if r.domainStrategy == Config_IpOnDemand {
  118. if outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain() {
  119. resolver.domain = outbound.Target.Address.Domain()
  120. ctx = ContextWithResolveIPs(ctx, resolver)
  121. }
  122. }
  123. for _, rule := range r.rules {
  124. if rule.Apply(ctx) {
  125. return rule, nil
  126. }
  127. }
  128. if outbound == nil || !outbound.Target.IsValid() {
  129. return nil, common.ErrNoClue
  130. }
  131. dest := outbound.Target
  132. if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  133. resolver.domain = dest.Address.Domain()
  134. ips := resolver.Resolve()
  135. if len(ips) > 0 {
  136. ctx = ContextWithResolveIPs(ctx, resolver)
  137. for _, rule := range r.rules {
  138. if rule.Apply(ctx) {
  139. return rule, nil
  140. }
  141. }
  142. }
  143. }
  144. return nil, common.ErrNoClue
  145. }
  146. // Start implements common.Runnable.
  147. func (*Router) Start() error {
  148. return nil
  149. }
  150. // Close implements common.Closable.
  151. func (*Router) Close() error {
  152. return nil
  153. }
  154. // Type implement common.HasType.
  155. func (*Router) Type() interface{} {
  156. return routing.RouterType()
  157. }