router.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, ok := proxy.TargetFromContext(ctx)
  64. if !ok {
  65. return "", ErrNoRuleApplicable
  66. }
  67. if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
  68. log.Trace(errors.New("looking up IP for ", dest).Path("App", "Router"))
  69. ipDests := v.resolveIP(dest)
  70. if ipDests != nil {
  71. ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
  72. for _, rule := range v.rules {
  73. if rule.Apply(ctx) {
  74. return rule.Tag, nil
  75. }
  76. }
  77. }
  78. }
  79. return "", ErrNoRuleApplicable
  80. }
  81. func (Router) Interface() interface{} {
  82. return (*Router)(nil)
  83. }
  84. func (Router) Start() error {
  85. return nil
  86. }
  87. func (Router) Close() {}
  88. func FromSpace(space app.Space) *Router {
  89. app := space.GetApplication((*Router)(nil))
  90. if app == nil {
  91. return nil
  92. }
  93. return app.(*Router)
  94. }
  95. func init() {
  96. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  97. return NewRouter(ctx, config.(*Config))
  98. }))
  99. }