config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package router
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/xtls/xray-core/common/net"
  6. "github.com/xtls/xray-core/features/outbound"
  7. "github.com/xtls/xray-core/features/routing"
  8. )
  9. type Rule struct {
  10. Tag string
  11. Balancer *Balancer
  12. Condition Condition
  13. }
  14. func (r *Rule) GetTag() (string, error) {
  15. if r.Balancer != nil {
  16. return r.Balancer.PickOutbound()
  17. }
  18. return r.Tag, nil
  19. }
  20. // Apply checks rule matching of current routing context.
  21. func (r *Rule) Apply(ctx routing.Context) bool {
  22. return r.Condition.Apply(ctx)
  23. }
  24. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  25. conds := NewConditionChan()
  26. if len(rr.Domain) > 0 {
  27. switch rr.DomainMatcher {
  28. case "linear":
  29. matcher, err := NewDomainMatcher(rr.Domain)
  30. if err != nil {
  31. return nil, newError("failed to build domain condition").Base(err)
  32. }
  33. conds.Add(matcher)
  34. case "mph", "hybrid":
  35. fallthrough
  36. default:
  37. matcher, err := NewMphMatcherGroup(rr.Domain)
  38. if err != nil {
  39. return nil, newError("failed to build domain condition with MphDomainMatcher").Base(err)
  40. }
  41. newError("MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)").AtDebug().WriteToLog()
  42. conds.Add(matcher)
  43. }
  44. }
  45. if len(rr.UserEmail) > 0 {
  46. conds.Add(NewUserMatcher(rr.UserEmail))
  47. }
  48. if len(rr.InboundTag) > 0 {
  49. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  50. }
  51. if rr.PortList != nil {
  52. conds.Add(NewPortMatcher(rr.PortList, false))
  53. } else if rr.PortRange != nil {
  54. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, 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. } else if rr.NetworkList != nil {
  62. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  63. }
  64. if len(rr.Geoip) > 0 {
  65. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  66. if err != nil {
  67. return nil, err
  68. }
  69. conds.Add(cond)
  70. } else if len(rr.Cidr) > 0 {
  71. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
  72. if err != nil {
  73. return nil, err
  74. }
  75. conds.Add(cond)
  76. }
  77. if len(rr.SourceGeoip) > 0 {
  78. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  79. if err != nil {
  80. return nil, err
  81. }
  82. conds.Add(cond)
  83. } else if len(rr.SourceCidr) > 0 {
  84. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
  85. if err != nil {
  86. return nil, err
  87. }
  88. conds.Add(cond)
  89. }
  90. if len(rr.Protocol) > 0 {
  91. conds.Add(NewProtocolMatcher(rr.Protocol))
  92. }
  93. if len(rr.Attributes) > 0 {
  94. configuredKeys := make(map[string]*regexp.Regexp)
  95. for key, value := range rr.Attributes {
  96. configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
  97. }
  98. conds.Add(&AttributeMatcher{configuredKeys})
  99. }
  100. if conds.Len() == 0 {
  101. return nil, newError("this rule has no effective fields").AtWarning()
  102. }
  103. return conds, nil
  104. }
  105. func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
  106. switch br.Strategy {
  107. case "leastPing":
  108. return &Balancer{
  109. selectors: br.OutboundSelector,
  110. strategy: &LeastPingStrategy{},
  111. ohm: ohm,
  112. }, nil
  113. case "random":
  114. fallthrough
  115. default:
  116. return &Balancer{
  117. selectors: br.OutboundSelector,
  118. strategy: &RandomStrategy{},
  119. ohm: ohm,
  120. }, nil
  121. }
  122. }