loyalsoldier 4 years ago
parent
commit
40f8e82204

+ 7 - 3
app/router/condition_geoip_test.go

@@ -13,18 +13,22 @@ import (
 	"github.com/v2fly/v2ray-core/v4/app/router"
 	"github.com/v2fly/v2ray-core/v4/common"
 	"github.com/v2fly/v2ray-core/v4/common/net"
-	"github.com/v2fly/v2ray-core/v4/common/platform"
 	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
 )
 
 func init() {
+	const (
+		geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
+		geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
+	)
+
 	wd, err := os.Getwd()
 	common.Must(err)
 
 	tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
-	os.Setenv("v2ray.location.asset", tempPath)
+	geoipPath := filepath.Join(tempPath, "geoip.dat")
 
-	geoipPath := platform.GetAssetLocation("geoip.dat")
+	os.Setenv("v2ray.location.asset", tempPath)
 
 	if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
 		common.Must(os.MkdirAll(tempPath, 0755))

+ 8 - 4
app/router/condition_test.go

@@ -14,7 +14,6 @@ import (
 	"github.com/v2fly/v2ray-core/v4/app/router"
 	"github.com/v2fly/v2ray-core/v4/common"
 	"github.com/v2fly/v2ray-core/v4/common/net"
-	"github.com/v2fly/v2ray-core/v4/common/platform"
 	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
 	"github.com/v2fly/v2ray-core/v4/common/protocol"
 	"github.com/v2fly/v2ray-core/v4/common/protocol/http"
@@ -24,14 +23,19 @@ import (
 )
 
 func init() {
+	const (
+		geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
+		geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
+	)
+
 	wd, err := os.Getwd()
 	common.Must(err)
 
 	tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
-	os.Setenv("v2ray.location.asset", tempPath)
+	geoipPath := filepath.Join(tempPath, "geoip.dat")
+	geositePath := filepath.Join(tempPath, "geosite.dat")
 
-	geoipPath := platform.GetAssetLocation("geoip.dat")
-	geositePath := platform.GetAssetLocation("geosite.dat")
+	os.Setenv("v2ray.location.asset", tempPath)
 
 	if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
 		common.Must(os.MkdirAll(tempPath, 0755))

+ 0 - 6
app/router/constant_test.go

@@ -1,6 +0,0 @@
-package router_test
-
-const (
-	geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
-	geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
-)

+ 0 - 6
infra/conf/conf_test.go

@@ -1,6 +0,0 @@
-package conf_test
-
-const (
-	geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
-	geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
-)

+ 8 - 4
infra/conf/dns_test.go

@@ -13,7 +13,6 @@ import (
 	"github.com/v2fly/v2ray-core/v4/app/dns"
 	"github.com/v2fly/v2ray-core/v4/common"
 	"github.com/v2fly/v2ray-core/v4/common/net"
-	"github.com/v2fly/v2ray-core/v4/common/platform"
 	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
 	"github.com/v2fly/v2ray-core/v4/infra/conf"
 
@@ -21,14 +20,19 @@ import (
 )
 
 func init() {
+	const (
+		geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
+		geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
+	)
+
 	wd, err := os.Getwd()
 	common.Must(err)
 
 	tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
-	os.Setenv("v2ray.location.asset", tempPath)
+	geoipPath := filepath.Join(tempPath, "geoip.dat")
+	geositePath := filepath.Join(tempPath, "geosite.dat")
 
-	geoipPath := platform.GetAssetLocation("geoip.dat")
-	geositePath := platform.GetAssetLocation("geosite.dat")
+	os.Setenv("v2ray.location.asset", tempPath)
 
 	if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
 		common.Must(os.MkdirAll(tempPath, 0755))

+ 9 - 9
infra/conf/geodata/memconservative/cache.go

@@ -4,9 +4,9 @@ import (
 	"io/ioutil"
 	"strings"
 
-	"github.com/golang/protobuf/proto"
 	"github.com/v2fly/v2ray-core/v4/app/router"
 	"github.com/v2fly/v2ray-core/v4/common/platform"
+	"google.golang.org/protobuf/proto"
 )
 
 type GeoIPCache map[string]*router.GeoIP
@@ -31,7 +31,7 @@ func (g GeoIPCache) Set(key string, value *router.GeoIP) {
 
 func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
 	asset := platform.GetAssetLocation(filename)
-	idx := strings.ToUpper(asset + "|" + code)
+	idx := strings.ToLower(asset + ":" + code)
 	if g.Has(idx) {
 		return g.Get(idx), nil
 	}
@@ -47,11 +47,11 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
 		return &geoip, nil
 
 	case errCodeNotFound:
-		return nil, newError(code, " not found in ", filename)
+		return nil, newError("country code ", code, " not found in ", filename)
 
 	case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
 		errInvalidGeodataFile, errInvalidGeodataVarintLength:
-		newError("failed to decode geodata file: ", filename, ". Fallback to the original ReadFile method.").AtWarning().WriteToLog()
+		newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
 		geoipBytes, err = ioutil.ReadFile(asset)
 		if err != nil {
 			return nil, err
@@ -71,7 +71,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
 		return nil, err
 	}
 
-	return nil, newError(code, " not found in ", filename)
+	return nil, newError("country code ", code, " not found in ", filename)
 }
 
 type GeoSiteCache map[string]*router.GeoSite
@@ -96,7 +96,7 @@ func (g GeoSiteCache) Set(key string, value *router.GeoSite) {
 
 func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error) {
 	asset := platform.GetAssetLocation(filename)
-	idx := strings.ToUpper(asset + "|" + code)
+	idx := strings.ToLower(asset + ":" + code)
 	if g.Has(idx) {
 		return g.Get(idx), nil
 	}
@@ -112,11 +112,11 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
 		return &geosite, nil
 
 	case errCodeNotFound:
-		return nil, newError(code, " not found in ", filename)
+		return nil, newError("list ", code, " not found in ", filename)
 
 	case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
 		errInvalidGeodataFile, errInvalidGeodataVarintLength:
-		newError("failed to decode geodata file: ", filename, ". Fallback to the original ReadFile method.").AtWarning().WriteToLog()
+		newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method")
 		geositeBytes, err = ioutil.ReadFile(asset)
 		if err != nil {
 			return nil, err
@@ -136,5 +136,5 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
 		return nil, err
 	}
 
-	return nil, newError(code, " not found in ", filename)
+	return nil, newError("list ", code, " not found in ", filename)
 }

+ 9 - 9
infra/conf/geodata/memconservative/decode_test.go

@@ -13,20 +13,20 @@ import (
 	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
 )
 
-const (
-	geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
-	geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
-)
-
 func init() {
+	const (
+		geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
+		geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
+	)
+
 	wd, err := os.Getwd()
 	common.Must(err)
 
-	tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
-	os.Setenv("v2ray.location.asset", tempPath)
+	tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
+	geoipPath := filepath.Join(tempPath, "geoip.dat")
+	geositePath := filepath.Join(tempPath, "geosite.dat")
 
-	geoipPath := platform.GetAssetLocation("geoip.dat")
-	geositePath := platform.GetAssetLocation("geosite.dat")
+	os.Setenv("v2ray.location.asset", tempPath)
 
 	if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
 		common.Must(os.MkdirAll(tempPath, 0755))

+ 8 - 13
infra/conf/rule/rule_test.go

@@ -18,27 +18,22 @@ import (
 	_ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
 )
 
-const (
-	geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
-)
-
 func init() {
+	const geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
+
 	wd, err := os.Getwd()
 	common.Must(err)
 
-	tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
+	tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp")
 	geoipPath := filepath.Join(tempPath, "geoip.dat")
 
 	os.Setenv("v2ray.location.asset", tempPath)
 
-	common.Must(os.MkdirAll(tempPath, 0755))
-
-	if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) {
-		if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
-			geoipBytes, err := common.FetchHTTPContent(geoipURL)
-			common.Must(err)
-			common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
-		}
+	if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
+		common.Must(os.MkdirAll(tempPath, 0755))
+		geoipBytes, err := common.FetchHTTPContent(geoipURL)
+		common.Must(err)
+		common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
 	}
 }