router.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package router
  2. import (
  3. "errors"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. )
  10. const (
  11. APP_ID = app.ID(3)
  12. )
  13. var (
  14. ErrInvalidRule = errors.New("Invalid Rule")
  15. ErrNoRuleApplicable = errors.New("No rule applicable")
  16. )
  17. type Router struct {
  18. domainStrategy Config_DomainStrategy
  19. rules []Rule
  20. cache *RoutingTable
  21. dnsServer dns.Server
  22. }
  23. func NewRouter(config *Config, space app.Space) *Router {
  24. r := &Router{
  25. domainStrategy: config.DomainStrategy,
  26. cache: NewRoutingTable(),
  27. rules: make([]Rule, len(config.Rule)),
  28. }
  29. space.InitializeApplication(func() error {
  30. for idx, rule := range config.Rule {
  31. r.rules[idx].Tag = rule.Tag
  32. cond, err := rule.BuildCondition()
  33. if err != nil {
  34. return err
  35. }
  36. r.rules[idx].Condition = cond
  37. }
  38. if !space.HasApp(dns.APP_ID) {
  39. log.Error("DNS: Router is not found in the space.")
  40. return app.ErrMissingApplication
  41. }
  42. r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
  43. return nil
  44. })
  45. return r
  46. }
  47. func (this *Router) Release() {
  48. }
  49. // Private: Visible for testing.
  50. func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
  51. ips := this.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 (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
  66. for _, rule := range this.rules {
  67. if rule.Apply(dest) {
  68. return rule.Tag, nil
  69. }
  70. }
  71. if this.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  72. log.Info("Router: Looking up IP for ", dest)
  73. ipDests := this.ResolveIP(dest)
  74. if ipDests != nil {
  75. for _, ipDest := range ipDests {
  76. log.Info("Router: Trying IP ", ipDest)
  77. for _, rule := range this.rules {
  78. if rule.Apply(ipDest) {
  79. return rule.Tag, nil
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return "", ErrNoRuleApplicable
  86. }
  87. func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
  88. destStr := dest.String()
  89. found, tag, err := this.cache.Get(destStr)
  90. if !found {
  91. tag, err := this.takeDetourWithoutCache(dest)
  92. this.cache.Set(destStr, tag, err)
  93. return tag, err
  94. }
  95. return tag, err
  96. }
  97. type RouterFactory struct{}
  98. func (RouterFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  99. router := NewRouter(config.(*Config), space)
  100. return router, nil
  101. }
  102. func (RouterFactory) AppId() app.ID {
  103. return APP_ID
  104. }
  105. func init() {
  106. app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
  107. }