router.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package rules
  2. import (
  3. "errors"
  4. "github.com/v2ray/v2ray-core/app"
  5. "github.com/v2ray/v2ray-core/app/dns"
  6. "github.com/v2ray/v2ray-core/app/router"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. )
  10. var (
  11. ErrInvalidRule = errors.New("Invalid Rule")
  12. ErrNoRuleApplicable = errors.New("No rule applicable")
  13. )
  14. type Router struct {
  15. config *RouterRuleConfig
  16. cache *RoutingTable
  17. dnsServer dns.Server
  18. }
  19. func NewRouter(config *RouterRuleConfig, space app.Space) *Router {
  20. r := &Router{
  21. config: config,
  22. cache: NewRoutingTable(),
  23. }
  24. space.InitializeApplication(func() error {
  25. if !space.HasApp(dns.APP_ID) {
  26. log.Error("DNS: Router is not found in the space.")
  27. return app.ErrMissingApplication
  28. }
  29. r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
  30. return nil
  31. })
  32. return r
  33. }
  34. func (this *Router) Release() {
  35. }
  36. // @Private
  37. func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
  38. ips := this.dnsServer.Get(dest.Address().Domain())
  39. if len(ips) == 0 {
  40. return nil
  41. }
  42. dests := make([]v2net.Destination, len(ips))
  43. for idx, ip := range ips {
  44. if dest.IsTCP() {
  45. dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port())
  46. } else {
  47. dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port())
  48. }
  49. }
  50. return dests
  51. }
  52. func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
  53. for _, rule := range this.config.Rules {
  54. if rule.Apply(dest) {
  55. return rule.Tag, nil
  56. }
  57. }
  58. if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().Family().IsDomain() {
  59. log.Info("Router: Looking up IP for ", dest)
  60. ipDests := this.ResolveIP(dest)
  61. if ipDests != nil {
  62. for _, ipDest := range ipDests {
  63. log.Info("Router: Trying IP ", ipDest)
  64. for _, rule := range this.config.Rules {
  65. if rule.Apply(ipDest) {
  66. return rule.Tag, nil
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return "", ErrNoRuleApplicable
  73. }
  74. func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
  75. destStr := dest.String()
  76. found, tag, err := this.cache.Get(destStr)
  77. if !found {
  78. tag, err := this.takeDetourWithoutCache(dest)
  79. this.cache.Set(destStr, tag, err)
  80. return tag, err
  81. }
  82. return tag, err
  83. }
  84. type RouterFactory struct {
  85. }
  86. func (this *RouterFactory) Create(rawConfig interface{}, space app.Space) (router.Router, error) {
  87. return NewRouter(rawConfig.(*RouterRuleConfig), space), nil
  88. }
  89. func init() {
  90. router.RegisterRouter("rules", &RouterFactory{})
  91. }