router.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package router
  2. import (
  3. "errors"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common/log"
  7. v2net "v2ray.com/core/common/net"
  8. )
  9. const (
  10. APP_ID = app.ID(3)
  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(config *Config, space app.Space) *Router {
  23. r := &Router{
  24. domainStrategy: config.DomainStrategy,
  25. cache: NewRoutingTable(),
  26. rules: make([]Rule, len(config.Rule)),
  27. }
  28. space.InitializeApplication(func() error {
  29. for idx, rule := range config.Rule {
  30. r.rules[idx].Tag = rule.Tag
  31. cond, err := rule.BuildCondition()
  32. if err != nil {
  33. return err
  34. }
  35. r.rules[idx].Condition = cond
  36. }
  37. if !space.HasApp(dns.APP_ID) {
  38. log.Error("DNS: Router is not found in the space.")
  39. return app.ErrMissingApplication
  40. }
  41. r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
  42. return nil
  43. })
  44. return r
  45. }
  46. func (this *Router) Release() {
  47. }
  48. // Private: Visible for testing.
  49. func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
  50. ips := this.dnsServer.Get(dest.Address.Domain())
  51. if len(ips) == 0 {
  52. return nil
  53. }
  54. dests := make([]v2net.Destination, len(ips))
  55. for idx, ip := range ips {
  56. if dest.Network == v2net.Network_TCP {
  57. dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port)
  58. } else {
  59. dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port)
  60. }
  61. }
  62. return dests
  63. }
  64. func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
  65. for _, rule := range this.rules {
  66. if rule.Apply(dest) {
  67. return rule.Tag, nil
  68. }
  69. }
  70. if this.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  71. log.Info("Router: Looking up IP for ", dest)
  72. ipDests := this.ResolveIP(dest)
  73. if ipDests != nil {
  74. for _, ipDest := range ipDests {
  75. log.Info("Router: Trying IP ", ipDest)
  76. for _, rule := range this.rules {
  77. if rule.Apply(ipDest) {
  78. return rule.Tag, nil
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return "", ErrNoRuleApplicable
  85. }
  86. func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
  87. destStr := dest.String()
  88. found, tag, err := this.cache.Get(destStr)
  89. if !found {
  90. tag, err := this.takeDetourWithoutCache(dest)
  91. this.cache.Set(destStr, tag, err)
  92. return tag, err
  93. }
  94. return tag, err
  95. }