config.go 3.6 KB

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