router.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package router
  2. //go:generate errorgen
  3. import (
  4. "context"
  5. "v2ray.com/core/common/session"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/proxy"
  10. )
  11. // Router is an implementation of core.Router.
  12. type Router struct {
  13. domainStrategy Config_DomainStrategy
  14. rules []Rule
  15. dns core.DNSClient
  16. }
  17. // NewRouter creates a new Router based on the given config.
  18. func NewRouter(ctx context.Context, config *Config) (*Router, error) {
  19. v := core.MustFromContext(ctx)
  20. r := &Router{
  21. domainStrategy: config.DomainStrategy,
  22. rules: make([]Rule, len(config.Rule)),
  23. dns: v.DNSClient(),
  24. }
  25. for idx, rule := range config.Rule {
  26. r.rules[idx].Tag = rule.Tag
  27. cond, err := rule.BuildCondition()
  28. if err != nil {
  29. return nil, err
  30. }
  31. r.rules[idx].Condition = cond
  32. }
  33. if err := v.RegisterFeature((*core.Router)(nil), r); err != nil {
  34. return nil, newError("unable to register Router").Base(err)
  35. }
  36. return r, nil
  37. }
  38. type ipResolver struct {
  39. dns core.DNSClient
  40. ip []net.Address
  41. domain string
  42. resolved bool
  43. }
  44. func (r *ipResolver) Resolve() []net.Address {
  45. if r.resolved {
  46. return r.ip
  47. }
  48. newError("looking for IP for domain: ", r.domain).WriteToLog()
  49. r.resolved = true
  50. ips, err := r.dns.LookupIP(r.domain)
  51. if err != nil {
  52. newError("failed to get IP address").Base(err).WriteToLog()
  53. }
  54. if len(ips) == 0 {
  55. return nil
  56. }
  57. r.ip = make([]net.Address, len(ips))
  58. for i, ip := range ips {
  59. r.ip[i] = net.IPAddress(ip)
  60. }
  61. return r.ip
  62. }
  63. // PickRoute implements core.Router.
  64. func (r *Router) PickRoute(ctx context.Context) (string, error) {
  65. resolver := &ipResolver{
  66. dns: r.dns,
  67. }
  68. outbound := session.OutboundFromContext(ctx)
  69. if r.domainStrategy == Config_IpOnDemand {
  70. if outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain() {
  71. resolver.domain = outbound.Target.Address.Domain()
  72. ctx = proxy.ContextWithResolveIPs(ctx, resolver)
  73. }
  74. }
  75. for _, rule := range r.rules {
  76. if rule.Apply(ctx) {
  77. return rule.Tag, nil
  78. }
  79. }
  80. if outbound == nil || !outbound.Target.IsValid() {
  81. return "", core.ErrNoClue
  82. }
  83. dest := outbound.Target
  84. if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  85. resolver.domain = dest.Address.Domain()
  86. ips := resolver.Resolve()
  87. if len(ips) > 0 {
  88. ctx = proxy.ContextWithResolveIPs(ctx, resolver)
  89. for _, rule := range r.rules {
  90. if rule.Apply(ctx) {
  91. return rule.Tag, nil
  92. }
  93. }
  94. }
  95. }
  96. return "", core.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. func init() {
  107. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  108. return NewRouter(ctx, config.(*Config))
  109. }))
  110. }