condition_geoip_test.go 3.9 KB

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