diff --git a/app/router/condition_geoip_test.go b/app/router/condition_geoip_test.go index 00fe21fb7..001013d04 100644 --- a/app/router/condition_geoip_test.go +++ b/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)) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index de346e07b..3a9ded4ba 100644 --- a/app/router/condition_test.go +++ b/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)) diff --git a/app/router/constant_test.go b/app/router/constant_test.go deleted file mode 100644 index fb95ed141..000000000 --- a/app/router/constant_test.go +++ /dev/null @@ -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" -) diff --git a/infra/conf/conf_test.go b/infra/conf/conf_test.go deleted file mode 100644 index 4c939029c..000000000 --- a/infra/conf/conf_test.go +++ /dev/null @@ -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" -) diff --git a/infra/conf/dns_test.go b/infra/conf/dns_test.go index 87088d29e..2125bb977 100644 --- a/infra/conf/dns_test.go +++ b/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)) diff --git a/infra/conf/geodata/memconservative/cache.go b/infra/conf/geodata/memconservative/cache.go index e62ecd074..116a185f5 100644 --- a/infra/conf/geodata/memconservative/cache.go +++ b/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) } diff --git a/infra/conf/geodata/memconservative/decode_test.go b/infra/conf/geodata/memconservative/decode_test.go index 189027c02..7f7031a40 100644 --- a/infra/conf/geodata/memconservative/decode_test.go +++ b/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)) diff --git a/infra/conf/rule/rule_test.go b/infra/conf/rule/rule_test.go index 12248c9b1..e6b45bb23 100644 --- a/infra/conf/rule/rule_test.go +++ b/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)) } }