config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package router
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  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. Condition Condition
  39. }
  40. func (r *Rule) Apply(ctx context.Context) bool {
  41. return r.Condition.Apply(ctx)
  42. }
  43. func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) {
  44. ipv4Net := net.NewIPNetTable()
  45. ipv6Cond := NewAnyCondition()
  46. hasIpv6 := false
  47. for _, ip := range cidr {
  48. switch len(ip.Ip) {
  49. case net.IPv4len:
  50. ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
  51. case net.IPv6len:
  52. hasIpv6 = true
  53. matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix, source)
  54. if err != nil {
  55. return nil, err
  56. }
  57. ipv6Cond.Add(matcher)
  58. default:
  59. return nil, newError("invalid IP length").AtWarning()
  60. }
  61. }
  62. switch {
  63. case !ipv4Net.IsEmpty() && hasIpv6:
  64. cond := NewAnyCondition()
  65. cond.Add(NewIPv4Matcher(ipv4Net, source))
  66. cond.Add(ipv6Cond)
  67. return cond, nil
  68. case !ipv4Net.IsEmpty():
  69. return NewIPv4Matcher(ipv4Net, source), nil
  70. default:
  71. return ipv6Cond, nil
  72. }
  73. }
  74. func (rr *RoutingRule) BuildCondition() (Condition, error) {
  75. conds := NewConditionChan()
  76. if len(rr.Domain) > 0 {
  77. matcher, err := NewDomainMatcher(rr.Domain)
  78. if err != nil {
  79. return nil, newError("failed to build domain condition").Base(err)
  80. }
  81. conds.Add(matcher)
  82. }
  83. if len(rr.UserEmail) > 0 {
  84. conds.Add(NewUserMatcher(rr.UserEmail))
  85. }
  86. if len(rr.InboundTag) > 0 {
  87. conds.Add(NewInboundTagMatcher(rr.InboundTag))
  88. }
  89. if rr.PortRange != nil {
  90. conds.Add(NewPortMatcher(*rr.PortRange))
  91. }
  92. if rr.NetworkList != nil {
  93. conds.Add(NewNetworkMatcher(rr.NetworkList))
  94. }
  95. if len(rr.Geoip) > 0 {
  96. cond, err := NewMultiGeoIPMatcher(rr.Geoip, false)
  97. if err != nil {
  98. return nil, err
  99. }
  100. conds.Add(cond)
  101. } else if len(rr.Cidr) > 0 {
  102. cond, err := cidrToCondition(rr.Cidr, false)
  103. if err != nil {
  104. return nil, err
  105. }
  106. conds.Add(cond)
  107. }
  108. if len(rr.SourceGeoip) > 0 {
  109. cond, err := NewMultiGeoIPMatcher(rr.SourceGeoip, true)
  110. if err != nil {
  111. return nil, err
  112. }
  113. conds.Add(cond)
  114. } else if len(rr.SourceCidr) > 0 {
  115. cond, err := cidrToCondition(rr.SourceCidr, true)
  116. if err != nil {
  117. return nil, err
  118. }
  119. conds.Add(cond)
  120. }
  121. if len(rr.Protocol) > 0 {
  122. conds.Add(NewProtocolMatcher(rr.Protocol))
  123. }
  124. if conds.Len() == 0 {
  125. return nil, newError("this rule has no effective fields").AtWarning()
  126. }
  127. return conds, nil
  128. }