condition_geoip_test.go 4.7 KB

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