config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package router
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. )
  6. type CIDRList []*CIDR
  7. func (l *CIDRList) Len() int {
  8. return len(*l)
  9. }
  10. func (l *CIDRList) Less(i int, j int) bool {
  11. ci := (*l)[i]
  12. cj := (*l)[j]
  13. if len(ci.Ip) < len(cj.Ip) {
  14. return true
  15. }
  16. if len(ci.Ip) > len(cj.Ip) {
  17. return false
  18. }
  19. for k := 0; k < len(ci.Ip); k++ {
  20. if ci.Ip[k] < cj.Ip[k] {
  21. return true
  22. }
  23. if ci.Ip[k] > cj.Ip[k] {
  24. return false
  25. }
  26. }
  27. return ci.Prefix < cj.Prefix
  28. }
  29. func (l *CIDRList) Swap(i int, j int) {
  30. (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
  31. }
  32. type Rule struct {
  33. Tag string
  34. Condition Condition
  35. }
  36. func (r *Rule) Apply(ctx context.Context) bool {
  37. return r.Condition.Apply(ctx)
  38. }
  39. func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) {
  40. ipv4Net := net.NewIPNetTable()
  41. ipv6Cond := NewAnyCondition()
  42. hasIpv6 := false
  43. for _, ip := range cidr {
  44. switch len(ip.Ip) {
  45. case net.IPv4len:
  46. ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
  47. case net.IPv6len:
  48. hasIpv6 = true
  49. matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, source)
  50. if err != nil {
  51. return nil, err
  52. }
  53. ipv6Cond.Add(matcher)
  54. default:
  55. return nil, newError("invalid IP length").AtWarning()
  56. }
  57. }
  58. switch {
  59. case !ipv4Net.IsEmpty() && hasIpv6:
  60. cond := NewAnyCondition()
  61. cond.Add(NewIPv4Matcher(ipv4Net, source))
  62. cond.Add(ipv6Cond)
  63. return cond, nil
  64. case !ipv4Net.IsEmpty():
  65. return NewIPv4Matcher(ipv4Net, source), nil
  66. default:
  67. return ipv6Cond, nil
  68. }
  69. }
  70. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  71. conds := NewConditionChan()
  72. if len(rr.Domain) > 0 {
  73. matcher, err := NewDomainMatcher(rr.Domain)
  74. if err != nil {
  75. return nil, newError("failed to build domain condition").Base(err)
  76. }
  77. conds.Add(matcher)
  78. }
  79. if len(rr.UserEmail) > 0 {
  80. conds.Add(NewUserMatcher(rr.UserEmail))
  81. }
  82. if len(rr.InboundTag) > 0 {
  83. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  84. }
  85. if rr.PortRange != nil {
  86. conds.Add(NewPortMatcher(*rr.PortRange))
  87. }
  88. if rr.NetworkList != nil {
  89. conds.Add(NewNetworkMatcher(rr.NetworkList))
  90. }
  91. if len(rr.Geoip) > 0 {
  92. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  93. if err != nil {
  94. return nil, err
  95. }
  96. conds.Add(cond)
  97. } else if len(rr.Cidr) > 0 {
  98. cond, err := cidrToCondition(rr.Cidr, false)
  99. if err != nil {
  100. return nil, err
  101. }
  102. conds.Add(cond)
  103. }
  104. if len(rr.SourceGeoip) > 0 {
  105. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  106. if err != nil {
  107. return nil, err
  108. }
  109. conds.Add(cond)
  110. } else if len(rr.SourceCidr) > 0 {
  111. cond, err := cidrToCondition(rr.SourceCidr, true)
  112. if err != nil {
  113. return nil, err
  114. }
  115. conds.Add(cond)
  116. }
  117. if len(rr.Protocol) > 0 {
  118. conds.Add(NewProtocolMatcher(rr.Protocol))
  119. }
  120. if conds.Len() == 0 {
  121. return nil, newError("this rule has no effective fields").AtWarning()
  122. }
  123. return conds, nil
  124. }