condition_geoip_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package router_test
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/xtls/xray-core/app/router"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/platform"
  11. "github.com/xtls/xray-core/common/platform/filesystem"
  12. "google.golang.org/protobuf/proto"
  13. )
  14. func getAssetPath(file string) (string, error) {
  15. path := platform.GetAssetLocation(file)
  16. _, err := os.Stat(path)
  17. if os.IsNotExist(err) {
  18. path := filepath.Join("..", "..", "resources", file)
  19. _, err := os.Stat(path)
  20. if os.IsNotExist(err) {
  21. return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
  22. }
  23. if err != nil {
  24. return "", fmt.Errorf("can't stat %s: %v", path, err)
  25. }
  26. return path, nil
  27. }
  28. if err != nil {
  29. return "", fmt.Errorf("can't stat %s: %v", path, err)
  30. }
  31. return path, nil
  32. }
  33. func TestGeoIPMatcherContainer(t *testing.T) {
  34. container := &router.GeoIPMatcherContainer{}
  35. m1, err := container.Add(&router.GeoIP{
  36. CountryCode: "CN",
  37. })
  38. common.Must(err)
  39. m2, err := container.Add(&router.GeoIP{
  40. CountryCode: "US",
  41. })
  42. common.Must(err)
  43. m3, err := container.Add(&router.GeoIP{
  44. CountryCode: "CN",
  45. })
  46. common.Must(err)
  47. if m1 != m3 {
  48. t.Error("expect same matcher for same geoip, but not")
  49. }
  50. if m1 == m2 {
  51. t.Error("expect different matcher for different geoip, but actually same")
  52. }
  53. }
  54. func TestGeoIPMatcher(t *testing.T) {
  55. cidrList := []*router.CIDR{
  56. {Ip: []byte{0, 0, 0, 0}, Prefix: 8},
  57. {Ip: []byte{10, 0, 0, 0}, Prefix: 8},
  58. {Ip: []byte{100, 64, 0, 0}, Prefix: 10},
  59. {Ip: []byte{127, 0, 0, 0}, Prefix: 8},
  60. {Ip: []byte{169, 254, 0, 0}, Prefix: 16},
  61. {Ip: []byte{172, 16, 0, 0}, Prefix: 12},
  62. {Ip: []byte{192, 0, 0, 0}, Prefix: 24},
  63. {Ip: []byte{192, 0, 2, 0}, Prefix: 24},
  64. {Ip: []byte{192, 168, 0, 0}, Prefix: 16},
  65. {Ip: []byte{192, 18, 0, 0}, Prefix: 15},
  66. {Ip: []byte{198, 51, 100, 0}, Prefix: 24},
  67. {Ip: []byte{203, 0, 113, 0}, Prefix: 24},
  68. {Ip: []byte{8, 8, 8, 8}, Prefix: 32},
  69. {Ip: []byte{91, 108, 4, 0}, Prefix: 16},
  70. }
  71. matcher := &router.GeoIPMatcher{}
  72. common.Must(matcher.Init(cidrList))
  73. testCases := []struct {
  74. Input string
  75. Output bool
  76. }{
  77. {
  78. Input: "192.168.1.1",
  79. Output: true,
  80. },
  81. {
  82. Input: "192.0.0.0",
  83. Output: true,
  84. },
  85. {
  86. Input: "192.0.1.0",
  87. Output: false,
  88. },
  89. {
  90. Input: "0.1.0.0",
  91. Output: true,
  92. },
  93. {
  94. Input: "1.0.0.1",
  95. Output: false,
  96. },
  97. {
  98. Input: "8.8.8.7",
  99. Output: false,
  100. },
  101. {
  102. Input: "8.8.8.8",
  103. Output: true,
  104. },
  105. {
  106. Input: "2001:cdba::3257:9652",
  107. Output: false,
  108. },
  109. {
  110. Input: "91.108.255.254",
  111. Output: true,
  112. },
  113. }
  114. for _, testCase := range testCases {
  115. ip := net.ParseAddress(testCase.Input).IP()
  116. actual := matcher.Match(ip)
  117. if actual != testCase.Output {
  118. t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual)
  119. }
  120. }
  121. }
  122. func TestGeoIPMatcherRegression(t *testing.T) {
  123. cidrList := []*router.CIDR{
  124. {Ip: []byte{98, 108, 20, 0}, Prefix: 22},
  125. {Ip: []byte{98, 108, 20, 0}, Prefix: 23},
  126. }
  127. matcher := &router.GeoIPMatcher{}
  128. common.Must(matcher.Init(cidrList))
  129. testCases := []struct {
  130. Input string
  131. Output bool
  132. }{
  133. {
  134. Input: "98.108.22.11",
  135. Output: true,
  136. },
  137. {
  138. Input: "98.108.25.0",
  139. Output: false,
  140. },
  141. }
  142. for _, testCase := range testCases {
  143. ip := net.ParseAddress(testCase.Input).IP()
  144. actual := matcher.Match(ip)
  145. if actual != testCase.Output {
  146. t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual)
  147. }
  148. }
  149. }
  150. func TestGeoIPReverseMatcher(t *testing.T) {
  151. cidrList := []*router.CIDR{
  152. {Ip: []byte{8, 8, 8, 8}, Prefix: 32},
  153. {Ip: []byte{91, 108, 4, 0}, Prefix: 16},
  154. }
  155. matcher := &router.GeoIPMatcher{}
  156. matcher.SetReverseMatch(true) // Reverse match
  157. common.Must(matcher.Init(cidrList))
  158. testCases := []struct {
  159. Input string
  160. Output bool
  161. }{
  162. {
  163. Input: "8.8.8.8",
  164. Output: false,
  165. },
  166. {
  167. Input: "2001:cdba::3257:9652",
  168. Output: true,
  169. },
  170. {
  171. Input: "91.108.255.254",
  172. Output: false,
  173. },
  174. }
  175. for _, testCase := range testCases {
  176. ip := net.ParseAddress(testCase.Input).IP()
  177. actual := matcher.Match(ip)
  178. if actual != testCase.Output {
  179. t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual)
  180. }
  181. }
  182. }
  183. func TestGeoIPMatcher4CN(t *testing.T) {
  184. ips, err := loadGeoIP("CN")
  185. common.Must(err)
  186. matcher := &router.GeoIPMatcher{}
  187. common.Must(matcher.Init(ips))
  188. if matcher.Match([]byte{8, 8, 8, 8}) {
  189. t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does")
  190. }
  191. }
  192. func TestGeoIPMatcher6US(t *testing.T) {
  193. ips, err := loadGeoIP("US")
  194. common.Must(err)
  195. matcher := &router.GeoIPMatcher{}
  196. common.Must(matcher.Init(ips))
  197. if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
  198. t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not")
  199. }
  200. }
  201. func loadGeoIP(country string) ([]*router.CIDR, error) {
  202. path, err := getAssetPath("geoip.dat")
  203. if err != nil {
  204. return nil, err
  205. }
  206. geoipBytes, err := filesystem.ReadFile(path)
  207. if err != nil {
  208. return nil, err
  209. }
  210. var geoipList router.GeoIPList
  211. if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
  212. return nil, err
  213. }
  214. for _, geoip := range geoipList.Entry {
  215. if geoip.CountryCode == country {
  216. return geoip.Cidr, nil
  217. }
  218. }
  219. panic("country not found: " + country)
  220. }
  221. func BenchmarkGeoIPMatcher4CN(b *testing.B) {
  222. ips, err := loadGeoIP("CN")
  223. common.Must(err)
  224. matcher := &router.GeoIPMatcher{}
  225. common.Must(matcher.Init(ips))
  226. b.ResetTimer()
  227. for i := 0; i < b.N; i++ {
  228. _ = matcher.Match([]byte{8, 8, 8, 8})
  229. }
  230. }
  231. func BenchmarkGeoIPMatcher6US(b *testing.B) {
  232. ips, err := loadGeoIP("US")
  233. common.Must(err)
  234. matcher := &router.GeoIPMatcher{}
  235. common.Must(matcher.Init(ips))
  236. b.ResetTimer()
  237. for i := 0; i < b.N; i++ {
  238. _ = matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP())
  239. }
  240. }