config.go 3.8 KB

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