1
0

config.go 3.6 KB

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