router.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/routing"
  11. )
  12. type key uint32
  13. const (
  14. resolvedIPsKey key = iota
  15. )
  16. type IPResolver interface {
  17. Resolve() []net.Address
  18. }
  19. func ContextWithResolveIPs(ctx context.Context, f IPResolver) context.Context {
  20. return context.WithValue(ctx, resolvedIPsKey, f)
  21. }
  22. func ResolvedIPsFromContext(ctx context.Context) (IPResolver, bool) {
  23. ips, ok := ctx.Value(resolvedIPsKey).(IPResolver)
  24. return ips, ok
  25. }
  26. func init() {
  27. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  28. r := new(Router)
  29. if err := core.RequireFeatures(ctx, func(d dns.Client) error {
  30. return r.Init(config.(*Config), d)
  31. }); err != nil {
  32. return nil, err
  33. }
  34. return r, nil
  35. }))
  36. }
  37. // Router is an implementation of routing.Router.
  38. type Router struct {
  39. domainStrategy Config_DomainStrategy
  40. rules []Rule
  41. dns dns.Client
  42. }
  43. // Init initializes the Router.
  44. func (r *Router) Init(config *Config, d dns.Client) error {
  45. r.domainStrategy = config.DomainStrategy
  46. r.rules = make([]Rule, len(config.Rule))
  47. r.dns = d
  48. for idx, rule := range config.Rule {
  49. r.rules[idx].Tag = rule.Tag
  50. cond, err := rule.BuildCondition()
  51. if err != nil {
  52. return err
  53. }
  54. r.rules[idx].Condition = cond
  55. }
  56. return nil
  57. }
  58. type ipResolver struct {
  59. dns dns.Client
  60. ip []net.Address
  61. domain string
  62. resolved bool
  63. }
  64. func (r *ipResolver) Resolve() []net.Address {
  65. if r.resolved {
  66. return r.ip
  67. }
  68. newError("looking for IP for domain: ", r.domain).WriteToLog()
  69. r.resolved = true
  70. ips, err := r.dns.LookupIP(r.domain)
  71. if err != nil {
  72. newError("failed to get IP address").Base(err).WriteToLog()
  73. }
  74. if len(ips) == 0 {
  75. return nil
  76. }
  77. r.ip = make([]net.Address, len(ips))
  78. for i, ip := range ips {
  79. r.ip[i] = net.IPAddress(ip)
  80. }
  81. return r.ip
  82. }
  83. // PickRoute implements routing.Router.
  84. func (r *Router) PickRoute(ctx context.Context) (string, error) {
  85. resolver := &ipResolver{
  86. dns: r.dns,
  87. }
  88. outbound := session.OutboundFromContext(ctx)
  89. if r.domainStrategy == Config_IpOnDemand {
  90. if outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain() {
  91. resolver.domain = outbound.Target.Address.Domain()
  92. ctx = ContextWithResolveIPs(ctx, resolver)
  93. }
  94. }
  95. for _, rule := range r.rules {
  96. if rule.Apply(ctx) {
  97. return rule.Tag, nil
  98. }
  99. }
  100. if outbound == nil || !outbound.Target.IsValid() {
  101. return "", common.ErrNoClue
  102. }
  103. dest := outbound.Target
  104. if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  105. resolver.domain = dest.Address.Domain()
  106. ips := resolver.Resolve()
  107. if len(ips) > 0 {
  108. ctx = ContextWithResolveIPs(ctx, resolver)
  109. for _, rule := range r.rules {
  110. if rule.Apply(ctx) {
  111. return rule.Tag, nil
  112. }
  113. }
  114. }
  115. }
  116. return "", common.ErrNoClue
  117. }
  118. // Start implements common.Runnable.
  119. func (*Router) Start() error {
  120. return nil
  121. }
  122. // Close implements common.Closable.
  123. func (*Router) Close() error {
  124. return nil
  125. }
  126. // Type implement common.HasType.
  127. func (*Router) Type() interface{} {
  128. return routing.RouterType()
  129. }