|
|
@@ -1,40 +1,17 @@
|
|
|
package router_test
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "runtime"
|
|
|
"testing"
|
|
|
|
|
|
"github.com/xtls/xray-core/app/router"
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
- "github.com/xtls/xray-core/common/platform"
|
|
|
- "github.com/xtls/xray-core/common/platform/filesystem"
|
|
|
- "google.golang.org/protobuf/proto"
|
|
|
+ "github.com/xtls/xray-core/infra/conf"
|
|
|
)
|
|
|
|
|
|
-func getAssetPath(file string) (string, error) {
|
|
|
- path := platform.GetAssetLocation(file)
|
|
|
- _, err := os.Stat(path)
|
|
|
- if os.IsNotExist(err) {
|
|
|
- path := filepath.Join("..", "..", "resources", file)
|
|
|
- _, err := os.Stat(path)
|
|
|
- if os.IsNotExist(err) {
|
|
|
- return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return "", fmt.Errorf("can't stat %s: %v", path, err)
|
|
|
- }
|
|
|
- return path, nil
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return "", fmt.Errorf("can't stat %s: %v", path, err)
|
|
|
- }
|
|
|
-
|
|
|
- return path, nil
|
|
|
-}
|
|
|
-
|
|
|
func TestGeoIPMatcher(t *testing.T) {
|
|
|
cidrList := []*router.CIDR{
|
|
|
{Ip: []byte{0, 0, 0, 0}, Prefix: 8},
|
|
|
@@ -182,12 +159,11 @@ func TestGeoIPReverseMatcher(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestGeoIPMatcher4CN(t *testing.T) {
|
|
|
- ips, err := loadGeoIP("CN")
|
|
|
+ geo := "geoip:cn"
|
|
|
+ geoip, err := loadGeoIP(geo)
|
|
|
common.Must(err)
|
|
|
|
|
|
- matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
|
|
|
- Cidr: ips,
|
|
|
- })
|
|
|
+ matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
|
|
|
common.Must(err)
|
|
|
|
|
|
if matcher.Match([]byte{8, 8, 8, 8}) {
|
|
|
@@ -196,12 +172,11 @@ func TestGeoIPMatcher4CN(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestGeoIPMatcher6US(t *testing.T) {
|
|
|
- ips, err := loadGeoIP("US")
|
|
|
+ geo := "geoip:us"
|
|
|
+ geoip, err := loadGeoIP(geo)
|
|
|
common.Must(err)
|
|
|
|
|
|
- matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
|
|
|
- Cidr: ips,
|
|
|
- })
|
|
|
+ matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
|
|
|
common.Must(err)
|
|
|
|
|
|
if !matcher.Match(net.ParseAddress("2001:4860:4860::8888").IP()) {
|
|
|
@@ -209,37 +184,34 @@ func TestGeoIPMatcher6US(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func loadGeoIP(country string) ([]*router.CIDR, error) {
|
|
|
- path, err := getAssetPath("geoip.dat")
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- geoipBytes, err := filesystem.ReadFile(path)
|
|
|
+func loadGeoIP(geo string) (*router.GeoIP, error) {
|
|
|
+ os.Setenv("XRAY_LOCATION_ASSET", filepath.Join("..", "..", "resources"))
|
|
|
+
|
|
|
+ geoip, err := conf.ToCidrList([]string{geo})
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- var geoipList router.GeoIPList
|
|
|
- if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
|
|
|
- return nil, err
|
|
|
+ if runtime.GOOS != "windows" && runtime.GOOS != "wasm" {
|
|
|
+ geoip, err = router.GetGeoIPList(geoip)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- for _, geoip := range geoipList.Entry {
|
|
|
- if geoip.CountryCode == country {
|
|
|
- return geoip.Cidr, nil
|
|
|
- }
|
|
|
+ if len(geoip) == 0 {
|
|
|
+ panic("country not found: " + geo)
|
|
|
}
|
|
|
|
|
|
- panic("country not found: " + country)
|
|
|
+ return geoip[0], nil
|
|
|
}
|
|
|
|
|
|
func BenchmarkGeoIPMatcher4CN(b *testing.B) {
|
|
|
- ips, err := loadGeoIP("CN")
|
|
|
+ geo := "geoip:cn"
|
|
|
+ geoip, err := loadGeoIP(geo)
|
|
|
common.Must(err)
|
|
|
|
|
|
- matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
|
|
|
- Cidr: ips,
|
|
|
- })
|
|
|
+ matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
|
|
|
common.Must(err)
|
|
|
|
|
|
b.ResetTimer()
|
|
|
@@ -250,12 +222,11 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
|
|
|
}
|
|
|
|
|
|
func BenchmarkGeoIPMatcher6US(b *testing.B) {
|
|
|
- ips, err := loadGeoIP("US")
|
|
|
+ geo := "geoip:us"
|
|
|
+ geoip, err := loadGeoIP(geo)
|
|
|
common.Must(err)
|
|
|
|
|
|
- matcher, err := router.BuildOptimizedGeoIPMatcher(&router.GeoIP{
|
|
|
- Cidr: ips,
|
|
|
- })
|
|
|
+ matcher, err := router.BuildOptimizedGeoIPMatcher(geoip)
|
|
|
common.Must(err)
|
|
|
|
|
|
b.ResetTimer()
|