config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package router
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/features/outbound"
  9. "github.com/xtls/xray-core/features/routing"
  10. )
  11. type Rule struct {
  12. Tag string
  13. RuleTag string
  14. Balancer *Balancer
  15. Condition Condition
  16. }
  17. func (r *Rule) GetTag() (string, error) {
  18. if r.Balancer != nil {
  19. return r.Balancer.PickOutbound()
  20. }
  21. return r.Tag, nil
  22. }
  23. // Apply checks rule matching of current routing context.
  24. func (r *Rule) Apply(ctx routing.Context) bool {
  25. return r.Condition.Apply(ctx)
  26. }
  27. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  28. conds := NewConditionChan()
  29. if len(rr.Domain) > 0 {
  30. switch rr.DomainMatcher {
  31. case "linear":
  32. matcher, err := NewDomainMatcher(rr.Domain)
  33. if err != nil {
  34. return nil, errors.New("failed to build domain condition").Base(err)
  35. }
  36. conds.Add(matcher)
  37. case "mph", "hybrid":
  38. fallthrough
  39. default:
  40. matcher, err := NewMphMatcherGroup(rr.Domain)
  41. if err != nil {
  42. return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
  43. }
  44. errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
  45. conds.Add(matcher)
  46. }
  47. }
  48. if len(rr.UserEmail) > 0 {
  49. conds.Add(NewUserMatcher(rr.UserEmail))
  50. }
  51. if len(rr.InboundTag) > 0 {
  52. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  53. }
  54. if rr.PortList != nil {
  55. conds.Add(NewPortMatcher(rr.PortList, false))
  56. } else if rr.PortRange != nil {
  57. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
  58. }
  59. if rr.SourcePortList != nil {
  60. conds.Add(NewPortMatcher(rr.SourcePortList, true))
  61. }
  62. if len(rr.Networks) > 0 {
  63. conds.Add(NewNetworkMatcher(rr.Networks))
  64. } else if rr.NetworkList != nil {
  65. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  66. }
  67. if len(rr.Geoip) > 0 {
  68. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  69. if err != nil {
  70. return nil, err
  71. }
  72. conds.Add(cond)
  73. } else if len(rr.Cidr) > 0 {
  74. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
  75. if err != nil {
  76. return nil, err
  77. }
  78. conds.Add(cond)
  79. }
  80. if len(rr.SourceGeoip) > 0 {
  81. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  82. if err != nil {
  83. return nil, err
  84. }
  85. conds.Add(cond)
  86. } else if len(rr.SourceCidr) > 0 {
  87. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
  88. if err != nil {
  89. return nil, err
  90. }
  91. conds.Add(cond)
  92. }
  93. if len(rr.Protocol) > 0 {
  94. conds.Add(NewProtocolMatcher(rr.Protocol))
  95. }
  96. if len(rr.Attributes) > 0 {
  97. configuredKeys := make(map[string]*regexp.Regexp)
  98. for key, value := range rr.Attributes {
  99. configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
  100. }
  101. conds.Add(&AttributeMatcher{configuredKeys})
  102. }
  103. if conds.Len() == 0 {
  104. return nil, errors.New("this rule has no effective fields").AtWarning()
  105. }
  106. return conds, nil
  107. }
  108. // Build builds the balancing rule
  109. func (br *BalancingRule) Build(ohm outbound.Manager, dispatcher routing.Dispatcher) (*Balancer, error) {
  110. switch strings.ToLower(br.Strategy) {
  111. case "leastping":
  112. return &Balancer{
  113. selectors: br.OutboundSelector,
  114. strategy: &LeastPingStrategy{},
  115. fallbackTag: br.FallbackTag,
  116. ohm: ohm,
  117. }, nil
  118. case "roundrobin":
  119. return &Balancer{
  120. selectors: br.OutboundSelector,
  121. strategy: &RoundRobinStrategy{FallbackTag: br.FallbackTag},
  122. fallbackTag: br.FallbackTag,
  123. ohm: ohm,
  124. }, nil
  125. case "leastload":
  126. i, err := br.StrategySettings.GetInstance()
  127. if err != nil {
  128. return nil, err
  129. }
  130. s, ok := i.(*StrategyLeastLoadConfig)
  131. if !ok {
  132. return nil, errors.New("not a StrategyLeastLoadConfig").AtError()
  133. }
  134. leastLoadStrategy := NewLeastLoadStrategy(s)
  135. return &Balancer{
  136. selectors: br.OutboundSelector,
  137. ohm: ohm,
  138. fallbackTag: br.FallbackTag,
  139. strategy: leastLoadStrategy,
  140. }, nil
  141. case "random":
  142. fallthrough
  143. case "":
  144. return &Balancer{
  145. selectors: br.OutboundSelector,
  146. ohm: ohm,
  147. fallbackTag: br.FallbackTag,
  148. strategy: &RandomStrategy{FallbackTag: br.FallbackTag},
  149. }, nil
  150. default:
  151. return nil, errors.New("unrecognized balancer type")
  152. }
  153. }