condition_geoip.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. reverseMatch bool
  15. ip4 []uint32
  16. prefix4 []uint8
  17. ip6 []ipv6
  18. prefix6 []uint8
  19. }
  20. func normalize4(ip uint32, prefix uint8) uint32 {
  21. return (ip >> (32 - prefix)) << (32 - prefix)
  22. }
  23. func normalize6(ip ipv6, prefix uint8) ipv6 {
  24. if prefix <= 64 {
  25. ip.a = (ip.a >> (64 - prefix)) << (64 - prefix)
  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(binary.BigEndian.Uint32(ip), prefix))
  58. m.prefix4 = append(m.prefix4, prefix)
  59. case 16:
  60. ip6 := ipv6{
  61. a: binary.BigEndian.Uint64(ip[0:8]),
  62. b: binary.BigEndian.Uint64(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) SetReverseMatch(isReverseMatch bool) {
  72. m.reverseMatch = isReverseMatch
  73. }
  74. func (m *GeoIPMatcher) match4(ip uint32) bool {
  75. if len(m.ip4) == 0 {
  76. return false
  77. }
  78. if ip < m.ip4[0] {
  79. return false
  80. }
  81. size := uint32(len(m.ip4))
  82. l := uint32(0)
  83. r := size
  84. for l < r {
  85. x := ((l + r) >> 1)
  86. if ip < m.ip4[x] {
  87. r = x
  88. continue
  89. }
  90. nip := normalize4(ip, m.prefix4[x])
  91. if nip == m.ip4[x] {
  92. return true
  93. }
  94. l = x + 1
  95. }
  96. return l > 0 && normalize4(ip, m.prefix4[l-1]) == m.ip4[l-1]
  97. }
  98. func less6(a ipv6, b ipv6) bool {
  99. return a.a < b.a || (a.a == b.a && a.b < b.b)
  100. }
  101. func (m *GeoIPMatcher) match6(ip ipv6) bool {
  102. if len(m.ip6) == 0 {
  103. return false
  104. }
  105. if less6(ip, m.ip6[0]) {
  106. return false
  107. }
  108. size := uint32(len(m.ip6))
  109. l := uint32(0)
  110. r := size
  111. for l < r {
  112. x := (l + r) / 2
  113. if less6(ip, m.ip6[x]) {
  114. r = x
  115. continue
  116. }
  117. if normalize6(ip, m.prefix6[x]) == m.ip6[x] {
  118. return true
  119. }
  120. l = x + 1
  121. }
  122. return l > 0 && normalize6(ip, m.prefix6[l-1]) == m.ip6[l-1]
  123. }
  124. // Match returns true if the given ip is included by the GeoIP.
  125. func (m *GeoIPMatcher) Match(ip net.IP) bool {
  126. switch len(ip) {
  127. case 4:
  128. if m.reverseMatch {
  129. return !m.match4(binary.BigEndian.Uint32(ip))
  130. }
  131. return m.match4(binary.BigEndian.Uint32(ip))
  132. case 16:
  133. if m.reverseMatch {
  134. return !m.match6(ipv6{
  135. a: binary.BigEndian.Uint64(ip[0:8]),
  136. b: binary.BigEndian.Uint64(ip[8:16]),
  137. })
  138. }
  139. return m.match6(ipv6{
  140. a: binary.BigEndian.Uint64(ip[0:8]),
  141. b: binary.BigEndian.Uint64(ip[8:16]),
  142. })
  143. default:
  144. return false
  145. }
  146. }
  147. // GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
  148. type GeoIPMatcherContainer struct {
  149. matchers []*GeoIPMatcher
  150. }
  151. // Add adds a new GeoIP set into the container.
  152. // If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
  153. func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
  154. if len(geoip.CountryCode) > 0 {
  155. for _, m := range c.matchers {
  156. if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch {
  157. return m, nil
  158. }
  159. }
  160. }
  161. m := &GeoIPMatcher{
  162. countryCode: geoip.CountryCode,
  163. reverseMatch: geoip.ReverseMatch,
  164. }
  165. if err := m.Init(geoip.Cidr); err != nil {
  166. return nil, err
  167. }
  168. if len(geoip.CountryCode) > 0 {
  169. c.matchers = append(c.matchers, m)
  170. }
  171. return m, nil
  172. }
  173. var globalGeoIPContainer GeoIPMatcherContainer