@@ -5,12 +5,12 @@ import (
55 "path/filepath"
66 "testing"
77
8- "github.com/golang/protobuf/proto"
98 "github.com/xtls/xray-core/common"
109 . "github.com/xtls/xray-core/common/matcher/geoip"
1110 "github.com/xtls/xray-core/common/net"
1211 "github.com/xtls/xray-core/common/platform"
1312 "github.com/xtls/xray-core/common/platform/filesystem"
13+ "google.golang.org/protobuf/proto"
1414)
1515
1616func init () {
@@ -20,11 +20,31 @@ func init() {
2020 if _ , err := os .Stat (platform .GetAssetLocation ("geoip.dat" )); err != nil && os .IsNotExist (err ) {
2121 common .Must (filesystem .CopyFile (platform .GetAssetLocation ("geoip.dat" ), filepath .Join (wd , ".." , ".." , ".." , "resources" , "geoip.dat" )))
2222 }
23+ if _ , err := os .Stat (platform .GetAssetLocation ("geoiptestrouter.dat" )); err != nil && os .IsNotExist (err ) {
24+ common .Must (filesystem .CopyFile (platform .GetAssetLocation ("geoiptestrouter.dat" ), filepath .Join (wd , ".." , ".." , ".." , "resources" , "geoip.dat" )))
25+ }
2326 if _ , err := os .Stat (platform .GetAssetLocation ("geosite.dat" )); err != nil && os .IsNotExist (err ) {
2427 common .Must (filesystem .CopyFile (platform .GetAssetLocation ("geosite.dat" ), filepath .Join (wd , ".." , ".." , ".." , "resources" , "geosite.dat" )))
2528 }
2629}
2730
31+ func TestParseIPList (t * testing.T ) {
32+ ips := []string {
33+ "geoip:us" ,
34+ "geoip:cn" ,
35+ "geoip:!cn" ,
36+ "ext:geoiptestrouter.dat:!cn" ,
37+ "ext:geoiptestrouter.dat:ca" ,
38+ "ext-ip:geoiptestrouter.dat:!cn" ,
39+ "ext-ip:geoiptestrouter.dat:!ca" ,
40+ }
41+
42+ _ , err := ParseIPList (ips )
43+ if err != nil {
44+ t .Fatalf ("Failed to parse geoip list, got %s" , err )
45+ }
46+ }
47+
2848func TestGeoIPMatcherContainer (t * testing.T ) {
2949 container := & GeoIPMatcherContainer {}
3050
@@ -123,6 +143,42 @@ func TestGeoIPMatcher(t *testing.T) {
123143 }
124144}
125145
146+ func TestGeoIPReverseMatcher (t * testing.T ) {
147+ cidrList := CIDRList {
148+ {Ip : []byte {8 , 8 , 8 , 8 }, Prefix : 32 },
149+ {Ip : []byte {91 , 108 , 4 , 0 }, Prefix : 16 },
150+ }
151+ matcher := & GeoIPMatcher {}
152+ matcher .SetReverseMatch (true ) // Reverse match
153+ common .Must (matcher .Init (cidrList ))
154+
155+ testCases := []struct {
156+ Input string
157+ Output bool
158+ }{
159+ {
160+ Input : "8.8.8.8" ,
161+ Output : false ,
162+ },
163+ {
164+ Input : "2001:cdba::3257:9652" ,
165+ Output : true ,
166+ },
167+ {
168+ Input : "91.108.255.254" ,
169+ Output : false ,
170+ },
171+ }
172+
173+ for _ , testCase := range testCases {
174+ ip := net .ParseAddress (testCase .Input ).IP ()
175+ actual := matcher .Match (ip )
176+ if actual != testCase .Output {
177+ t .Error ("expect input" , testCase .Input , "to be" , testCase .Output , ", but actually" , actual )
178+ }
179+ }
180+ }
181+
126182func TestGeoIPMatcher4CN (t * testing.T ) {
127183 ips , err := loadGeoIP ("CN" )
128184 common .Must (err )
0 commit comments