condition_geoip.go 3.7 KB

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