router.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "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. log.Error("Router: DNS is not found in the space.")
  41. return app.ErrMissingApplication
  42. }
  43. r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
  44. return nil
  45. })
  46. return r
  47. }
  48. func (this *Router) Release() {
  49. }
  50. // Private: Visible for testing.
  51. func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
  52. ips := this.dnsServer.Get(dest.Address.Domain())
  53. if len(ips) == 0 {
  54. return nil
  55. }
  56. dests := make([]v2net.Destination, len(ips))
  57. for idx, ip := range ips {
  58. if dest.Network == v2net.Network_TCP {
  59. dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port)
  60. } else {
  61. dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port)
  62. }
  63. }
  64. return dests
  65. }
  66. func (this *Router) takeDetourWithoutCache(session *proxy.SessionInfo) (string, error) {
  67. for _, rule := range this.rules {
  68. if rule.Apply(session) {
  69. return rule.Tag, nil
  70. }
  71. }
  72. dest := session.Destination
  73. if this.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  74. log.Info("Router: Looking up IP for ", dest)
  75. ipDests := this.ResolveIP(dest)
  76. if ipDests != nil {
  77. for _, ipDest := range ipDests {
  78. log.Info("Router: Trying IP ", ipDest)
  79. for _, rule := range this.rules {
  80. if rule.Apply(&proxy.SessionInfo{
  81. Source: session.Source,
  82. Destination: ipDest,
  83. User: session.User,
  84. }) {
  85. return rule.Tag, nil
  86. }
  87. }
  88. }
  89. }
  90. }
  91. return "", ErrNoRuleApplicable
  92. }
  93. func (this *Router) TakeDetour(session *proxy.SessionInfo) (string, error) {
  94. //destStr := dest.String()
  95. //found, tag, err := this.cache.Get(destStr)
  96. //if !found {
  97. tag, err := this.takeDetourWithoutCache(session)
  98. //this.cache.Set(destStr, tag, err)
  99. return tag, err
  100. //}
  101. //return tag, err
  102. }
  103. type RouterFactory struct{}
  104. func (RouterFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  105. router := NewRouter(config.(*Config), space)
  106. return router, nil
  107. }
  108. func (RouterFactory) AppId() app.ID {
  109. return APP_ID
  110. }
  111. func init() {
  112. app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
  113. }