0
mirror of https://github.com/XTLS/Xray-core.git synced 2025-06-19 22:58:03 +03:00

Feat: add reverse match for GeoIP

This commit is contained in:
秋のかえで
2021-04-08 13:07:53 +08:00
parent 8a7cd65fc2
commit 68201a8898
5 changed files with 106 additions and 32 deletions

View File

@ -135,6 +135,42 @@ func TestGeoIPMatcher4CN(t *testing.T) {
}
}
func TestGeoIPReverseMatcher(t *testing.T) {
cidrList := CIDRList{
{Ip: []byte{8, 8, 8, 8}, Prefix: 32},
{Ip: []byte{91, 108, 4, 0}, Prefix: 16},
}
matcher := &GeoIPMatcher{}
matcher.SetReverseMatch(true) // Reverse match
common.Must(matcher.Init(cidrList))
testCases := []struct {
Input string
Output bool
}{
{
Input: "8.8.8.8",
Output: false,
},
{
Input: "2001:cdba::3257:9652",
Output: true,
},
{
Input: "91.108.255.254",
Output: false,
},
}
for _, testCase := range testCases {
ip := net.ParseAddress(testCase.Input).IP()
actual := matcher.Match(ip)
if actual != testCase.Output {
t.Error("expect input", testCase.Input, "to be", testCase.Output, ", but actually", actual)
}
}
}
func TestGeoIPMatcher6US(t *testing.T) {
ips, err := loadGeoIP("US")
common.Must(err)