config.go 2.9 KB

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