condition_geoip.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package router
  2. import (
  3. "encoding/binary"
  4. "sort"
  5. "github.com/v2fly/v2ray-core/v4/common/net"
  6. )
  7. type ipv6 struct {
  8. a uint64
  9. b uint64
  10. }
  11. type GeoIPMatcher struct {
  12. countryCode string
  13. inverseMatch bool
  14. ip4 []uint32
  15. prefix4 []uint8
  16. ip6 []ipv6
  17. prefix6 []uint8
  18. }
  19. func normalize4(ip uint32, prefix uint8) uint32 {
  20. return (ip >> (32 - prefix)) << (32 - prefix)
  21. }
  22. func normalize6(ip ipv6, prefix uint8) ipv6 {
  23. if prefix <= 64 {
  24. ip.a = (ip.a >> (64 - prefix)) << (64 - prefix)
  25. ip.b = 0
  26. } else {
  27. ip.b = (ip.b >> (128 - prefix)) << (128 - prefix)
  28. }
  29. return ip
  30. }
  31. func (m *GeoIPMatcher) Init(cidrs []*CIDR) error {
  32. ip4Count := 0
  33. ip6Count := 0
  34. for _, cidr := range cidrs {
  35. ip := cidr.Ip
  36. switch len(ip) {
  37. case 4:
  38. ip4Count++
  39. case 16:
  40. ip6Count++
  41. default:
  42. return newError("unexpect ip length: ", len(ip))
  43. }
  44. }
  45. cidrList := CIDRList(cidrs)
  46. sort.Sort(&cidrList)
  47. m.ip4 = make([]uint32, 0, ip4Count)
  48. m.prefix4 = make([]uint8, 0, ip4Count)
  49. m.ip6 = make([]ipv6, 0, ip6Count)
  50. m.prefix6 = make([]uint8, 0, ip6Count)
  51. for _, cidr := range cidrs {
  52. ip := cidr.Ip
  53. prefix := uint8(cidr.Prefix)
  54. switch len(ip) {
  55. case 4:
  56. m.ip4 = append(m.ip4, normalize4(binary.BigEndian.Uint32(ip), prefix))
  57. m.prefix4 = append(m.prefix4, prefix)
  58. case 16:
  59. ip6 := ipv6{
  60. a: binary.BigEndian.Uint64(ip[0:8]),
  61. b: binary.BigEndian.Uint64(ip[8:16]),
  62. }
  63. ip6 = normalize6(ip6, prefix)
  64. m.ip6 = append(m.ip6, ip6)
  65. m.prefix6 = append(m.prefix6, prefix)
  66. }
  67. }
  68. return nil
  69. }
  70. func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
  71. m.inverseMatch = isReverseMatch
  72. }
  73. func (m *GeoIPMatcher) match4(ip uint32) bool {
  74. if len(m.ip4) == 0 {
  75. return false
  76. }
  77. if ip < m.ip4[0] {
  78. return false
  79. }
  80. size := uint32(len(m.ip4))
  81. l := uint32(0)
  82. r := size
  83. for l < r {
  84. x := ((l + r) >> 1)
  85. if ip < m.ip4[x] {
  86. r = x
  87. continue
  88. }
  89. nip := normalize4(ip, m.prefix4[x])
  90. if nip == m.ip4[x] {
  91. return true
  92. }
  93. l = x + 1
  94. }
  95. return l > 0 && normalize4(ip, m.prefix4[l-1]) == m.ip4[l-1]
  96. }
  97. func less6(a ipv6, b ipv6) bool {
  98. return a.a < b.a || (a.a == b.a && a.b < b.b)
  99. }
  100. func (m *GeoIPMatcher) match6(ip ipv6) bool {
  101. if len(m.ip6) == 0 {
  102. return false
  103. }
  104. if less6(ip, m.ip6[0]) {
  105. return false
  106. }
  107. size := uint32(len(m.ip6))
  108. l := uint32(0)
  109. r := size
  110. for l < r {
  111. x := (l + r) / 2
  112. if less6(ip, m.ip6[x]) {
  113. r = x
  114. continue
  115. }
  116. if normalize6(ip, m.prefix6[x]) == m.ip6[x] {
  117. return true
  118. }
  119. l = x + 1
  120. }
  121. return l > 0 && normalize6(ip, m.prefix6[l-1]) == m.ip6[l-1]
  122. }
  123. // Match returns true if the given ip is included by the GeoIP.
  124. func (m *GeoIPMatcher) Match(ip net.IP) bool {
  125. switch len(ip) {
  126. case 4:
  127. if m.inverseMatch {
  128. return !m.match4(binary.BigEndian.Uint32(ip))
  129. }
  130. return m.match4(binary.BigEndian.Uint32(ip))
  131. case 16:
  132. if m.inverseMatch {
  133. return !m.match6(ipv6{
  134. a: binary.BigEndian.Uint64(ip[0:8]),
  135. b: binary.BigEndian.Uint64(ip[8:16]),
  136. })
  137. }
  138. return m.match6(ipv6{
  139. a: binary.BigEndian.Uint64(ip[0:8]),
  140. b: binary.BigEndian.Uint64(ip[8:16]),
  141. })
  142. default:
  143. return false
  144. }
  145. }
  146. // GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
  147. type GeoIPMatcherContainer struct {
  148. matchers []*GeoIPMatcher
  149. }
  150. // Add adds a new GeoIP set into the container.
  151. // If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
  152. func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
  153. if len(geoip.CountryCode) > 0 {
  154. for _, m := range c.matchers {
  155. if m.countryCode == geoip.CountryCode && m.inverseMatch == geoip.InverseMatch {
  156. return m, nil
  157. }
  158. }
  159. }
  160. m := &GeoIPMatcher{
  161. countryCode: geoip.CountryCode,
  162. inverseMatch: geoip.InverseMatch,
  163. }
  164. if err := m.Init(geoip.Cidr); err != nil {
  165. return nil, err
  166. }
  167. if len(geoip.CountryCode) > 0 {
  168. c.matchers = append(c.matchers, m)
  169. }
  170. return m, nil
  171. }
  172. var globalGeoIPContainer GeoIPMatcherContainer