router.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package router
  2. import (
  3. "context"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/errors"
  8. "v2ray.com/core/common/log"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/proxy"
  11. )
  12. var (
  13. ErrInvalidRule = errors.New("Invalid Rule")
  14. ErrNoRuleApplicable = errors.New("No rule applicable")
  15. )
  16. type Router struct {
  17. domainStrategy Config_DomainStrategy
  18. rules []Rule
  19. // cache *RoutingTable
  20. dnsServer dns.Server
  21. }
  22. func NewRouter(ctx context.Context, config *Config) (*Router, error) {
  23. space := app.SpaceFromContext(ctx)
  24. if space == nil {
  25. return nil, errors.New("Router: No space in context.")
  26. }
  27. r := &Router{
  28. domainStrategy: config.DomainStrategy,
  29. //cache: NewRoutingTable(),
  30. rules: make([]Rule, len(config.Rule)),
  31. }
  32. space.OnInitialize(func() error {
  33. for idx, rule := range config.Rule {
  34. r.rules[idx].Tag = rule.Tag
  35. cond, err := rule.BuildCondition()
  36. if err != nil {
  37. return err
  38. }
  39. r.rules[idx].Condition = cond
  40. }
  41. r.dnsServer = dns.FromSpace(space)
  42. if r.dnsServer == nil {
  43. return errors.New("Router: DNS is not found in the space.")
  44. }
  45. return nil
  46. })
  47. return r, nil
  48. }
  49. // Private: Visible for testing.
  50. func (v *Router) ResolveIP(dest net.Destination) []net.Address {
  51. ips := v.dnsServer.Get(dest.Address.Domain())
  52. if len(ips) == 0 {
  53. return nil
  54. }
  55. dests := make([]net.Address, len(ips))
  56. for idx, ip := range ips {
  57. dests[idx] = net.IPAddress(ip)
  58. }
  59. return dests
  60. }
  61. func (v *Router) takeDetourWithoutCache(ctx context.Context) (string, error) {
  62. for _, rule := range v.rules {
  63. if rule.Apply(ctx) {
  64. return rule.Tag, nil
  65. }
  66. }
  67. dest := proxy.DestinationFromContext(ctx)
  68. if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  69. log.Info("Router: Looking up IP for ", dest)
  70. ipDests := v.ResolveIP(dest)
  71. if ipDests != nil {
  72. ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
  73. for _, rule := range v.rules {
  74. if rule.Apply(ctx) {
  75. return rule.Tag, nil
  76. }
  77. }
  78. }
  79. }
  80. return "", ErrNoRuleApplicable
  81. }
  82. func (v *Router) TakeDetour(ctx context.Context) (string, error) {
  83. //destStr := dest.String()
  84. //found, tag, err := v.cache.Get(destStr)
  85. //if !found {
  86. tag, err := v.takeDetourWithoutCache(ctx)
  87. //v.cache.Set(destStr, tag, err)
  88. return tag, err
  89. //}
  90. //return tag, err
  91. }
  92. func (Router) Interface() interface{} {
  93. return (*Router)(nil)
  94. }
  95. func FromSpace(space app.Space) *Router {
  96. app := space.GetApplication((*Router)(nil))
  97. if app == nil {
  98. return nil
  99. }
  100. return app.(*Router)
  101. }
  102. func init() {
  103. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  104. return NewRouter(ctx, config.(*Config))
  105. }))
  106. }