router.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // +build !confonly
  2. package router
  3. //go:generate errorgen
  4. import (
  5. "context"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/features/dns"
  10. "v2ray.com/core/features/outbound"
  11. "v2ray.com/core/features/routing"
  12. )
  13. func init() {
  14. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  15. r := new(Router)
  16. if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
  17. return r.Init(config.(*Config), d, ohm)
  18. }); err != nil {
  19. return nil, err
  20. }
  21. return r, nil
  22. }))
  23. }
  24. // Router is an implementation of routing.Router.
  25. type Router struct {
  26. domainStrategy Config_DomainStrategy
  27. rules []*Rule
  28. balancers map[string]*Balancer
  29. dns dns.Client
  30. }
  31. // Init initializes the Router.
  32. func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
  33. r.domainStrategy = config.DomainStrategy
  34. r.dns = d
  35. r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
  36. for _, rule := range config.BalancingRule {
  37. balancer, err := rule.Build(ohm)
  38. if err != nil {
  39. return err
  40. }
  41. r.balancers[rule.Tag] = balancer
  42. }
  43. r.rules = make([]*Rule, 0, len(config.Rule))
  44. for _, rule := range config.Rule {
  45. cond, err := rule.BuildCondition()
  46. if err != nil {
  47. return err
  48. }
  49. rr := &Rule{
  50. Condition: cond,
  51. Tag: rule.GetTag(),
  52. }
  53. btag := rule.GetBalancingTag()
  54. if len(btag) > 0 {
  55. brule, found := r.balancers[btag]
  56. if !found {
  57. return newError("balancer ", btag, " not found")
  58. }
  59. rr.Balancer = brule
  60. }
  61. r.rules = append(r.rules, rr)
  62. }
  63. return nil
  64. }
  65. // PickRoute implements routing.Router.
  66. func (r *Router) PickRoute(ctx routing.Context) (string, error) {
  67. rule, err := r.pickRouteInternal(ctx)
  68. if err != nil {
  69. return "", err
  70. }
  71. return rule.GetTag()
  72. }
  73. func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, error) {
  74. if r.domainStrategy == Config_IpOnDemand {
  75. ctx = ContextWithDNSClient(ctx, r.dns)
  76. }
  77. for _, rule := range r.rules {
  78. if rule.Apply(ctx) {
  79. return rule, nil
  80. }
  81. }
  82. if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 {
  83. return nil, common.ErrNoClue
  84. }
  85. ctx = ContextWithDNSClient(ctx, r.dns)
  86. // Try applying rules again if we have IPs.
  87. for _, rule := range r.rules {
  88. if rule.Apply(ctx) {
  89. return rule, nil
  90. }
  91. }
  92. return nil, common.ErrNoClue
  93. }
  94. // Start implements common.Runnable.
  95. func (*Router) Start() error {
  96. return nil
  97. }
  98. // Close implements common.Closable.
  99. func (*Router) Close() error {
  100. return nil
  101. }
  102. // Type implement common.HasType.
  103. func (*Router) Type() interface{} {
  104. return routing.RouterType()
  105. }
  106. // ContextWithDNSClient creates a new routing context with domain resolving capability. Resolved domain IPs can be retrieved by GetTargetIPs().
  107. func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
  108. return &resolvableContext{Context: ctx, dnsClient: client}
  109. }
  110. type resolvableContext struct {
  111. routing.Context
  112. dnsClient dns.Client
  113. resolvedIPs []net.IP
  114. }
  115. func (ctx *resolvableContext) GetTargetIPs() []net.IP {
  116. if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
  117. return ips
  118. }
  119. if len(ctx.resolvedIPs) > 0 {
  120. return ctx.resolvedIPs
  121. }
  122. if domain := ctx.GetTargetDomain(); len(domain) != 0 {
  123. ips, err := ctx.dnsClient.LookupIP(domain)
  124. if err == nil {
  125. ctx.resolvedIPs = ips
  126. return ips
  127. }
  128. newError("resolve ip for ", domain).Base(err).WriteToLog()
  129. }
  130. return nil
  131. }