router.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package router
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/app/dns"
  5. "v2ray.com/core/common/errors"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/proxy"
  10. )
  11. const (
  12. APP_ID = app.ID(3)
  13. )
  14. var (
  15. ErrInvalidRule = errors.New("Invalid Rule")
  16. ErrNoRuleApplicable = errors.New("No rule applicable")
  17. )
  18. type Router struct {
  19. domainStrategy Config_DomainStrategy
  20. rules []Rule
  21. // cache *RoutingTable
  22. dnsServer dns.Server
  23. }
  24. func NewRouter(config *Config, space app.Space) *Router {
  25. r := &Router{
  26. domainStrategy: config.DomainStrategy,
  27. //cache: NewRoutingTable(),
  28. rules: make([]Rule, len(config.Rule)),
  29. }
  30. space.InitializeApplication(func() error {
  31. for idx, rule := range config.Rule {
  32. r.rules[idx].Tag = rule.Tag
  33. cond, err := rule.BuildCondition()
  34. if err != nil {
  35. return err
  36. }
  37. r.rules[idx].Condition = cond
  38. }
  39. if !space.HasApp(dns.APP_ID) {
  40. return errors.New("Router: DNS is not found in the space.")
  41. }
  42. r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
  43. return nil
  44. })
  45. return r
  46. }
  47. func (v *Router) Release() {
  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. type RouterFactory struct{}
  103. func (RouterFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  104. router := NewRouter(config.(*Config), space)
  105. return router, nil
  106. }
  107. func (RouterFactory) AppId() app.ID {
  108. return APP_ID
  109. }
  110. func init() {
  111. app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
  112. }