condition_geoip.go 4.0 KB

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