diff --git a/common/platform/others.go b/common/platform/others.go index a2f92c714..2d7d0ceb8 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -33,6 +33,7 @@ func GetAssetLocation(file string) string { filepath.Join("/usr/local/share/v2ray/", file), filepath.Join("/usr/share/v2ray/", file), filepath.Join("/opt/share/v2ray/", file), + file, } { if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) { continue diff --git a/common/platform/platform_test.go b/common/platform/platform_test.go index b7dbab351..9d7a3f02e 100644 --- a/common/platform/platform_test.go +++ b/common/platform/platform_test.go @@ -9,9 +9,30 @@ import ( "testing" "github.com/v2fly/v2ray-core/v4/common" - . "github.com/v2fly/v2ray-core/v4/common/platform" + "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") + geoipPath := filepath.Join(tempPath, "geoip.dat") + + 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)) + } +} + func TestNormalizeEnvName(t *testing.T) { cases := []struct { input string @@ -31,14 +52,14 @@ func TestNormalizeEnvName(t *testing.T) { }, } for _, test := range cases { - if v := NormalizeEnvName(test.input); v != test.output { + if v := platform.NormalizeEnvName(test.input); v != test.output { t.Error("unexpected output: ", v, " want ", test.output) } } } func TestEnvFlag(t *testing.T) { - if v := (EnvFlag{ + if v := (platform.EnvFlag{ Name: "xxxxx.y", }.GetValueAsInt(10)); v != 10 { t.Error("env value: ", v) @@ -73,21 +94,30 @@ func TestWrongErrorCheckOnOSStat(t *testing.T) { } func TestGetAssetLocation(t *testing.T) { + // Test for external geo files + wd, err := os.Getwd() + common.Must(err) + tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + asset := platform.GetAssetLocation(geoipPath) + if _, err := os.Stat(asset); err != nil && errors.Is(err, fs.ErrNotExist) { + t.Error("cannot find external geo file:", asset) + } + exec, err := os.Executable() common.Must(err) - - loc := GetAssetLocation("t") + loc := platform.GetAssetLocation("t") if filepath.Dir(loc) != filepath.Dir(exec) { t.Error("asset dir: ", loc, " not in ", exec) } os.Setenv("v2ray.location.asset", "/v2ray") if runtime.GOOS == "windows" { - if v := GetAssetLocation("t"); v != "\\v2ray\\t" { + if v := platform.GetAssetLocation("t"); v != "\\v2ray\\t" { t.Error("asset loc: ", v) } } else { - if v := GetAssetLocation("t"); v != "/v2ray/t" { + if v := platform.GetAssetLocation("t"); v != "/v2ray/t" { t.Error("asset loc: ", v) } } diff --git a/common/platform/windows.go b/common/platform/windows.go index 454a24063..08df2f46c 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -2,7 +2,12 @@ package platform -import "path/filepath" +import ( + "errors" + "io/fs" + "os" + "path/filepath" +) func ExpandEnv(s string) string { // TODO @@ -23,5 +28,19 @@ func GetToolLocation(file string) string { func GetAssetLocation(file string) string { const name = "v2ray.location.asset" assetPath := NewEnvFlag(name).GetValue(getExecutableDir) - return filepath.Join(assetPath, file) + defPath := filepath.Join(assetPath, file) + for _, p := range []string{ + defPath, + file, + } { + if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) { + continue + } + + // asset found + return p + } + + // asset not found, let the caller throw out the error + return defPath }