config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package router
  2. import (
  3. "context"
  4. "v2ray.com/core/features/outbound"
  5. )
  6. // CIDRList is an alias of []*CIDR to provide sort.Interface.
  7. type CIDRList []*CIDR
  8. // Len implements sort.Interface.
  9. func (l *CIDRList) Len() int {
  10. return len(*l)
  11. }
  12. // Less implements sort.Interface.
  13. func (l *CIDRList) Less(i int, j int) bool {
  14. ci := (*l)[i]
  15. cj := (*l)[j]
  16. if len(ci.Ip) < len(cj.Ip) {
  17. return true
  18. }
  19. if len(ci.Ip) > len(cj.Ip) {
  20. return false
  21. }
  22. for k := 0; k < len(ci.Ip); k++ {
  23. if ci.Ip[k] < cj.Ip[k] {
  24. return true
  25. }
  26. if ci.Ip[k] > cj.Ip[k] {
  27. return false
  28. }
  29. }
  30. return ci.Prefix < cj.Prefix
  31. }
  32. // Swap implements sort.Interface.
  33. func (l *CIDRList) Swap(i int, j int) {
  34. (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
  35. }
  36. type Rule struct {
  37. Tag string
  38. Balancer *Balancer
  39. Condition Condition
  40. }
  41. func (r *Rule) GetTag() (string, error) {
  42. if r.Balancer != nil {
  43. return r.Balancer.PickOutbound()
  44. }
  45. return r.Tag, nil
  46. }
  47. func (r *Rule) Apply(ctx context.Context) bool {
  48. return r.Condition.Apply(ctx)
  49. }
  50. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  51. conds := NewConditionChan()
  52. if len(rr.Domain) > 0 {
  53. matcher, err := NewDomainMatcher(rr.Domain)
  54. if err != nil {
  55. return nil, newError("failed to build domain condition").Base(err)
  56. }
  57. conds.Add(matcher)
  58. }
  59. if len(rr.UserEmail) > 0 {
  60. conds.Add(NewUserMatcher(rr.UserEmail))
  61. }
  62. if len(rr.InboundTag) > 0 {
  63. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  64. }
  65. if rr.PortRange != nil {
  66. conds.Add(NewPortMatcher(*rr.PortRange))
  67. }
  68. if rr.NetworkList != nil {
  69. conds.Add(NewNetworkMatcher(rr.NetworkList.Network))
  70. }
  71. if len(rr.Geoip) > 0 {
  72. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  73. if err != nil {
  74. return nil, err
  75. }
  76. conds.Add(cond)
  77. } else if len(rr.Cidr) > 0 {
  78. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.Cidr}}, false)
  79. if err != nil {
  80. return nil, err
  81. }
  82. conds.Add(cond)
  83. }
  84. if len(rr.SourceGeoip) > 0 {
  85. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  86. if err != nil {
  87. return nil, err
  88. }
  89. conds.Add(cond)
  90. } else if len(rr.SourceCidr) > 0 {
  91. cond, err := NewMultiGeoIPMatcher([]*GeoIP{{Cidr: rr.SourceCidr}}, true)
  92. if err != nil {
  93. return nil, err
  94. }
  95. conds.Add(cond)
  96. }
  97. if len(rr.Protocol) > 0 {
  98. conds.Add(NewProtocolMatcher(rr.Protocol))
  99. }
  100. if conds.Len() == 0 {
  101. return nil, newError("this rule has no effective fields").AtWarning()
  102. }
  103. return conds, nil
  104. }
  105. func (br *BalancingRule) Build(ohm outbound.Manager) (*Balancer, error) {
  106. return &Balancer{
  107. selectors: br.OutboundSelector,
  108. strategy: &RandomStrategy{},
  109. ohm: ohm,
  110. }, nil
  111. }