router.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. v2net "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 v2net.Destination) []v2net.Destination {
  51. ips := v.dnsServer.Get(dest.Address.Domain())
  52. if len(ips) == 0 {
  53. return nil
  54. }
  55. dests := make([]v2net.Destination, len(ips))
  56. for idx, ip := range ips {
  57. if dest.Network == v2net.Network_TCP {
  58. dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port)
  59. } else {
  60. dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port)
  61. }
  62. }
  63. return dests
  64. }
  65. func (v *Router) takeDetourWithoutCache(session *proxy.SessionInfo) (string, error) {
  66. for _, rule := range v.rules {
  67. if rule.Apply(session) {
  68. return rule.Tag, nil
  69. }
  70. }
  71. dest := session.Destination
  72. if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  73. log.Info("Router: Looking up IP for ", dest)
  74. ipDests := v.ResolveIP(dest)
  75. if ipDests != nil {
  76. for _, ipDest := range ipDests {
  77. log.Info("Router: Trying IP ", ipDest)
  78. for _, rule := range v.rules {
  79. if rule.Apply(&proxy.SessionInfo{
  80. Source: session.Source,
  81. Destination: ipDest,
  82. User: session.User,
  83. }) {
  84. return rule.Tag, nil
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return "", ErrNoRuleApplicable
  91. }
  92. func (v *Router) TakeDetour(session *proxy.SessionInfo) (string, error) {
  93. //destStr := dest.String()
  94. //found, tag, err := v.cache.Get(destStr)
  95. //if !found {
  96. tag, err := v.takeDetourWithoutCache(session)
  97. //v.cache.Set(destStr, tag, err)
  98. return tag, err
  99. //}
  100. //return tag, err
  101. }
  102. func (Router) Interface() interface{} {
  103. return (*Router)(nil)
  104. }
  105. type RouterFactory struct{}
  106. func FromSpace(space app.Space) *Router {
  107. app := space.GetApplication((*Router)(nil))
  108. if app == nil {
  109. return nil
  110. }
  111. return app.(*Router)
  112. }
  113. func init() {
  114. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  115. return NewRouter(ctx, config.(*Config))
  116. }))
  117. }