config.go 3.1 KB

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