condition_geoip.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // +build !confonly
  2. package router
  3. import (
  4. "encoding/binary"
  5. "sort"
  6. "github.com/v2fly/v2ray-core/v4/common/net"
  7. )
  8. type ipv6 struct {
  9. a uint64
  10. b uint64
  11. }
  12. type GeoIPMatcher struct {
  13. countryCode string
  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) match4(ip uint32) bool {
  71. if len(m.ip4) == 0 {
  72. return false
  73. }
  74. if ip < m.ip4[0] {
  75. return false
  76. }
  77. size := uint32(len(m.ip4))
  78. l := uint32(0)
  79. r := size
  80. for l < r {
  81. x := ((l + r) >> 1)
  82. if ip < m.ip4[x] {
  83. r = x
  84. continue
  85. }
  86. nip := normalize4(ip, m.prefix4[x])
  87. if nip == m.ip4[x] {
  88. return true
  89. }
  90. l = x + 1
  91. }
  92. return l > 0 && normalize4(ip, m.prefix4[l-1]) == m.ip4[l-1]
  93. }
  94. func less6(a ipv6, b ipv6) bool {
  95. return a.a < b.a || (a.a == b.a && a.b < b.b)
  96. }
  97. func (m *GeoIPMatcher) match6(ip ipv6) bool {
  98. if len(m.ip6) == 0 {
  99. return false
  100. }
  101. if less6(ip, m.ip6[0]) {
  102. return false
  103. }
  104. size := uint32(len(m.ip6))
  105. l := uint32(0)
  106. r := size
  107. for l < r {
  108. x := (l + r) / 2
  109. if less6(ip, m.ip6[x]) {
  110. r = x
  111. continue
  112. }
  113. if normalize6(ip, m.prefix6[x]) == m.ip6[x] {
  114. return true
  115. }
  116. l = x + 1
  117. }
  118. return l > 0 && normalize6(ip, m.prefix6[l-1]) == m.ip6[l-1]
  119. }
  120. // Match returns true if the given ip is included by the GeoIP.
  121. func (m *GeoIPMatcher) Match(ip net.IP) bool {
  122. switch len(ip) {
  123. case 4:
  124. return m.match4(binary.BigEndian.Uint32(ip))
  125. case 16:
  126. return m.match6(ipv6{
  127. a: binary.BigEndian.Uint64(ip[0:8]),
  128. b: binary.BigEndian.Uint64(ip[8:16]),
  129. })
  130. default:
  131. return false
  132. }
  133. }
  134. // GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
  135. type GeoIPMatcherContainer struct {
  136. matchers []*GeoIPMatcher
  137. }
  138. // Add adds a new GeoIP set into the container.
  139. // If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
  140. func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
  141. if len(geoip.CountryCode) > 0 {
  142. for _, m := range c.matchers {
  143. if m.countryCode == geoip.CountryCode {
  144. return m, nil
  145. }
  146. }
  147. }
  148. m := &GeoIPMatcher{
  149. countryCode: geoip.CountryCode,
  150. }
  151. if err := m.Init(geoip.Cidr); err != nil {
  152. return nil, err
  153. }
  154. if len(geoip.CountryCode) > 0 {
  155. c.matchers = append(c.matchers, m)
  156. }
  157. return m, nil
  158. }
  159. var (
  160. globalGeoIPContainer GeoIPMatcherContainer
  161. )