condition_geoip_test.go 3.9 KB

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