condition_geoip.go 3.5 KB

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