router.go 2.8 KB

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