11package router_test
22
33import (
4+ "fmt"
45 "os"
56 "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/infra/conf"
12+ "github.com/xtls/xray-core/common/platform"
13+ "github.com/xtls/xray-core/common/platform/filesystem"
14+ "google.golang.org/protobuf/proto"
1315)
1416
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+
1538func TestGeoIPMatcher (t * testing.T ) {
1639 cidrList := []* router.CIDR {
1740 {Ip : []byte {0 , 0 , 0 , 0 }, Prefix : 8 },
@@ -159,11 +182,12 @@ func TestGeoIPReverseMatcher(t *testing.T) {
159182}
160183
161184func TestGeoIPMatcher4CN (t * testing.T ) {
162- geo := "geoip:cn"
163- geoip , err := loadGeoIP (geo )
185+ ips , err := loadGeoIP ("CN" )
164186 common .Must (err )
165187
166- matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
188+ matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
189+ Cidr : ips ,
190+ })
167191 common .Must (err )
168192
169193 if matcher .Match ([]byte {8 , 8 , 8 , 8 }) {
@@ -172,46 +196,50 @@ func TestGeoIPMatcher4CN(t *testing.T) {
172196}
173197
174198func TestGeoIPMatcher6US (t * testing.T ) {
175- geo := "geoip:us"
176- geoip , err := loadGeoIP (geo )
199+ ips , err := loadGeoIP ("US" )
177200 common .Must (err )
178201
179- matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
202+ matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
203+ Cidr : ips ,
204+ })
180205 common .Must (err )
181206
182207 if ! matcher .Match (net .ParseAddress ("2001:4860:4860::8888" ).IP ()) {
183208 t .Error ("expect US geoip contain 2001:4860:4860::8888, but actually not" )
184209 }
185210}
186211
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 })
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 )
191218 if err != nil {
192219 return nil , err
193220 }
194221
195- if runtime .GOOS != "windows" && runtime .GOOS != "wasm" {
196- geoip , err = router .GetGeoIPList (geoip )
197- if err != nil {
198- return nil , err
199- }
222+ var geoipList router.GeoIPList
223+ if err := proto .Unmarshal (geoipBytes , & geoipList ); err != nil {
224+ return nil , err
200225 }
201226
202- if len (geoip ) == 0 {
203- panic ("country not found: " + geo )
227+ for _ , geoip := range geoipList .Entry {
228+ if geoip .CountryCode == country {
229+ return geoip .Cidr , nil
230+ }
204231 }
205232
206- return geoip [ 0 ], nil
233+ panic ( "country not found: " + country )
207234}
208235
209236func BenchmarkGeoIPMatcher4CN (b * testing.B ) {
210- geo := "geoip:cn"
211- geoip , err := loadGeoIP (geo )
237+ ips , err := loadGeoIP ("CN" )
212238 common .Must (err )
213239
214- matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
240+ matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
241+ Cidr : ips ,
242+ })
215243 common .Must (err )
216244
217245 b .ResetTimer ()
@@ -222,11 +250,12 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
222250}
223251
224252func BenchmarkGeoIPMatcher6US (b * testing.B ) {
225- geo := "geoip:us"
226- geoip , err := loadGeoIP (geo )
253+ ips , err := loadGeoIP ("US" )
227254 common .Must (err )
228255
229- matcher , err := router .BuildOptimizedGeoIPMatcher (geoip )
256+ matcher , err := router .BuildOptimizedGeoIPMatcher (& router.GeoIP {
257+ Cidr : ips ,
258+ })
230259 common .Must (err )
231260
232261 b .ResetTimer ()
0 commit comments