config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // +build !confonly
  2. package router
  3. import (
  4. "context"
  5. net "v2ray.com/core/common/net"
  6. "v2ray.com/core/features/outbound"
  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. func (r *Rule) Apply(ctx context.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))
  69. } else if rr.PortRange != nil {
  70. conds.Add(NewPortMatcher(&net.PortList{Range: []*net.PortRange{rr.PortRange}}))
  71. }
  72. if len(rr.Networks) > 0 {
  73. conds.Add(NewNetworkMatcher(rr.Networks))
  74. } else if rr.NetworkList != nil {
  75. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  76. }
  77. if len(rr.Geoip) > 0 {
  78. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  79. if err != nil {
  80. return nil, err
  81. }
  82. conds.Add(cond)
  83. } else if len(rr.Cidr) > 0 {
  84. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
  85. if err != nil {
  86. return nil, err
  87. }
  88. conds.Add(cond)
  89. }
  90. if len(rr.SourceGeoip) > 0 {
  91. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  92. if err != nil {
  93. return nil, err
  94. }
  95. conds.Add(cond)
  96. } else if len(rr.SourceCidr) > 0 {
  97. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
  98. if err != nil {
  99. return nil, err
  100. }
  101. conds.Add(cond)
  102. }
  103. if len(rr.Protocol) > 0 {
  104. conds.Add(NewProtocolMatcher(rr.Protocol))
  105. }
  106. if conds.Len() == 0 {
  107. return nil, newError("this rule has no effective fields").AtWarning()
  108. }
  109. return conds, nil
  110. }
  111. func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
  112. return &Balancer{
  113. selectors: br.OutboundSelector,
  114. strategy: &RandomStrategy{},
  115. ohm: ohm,
  116. }, nil
  117. }