condition_geoip.go 3.7 KB

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