config.go 3.7 KB

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