condition_geoip_test.go 5.5 KB

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