router.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. type Router struct {
  11. domainStrategy Config_DomainStrategy
  12. rules []Rule
  13. dns core.DNSClient
  14. }
  15. func NewRouter(ctx context.Context, config *Config) (*Router, error) {
  16. v := core.FromContext(ctx)
  17. if v == nil {
  18. return nil, newError("V is not in context")
  19. }
  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. 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. func (*Router) Start() error {
  97. return nil
  98. }
  99. func (*Router) Close() {}
  100. func init() {
  101. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  102. return NewRouter(ctx, config.(*Config))
  103. }))
  104. }