11package router_test
22
33import (
4- "fmt"
54 "os"
65 "path/filepath"
6+ "runtime"
77 "testing"
88
99 "github.com/xtls/xray-core/app/router"
1010 "github.com/xtls/xray-core/common"
1111 "github.com/xtls/xray-core/common/net"
12- "github.com/xtls/xray-core/common/platform"
13- "github.com/xtls/xray-core/common/platform/filesystem"
14- "google.golang.org/protobuf/proto"
12+ "github.com/xtls/xray-core/infra/conf"
1513)
1614
17- func getAssetPath (file string ) (string , error ) {
18- path := platform .GetAssetLocation (file )
19- _ , err := os .Stat (path )
20- if os .IsNotExist (err ) {
21- path := filepath .Join (".." , ".." , "resources" , file )
22- _ , err := os .Stat (path )
23- if os .IsNotExist (err ) {
24- return "" , fmt .Errorf ("can't find %s in standard asset locations or {project_root}/resources" , file )
25- }
26- if err != nil {
27- return "" , fmt .Errorf ("can't stat %s: %v" , path , err )
28- }
29- return path , nil
30- }
31- if err != nil {
32- return "" , fmt .Errorf ("can't stat %s: %v" , path , err )
33- }
34-
35- return path , nil
36- }
37-
3815func TestGeoIPMatcher (t * testing.T ) {
3916 cidrList := []* router.CIDR {
4017 {Ip : []byte {0 , 0 , 0 , 0 }, Prefix : 8 },
@@ -182,12 +159,11 @@ func TestGeoIPReverseMatcher(t *testing.T) {
182159}
183160
184161func TestGeoIPMatcher4CN (t * testing.T ) {
185- ips , err := loadGeoIP ("CN" )
162+ geo := "geoip:cn"
163+ geoip , err := loadGeoIP (geo )
186164 common .Must (err )
187165
188- matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
189- Cidr : ips ,
190- })
166+ matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
191167 common .Must (err )
192168
193169 if matcher .Match ([]byte {8 , 8 , 8 , 8 }) {
@@ -196,50 +172,46 @@ func TestGeoIPMatcher4CN(t *testing.T) {
196172}
197173
198174func TestGeoIPMatcher6US (t * testing.T ) {
199- ips , err := loadGeoIP ("US" )
175+ geo := "geoip:us"
176+ geoip , err := loadGeoIP (geo )
200177 common .Must (err )
201178
202- matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
203- Cidr : ips ,
204- })
179+ matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
205180 common .Must (err )
206181
207182 if ! matcher .Match (net .ParseAddress ("2001:4860:4860::8888" ).IP ()) {
208183 t .Error ("expect US geoip contain 2001:4860:4860::8888, but actually not" )
209184 }
210185}
211186
212- func loadGeoIP (country string ) ([]* router.CIDR , error ) {
213- path , err := getAssetPath ("geoip.dat" )
214- if err != nil {
215- return nil , err
216- }
217- geoipBytes , err := filesystem .ReadFile (path )
187+ func loadGeoIP (geo string ) (* router.GeoIP , error ) {
188+ os .Setenv ("XRAY_LOCATION_ASSET" , filepath .Join (".." , ".." , "resources" ))
189+
190+ geoip , err := conf .ToCidrList ([]string {geo })
218191 if err != nil {
219192 return nil , err
220193 }
221194
222- var geoipList router.GeoIPList
223- if err := proto .Unmarshal (geoipBytes , & geoipList ); err != nil {
224- return nil , err
195+ if runtime .GOOS != "windows" && runtime .GOOS != "wasm" {
196+ geoip , err = router .GetGeoIPList (geoip )
197+ if err != nil {
198+ return nil , err
199+ }
225200 }
226201
227- for _ , geoip := range geoipList .Entry {
228- if geoip .CountryCode == country {
229- return geoip .Cidr , nil
230- }
202+ if len (geoip ) == 0 {
203+ panic ("country not found: " + geo )
231204 }
232205
233- panic ( "country not found: " + country )
206+ return geoip [ 0 ], nil
234207}
235208
236209func BenchmarkGeoIPMatcher4CN (b * testing.B ) {
237- ips , err := loadGeoIP ("CN" )
210+ geo := "geoip:cn"
211+ geoip , err := loadGeoIP (geo )
238212 common .Must (err )
239213
240- matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
241- Cidr : ips ,
242- })
214+ matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
243215 common .Must (err )
244216
245217 b .ResetTimer ()
@@ -250,12 +222,11 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
250222}
251223
252224func BenchmarkGeoIPMatcher6US (b * testing.B ) {
253- ips , err := loadGeoIP ("US" )
225+ geo := "geoip:us"
226+ geoip , err := loadGeoIP (geo )
254227 common .Must (err )
255228
256- matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
257- Cidr : ips ,
258- })
229+ matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
259230 common .Must (err )
260231
261232 b .ResetTimer ()
0 commit comments