router.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/common/session"
  10. "v2ray.com/core/features/dns"
  11. "v2ray.com/core/features/outbound"
  12. "v2ray.com/core/features/routing"
  13. )
  14. func init() {
  15. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  16. r := new(Router)
  17. if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
  18. return r.Init(config.(*Config), d, ohm)
  19. }); err != nil {
  20. return nil, err
  21. }
  22. return r, nil
  23. }))
  24. }
  25. // Router is an implementation of routing.Router.
  26. type Router struct {
  27. domainStrategy Config_DomainStrategy
  28. rules []*Rule
  29. balancers map[string]*Balancer
  30. dns dns.Client
  31. }
  32. // Init initializes the Router.
  33. func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
  34. r.domainStrategy = config.DomainStrategy
  35. r.dns = d
  36. r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
  37. for _, rule := range config.BalancingRule {
  38. balancer, err := rule.Build(ohm)
  39. if err != nil {
  40. return err
  41. }
  42. r.balancers[rule.Tag] = balancer
  43. }
  44. r.rules = make([]*Rule, 0, len(config.Rule))
  45. for _, rule := range config.Rule {
  46. cond, err := rule.BuildCondition()
  47. if err != nil {
  48. return err
  49. }
  50. rr := &Rule{
  51. Condition: cond,
  52. Tag: rule.GetTag(),
  53. }
  54. btag := rule.GetBalancingTag()
  55. if len(btag) > 0 {
  56. brule, found := r.balancers[btag]
  57. if !found {
  58. return newError("balancer ", btag, " not found")
  59. }
  60. rr.Balancer = brule
  61. }
  62. r.rules = append(r.rules, rr)
  63. }
  64. return nil
  65. }
  66. func (r *Router) PickRoute(ctx context.Context) (string, error) {
  67. rule, err := r.pickRouteInternal(ctx)
  68. if err != nil {
  69. return "", err
  70. }
  71. return rule.GetTag()
  72. }
  73. func isDomainOutbound(outbound *session.Outbound) bool {
  74. return outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain()
  75. }
  76. // PickRoute implements routing.Router.
  77. func (r *Router) pickRouteInternal(ctx context.Context) (*Rule, error) {
  78. sessionContext := &Context{
  79. Inbound: session.InboundFromContext(ctx),
  80. Outbound: session.OutboundFromContext(ctx),
  81. Content: session.ContentFromContext(ctx),
  82. }
  83. if r.domainStrategy == Config_IpOnDemand {
  84. sessionContext.dnsClient = r.dns
  85. }
  86. for _, rule := range r.rules {
  87. if rule.Apply(sessionContext) {
  88. return rule, nil
  89. }
  90. }
  91. if r.domainStrategy != Config_IpIfNonMatch || !isDomainOutbound(sessionContext.Outbound) {
  92. return nil, common.ErrNoClue
  93. }
  94. sessionContext.dnsClient = r.dns
  95. // Try applying rules again if we have IPs.
  96. for _, rule := range r.rules {
  97. if rule.Apply(sessionContext) {
  98. return rule, nil
  99. }
  100. }
  101. return nil, common.ErrNoClue
  102. }
  103. // Start implements common.Runnable.
  104. func (*Router) Start() error {
  105. return nil
  106. }
  107. // Close implements common.Closable.
  108. func (*Router) Close() error {
  109. return nil
  110. }
  111. // Type implement common.HasType.
  112. func (*Router) Type() interface{} {
  113. return routing.RouterType()
  114. }
  115. type Context struct {
  116. Inbound *session.Inbound
  117. Outbound *session.Outbound
  118. Content *session.Content
  119. dnsClient dns.Client
  120. }
  121. func (c *Context) GetTargetIPs() []net.IP {
  122. if c.Outbound == nil || !c.Outbound.Target.IsValid() {
  123. return nil
  124. }
  125. if c.Outbound.Target.Address.Family().IsIP() {
  126. return []net.IP{c.Outbound.Target.Address.IP()}
  127. }
  128. if len(c.Outbound.ResolvedIPs) > 0 {
  129. return c.Outbound.ResolvedIPs
  130. }
  131. if c.dnsClient != nil {
  132. domain := c.Outbound.Target.Address.Domain()
  133. ips, err := c.dnsClient.LookupIP(domain)
  134. if err == nil {
  135. c.Outbound.ResolvedIPs = ips
  136. return ips
  137. }
  138. newError("resolve ip for ", domain).Base(err).WriteToLog()
  139. }
  140. return nil
  141. }