condition_geoip_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package router_test
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. proto "github.com/golang/protobuf/proto"
  7. "v2ray.com/core/app/router"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/platform"
  11. "v2ray.com/core/common/platform/filesystem"
  12. )
  13. func init() {
  14. wd, err := os.Getwd()
  15. common.Must(err)
  16. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
  17. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
  18. }
  19. func TestGeoIPMatcherContainer(t *testing.T) {
  20. container := &router.GeoIPMatcherContainer{}
  21. m1, err := container.Add(&router.GeoIP{
  22. CountryCode: "CN",
  23. })
  24. common.Must(err)
  25. m2, err := container.Add(&router.GeoIP{
  26. CountryCode: "US",
  27. })
  28. common.Must(err)
  29. m3, err := container.Add(&router.GeoIP{
  30. CountryCode: "CN",
  31. })
  32. common.Must(err)
  33. if m1 != m3 {
  34. t.Error("expect same matcher for same geoip, but not")
  35. }
  36. if m1 == m2 {
  37. t.Error("expect different matcher for different geoip, but actually same")
  38. }
  39. }
  40. func TestGeoIPMatcher(t *testing.T) {
  41. cidrList := router.CIDRList{
  42. {Ip: []byte{0, 0, 0, 0}, Prefix: 8},
  43. {Ip: []byte{10, 0, 0, 0}, Prefix: 8},
  44. {Ip: []byte{100, 64, 0, 0}, Prefix: 10},
  45. {Ip: []byte{127, 0, 0, 0}, Prefix: 8},
  46. {Ip: []byte{169, 254, 0, 0}, Prefix: 16},
  47. {Ip: []byte{172, 16, 0, 0}, Prefix: 12},
  48. {Ip: []byte{192, 0, 0, 0}, Prefix: 24},
  49. {Ip: []byte{192, 0, 2, 0}, Prefix: 24},
  50. {Ip: []byte{192, 168, 0, 0}, Prefix: 16},
  51. {Ip: []byte{192, 18, 0, 0}, Prefix: 15},
  52. {Ip: []byte{198, 51, 100, 0}, Prefix: 24},
  53. {Ip: []byte{203, 0, 113, 0}, Prefix: 24},
  54. {Ip: []byte{8, 8, 8, 8}, Prefix: 32},
  55. {Ip: []byte{91, 108, 4, 0}, Prefix: 16},
  56. }
  57. matcher := &router.GeoIPMatcher{}
  58. common.Must(matcher.Init(cidrList))
  59. testCases := []struct {
  60. Input string
  61. Output bool
  62. }{
  63. {
  64. Input: "192.168.1.1",
  65. Output: true,
  66. },
  67. {
  68. Input: "192.0.0.0",
  69. Output: true,
  70. },
  71. {
  72. Input: "192.0.1.0",
  73. Output: false,
  74. }, {
  75. Input: "0.1.0.0",
  76. Output: true,
  77. },
  78. {
  79. Input: "1.0.0.1",
  80. Output: false,
  81. },
  82. {
  83. Input: "8.8.8.7",
  84. Output: false,
  85. },
  86. {
  87. Input: "8.8.8.8",
  88. Output: true,
  89. },
  90. {
  91. Input: "2001:cdba::3257:9652",
  92. Output: false,
  93. },
  94. {
  95. Input: "91.108.255.254",
  96. Output: true,
  97. },
  98. }
  99. for _, testCase := range testCases {
  100. ip := net.ParseAddress(testCase.Input).IP()
  101. actual := matcher.Match(ip)
  102. if actual != testCase.Output {
  103. t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual)
  104. }
  105. }
  106. }
  107. func TestGeoIPMatcher4CN(t *testing.T) {
  108. ips, err := loadGeoIP("CN")
  109. common.Must(err)
  110. matcher := &router.GeoIPMatcher{}
  111. common.Must(matcher.Init(ips))
  112. if matcher.Match([]byte{8, 8, 8, 8}) {
  113. t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does")
  114. }
  115. }
  116. func TestGeoIPMatcher6US(t *testing.T) {
  117. ips, err := loadGeoIP("US")
  118. common.Must(err)
  119. matcher := &router.GeoIPMatcher{}
  120. common.Must(matcher.Init(ips))
  121. if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
  122. t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not")
  123. }
  124. }
  125. func loadGeoIP(country string) ([]*router.CIDR, error) {
  126. geoipBytes, err := filesystem.ReadAsset("geoip.dat")
  127. if err != nil {
  128. return nil, err
  129. }
  130. var geoipList router.GeoIPList
  131. if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
  132. return nil, err
  133. }
  134. for _, geoip := range geoipList.Entry {
  135. if geoip.CountryCode == country {
  136. return geoip.Cidr, nil
  137. }
  138. }
  139. panic("country not found: " + country)
  140. }
  141. func BenchmarkGeoIPMatcher4CN(b *testing.B) {
  142. ips, err := loadGeoIP("CN")
  143. common.Must(err)
  144. matcher := &router.GeoIPMatcher{}
  145. common.Must(matcher.Init(ips))
  146. b.ResetTimer()
  147. for i := 0; i < b.N; i++ {
  148. _ = matcher.Match([]byte{8, 8, 8, 8})
  149. }
  150. }
  151. func BenchmarkGeoIPMatcher6US(b *testing.B) {
  152. ips, err := loadGeoIP("US")
  153. common.Must(err)
  154. matcher := &router.GeoIPMatcher{}
  155. common.Must(matcher.Init(ips))
  156. b.ResetTimer()
  157. for i := 0; i < b.N; i++ {
  158. _ = matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP())
  159. }
  160. }