2018-11-01 04:10:41 -04:00
|
|
|
package router_test
|
|
|
|
|
|
|
|
import (
|
2021-04-06 15:08:03 -04:00
|
|
|
"errors"
|
2021-04-11 22:01:30 -04:00
|
|
|
"io/fs"
|
2018-11-01 04:10:41 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-05-03 16:03:45 -04:00
|
|
|
"strings"
|
2018-11-01 04:10:41 -04:00
|
|
|
"testing"
|
|
|
|
|
2021-05-03 16:03:45 -04:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
|
2018-11-01 04:10:41 -04:00
|
|
|
)
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
func init() {
|
2021-05-04 19:28:09 -04:00
|
|
|
const geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
|
2021-05-04 09:51:38 -04:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
|
2021-05-04 09:51:38 -04:00
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
2021-05-04 04:30:23 -04:00
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
2021-05-04 14:13:52 -04:00
|
|
|
|
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 14:13:52 -04:00
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
2020-09-03 04:00:24 -04:00
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2018-11-04 13:14:17 -05:00
|
|
|
func TestGeoIPMatcherContainer(t *testing.T) {
|
|
|
|
container := &router.GeoIPMatcherContainer{}
|
|
|
|
|
2021-09-07 08:43:28 -04:00
|
|
|
m1, err := container.Add(&routercommon.GeoIP{
|
2018-11-04 13:14:17 -05:00
|
|
|
CountryCode: "CN",
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-09-07 08:43:28 -04:00
|
|
|
m2, err := container.Add(&routercommon.GeoIP{
|
2018-11-04 13:14:17 -05:00
|
|
|
CountryCode: "US",
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-09-07 08:43:28 -04:00
|
|
|
m3, err := container.Add(&routercommon.GeoIP{
|
2018-11-04 13:14:17 -05:00
|
|
|
CountryCode: "CN",
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if m1 != m3 {
|
|
|
|
t.Error("expect same matcher for same geoip, but not")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1 == m2 {
|
|
|
|
t.Error("expect different matcher for different geoip, but actually same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:10:41 -04:00
|
|
|
func TestGeoIPMatcher(t *testing.T) {
|
2021-09-27 16:50:18 -04:00
|
|
|
cidrList := []*routercommon.CIDR{
|
2018-11-01 04:10:41 -04:00
|
|
|
{Ip: []byte{0, 0, 0, 0}, Prefix: 8},
|
|
|
|
{Ip: []byte{10, 0, 0, 0}, Prefix: 8},
|
|
|
|
{Ip: []byte{100, 64, 0, 0}, Prefix: 10},
|
|
|
|
{Ip: []byte{127, 0, 0, 0}, Prefix: 8},
|
|
|
|
{Ip: []byte{169, 254, 0, 0}, Prefix: 16},
|
|
|
|
{Ip: []byte{172, 16, 0, 0}, Prefix: 12},
|
|
|
|
{Ip: []byte{192, 0, 0, 0}, Prefix: 24},
|
|
|
|
{Ip: []byte{192, 0, 2, 0}, Prefix: 24},
|
|
|
|
{Ip: []byte{192, 168, 0, 0}, Prefix: 16},
|
|
|
|
{Ip: []byte{192, 18, 0, 0}, Prefix: 15},
|
|
|
|
{Ip: []byte{198, 51, 100, 0}, Prefix: 24},
|
|
|
|
{Ip: []byte{203, 0, 113, 0}, Prefix: 24},
|
|
|
|
{Ip: []byte{8, 8, 8, 8}, Prefix: 32},
|
|
|
|
{Ip: []byte{91, 108, 4, 0}, Prefix: 16},
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher := &router.GeoIPMatcher{}
|
|
|
|
common.Must(matcher.Init(cidrList))
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Input string
|
|
|
|
Output bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "192.168.1.1",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "192.0.0.0",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "192.0.1.0",
|
|
|
|
Output: false,
|
2021-05-19 17:28:52 -04:00
|
|
|
},
|
|
|
|
{
|
2018-11-01 04:10:41 -04:00
|
|
|
Input: "0.1.0.0",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "1.0.0.1",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "8.8.8.7",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "8.8.8.8",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "2001:cdba::3257:9652",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "91.108.255.254",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:29:12 -04:00
|
|
|
func TestGeoIPReverseMatcher(t *testing.T) {
|
2021-09-27 16:50:18 -04:00
|
|
|
cidrList := []*routercommon.CIDR{
|
2021-04-06 00:29:12 -04:00
|
|
|
{Ip: []byte{8, 8, 8, 8}, Prefix: 32},
|
|
|
|
{Ip: []byte{91, 108, 4, 0}, Prefix: 16},
|
|
|
|
}
|
|
|
|
matcher := &router.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:10:41 -04:00
|
|
|
func TestGeoIPMatcher4CN(t *testing.T) {
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("CN")
|
2018-11-01 04:10:41 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
matcher := &router.GeoIPMatcher{}
|
|
|
|
common.Must(matcher.Init(ips))
|
|
|
|
|
|
|
|
if matcher.Match([]byte{8, 8, 8, 8}) {
|
|
|
|
t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGeoIPMatcher6US(t *testing.T) {
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("US")
|
2018-11-01 04:10:41 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
matcher := &router.GeoIPMatcher{}
|
|
|
|
common.Must(matcher.Init(ips))
|
|
|
|
|
|
|
|
if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
|
|
|
|
t.Error("expect US geoip contain 2001:4860:4860::8888, but actually not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 08:43:28 -04:00
|
|
|
func loadGeoIP(country string) ([]*routercommon.CIDR, error) {
|
2021-05-03 16:03:45 -04:00
|
|
|
geoipBytes, err := filesystem.ReadAsset("geoip.dat")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-07 08:43:28 -04:00
|
|
|
var geoipList routercommon.GeoIPList
|
2021-05-03 16:03:45 -04:00
|
|
|
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, geoip := range geoipList.Entry {
|
|
|
|
if strings.EqualFold(geoip.CountryCode, country) {
|
|
|
|
return geoip.Cidr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("country not found: " + country)
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:10:41 -04:00
|
|
|
func BenchmarkGeoIPMatcher4CN(b *testing.B) {
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("CN")
|
2018-11-01 04:10:41 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
matcher := &router.GeoIPMatcher{}
|
|
|
|
common.Must(matcher.Init(ips))
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = matcher.Match([]byte{8, 8, 8, 8})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeoIPMatcher6US(b *testing.B) {
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("US")
|
2018-11-01 04:10:41 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
matcher := &router.GeoIPMatcher{}
|
|
|
|
common.Must(matcher.Init(ips))
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP())
|
|
|
|
}
|
|
|
|
}
|