config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. matcher, err := NewDomainMatcher(rr.Domain)
  56. if err != nil {
  57. return nil, newError("failed to build domain condition").Base(err)
  58. }
  59. conds.Add(matcher)
  60. }
  61. if len(rr.UserEmail) > 0 {
  62. conds.Add(NewUserMatcher(rr.UserEmail))
  63. }
  64. if len(rr.InboundTag) > 0 {
  65. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  66. }
  67. if rr.PortList != nil {
  68. conds.Add(NewPortMatcher(rr.PortList, false))
  69. } else if rr.PortRange != nil {
  70. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}, false))
  71. }
  72. if rr.SourcePortList != nil {
  73. conds.Add(NewPortMatcher(rr.SourcePortList, true))
  74. }
  75. if len(rr.Networks) > 0 {
  76. conds.Add(NewNetworkMatcher(rr.Networks))
  77. } else if rr.NetworkList != nil {
  78. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  79. }
  80. if len(rr.Geoip) > 0 {
  81. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  82. if err != nil {
  83. return nil, err
  84. }
  85. conds.Add(cond)
  86. } else if len(rr.Cidr) > 0 {
  87. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
  88. if err != nil {
  89. return nil, err
  90. }
  91. conds.Add(cond)
  92. }
  93. if len(rr.SourceGeoip) > 0 {
  94. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  95. if err != nil {
  96. return nil, err
  97. }
  98. conds.Add(cond)
  99. } else if len(rr.SourceCidr) > 0 {
  100. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
  101. if err != nil {
  102. return nil, err
  103. }
  104. conds.Add(cond)
  105. }
  106. if len(rr.Protocol) > 0 {
  107. conds.Add(NewProtocolMatcher(rr.Protocol))
  108. }
  109. if len(rr.Attributes) > 0 {
  110. cond, err := NewAttributeMatcher(rr.Attributes)
  111. if err != nil {
  112. return nil, err
  113. }
  114. conds.Add(cond)
  115. }
  116. if conds.Len() == 0 {
  117. return nil, newError("this rule has no effective fields").AtWarning()
  118. }
  119. return conds, nil
  120. }
  121. func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
  122. return &Balancer{
  123. selectors: br.OutboundSelector,
  124. strategy: &RandomStrategy{},
  125. ohm: ohm,
  126. }, nil
  127. }