router.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package router
  2. import (
  3. "context"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/app/log"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/proxy"
  11. )
  12. var (
  13. ErrNoRuleApplicable = errors.New("No rule applicable")
  14. )
  15. type Router struct {
  16. domainStrategy Config_DomainStrategy
  17. rules []Rule
  18. dnsServer dns.Server
  19. }
  20. func NewRouter(ctx context.Context, config *Config) (*Router, error) {
  21. space := app.SpaceFromContext(ctx)
  22. if space == nil {
  23. return nil, errors.New("Router: No space in context.")
  24. }
  25. r := &Router{
  26. domainStrategy: config.DomainStrategy,
  27. rules: make([]Rule, len(config.Rule)),
  28. }
  29. space.OnInitialize(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. r.dnsServer = dns.FromSpace(space)
  39. if r.dnsServer == nil {
  40. return errors.New("Router: DNS is not found in the space.")
  41. }
  42. return nil
  43. })
  44. return r, nil
  45. }
  46. func (v *Router) resolveIP(dest net.Destination) []net.Address {
  47. ips := v.dnsServer.Get(dest.Address.Domain())
  48. if len(ips) == 0 {
  49. return nil
  50. }
  51. dests := make([]net.Address, len(ips))
  52. for idx, ip := range ips {
  53. dests[idx] = net.IPAddress(ip)
  54. }
  55. return dests
  56. }
  57. func (v *Router) TakeDetour(ctx context.Context) (string, error) {
  58. for _, rule := range v.rules {
  59. if rule.Apply(ctx) {
  60. return rule.Tag, nil
  61. }
  62. }
  63. dest := proxy.DestinationFromContext(ctx)
  64. if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  65. log.Info("Router: Looking up IP for ", dest)
  66. ipDests := v.resolveIP(dest)
  67. if ipDests != nil {
  68. ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
  69. for _, rule := range v.rules {
  70. if rule.Apply(ctx) {
  71. return rule.Tag, nil
  72. }
  73. }
  74. }
  75. }
  76. return "", ErrNoRuleApplicable
  77. }
  78. func (Router) Interface() interface{} {
  79. return (*Router)(nil)
  80. }
  81. func (Router) Start() error {
  82. return nil
  83. }
  84. func (Router) Close() {}
  85. func FromSpace(space app.Space) *Router {
  86. app := space.GetApplication((*Router)(nil))
  87. if app == nil {
  88. return nil
  89. }
  90. return app.(*Router)
  91. }
  92. func init() {
  93. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  94. return NewRouter(ctx, config.(*Config))
  95. }))
  96. }