diff --git a/version.txt b/version.txt index a97fc44..3b34d22 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.7.1 +v1.7.2 diff --git a/version/version.go b/version/version.go index eb7bb5f..b410def 100644 --- a/version/version.go +++ b/version/version.go @@ -6,6 +6,8 @@ import ( "io" "net/http" "os" + "os/exec" + "strings" "golang.org/x/mod/semver" ) @@ -61,22 +63,44 @@ func isUpdateRequired(current string, latest string) bool { } func getLatestVersion() (string, error) { - resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/github.com/%s/%s/@latest", repoOwner, repoName)) + cmd := exec.Command("go", "env", "GOPROXY") + output, err := cmd.Output() if err != nil { return "", err } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err + goproxy := strings.TrimSpace(string(output)) + if goproxy == "" { + goproxy = "https://proxy.golang.org" } - var version struct{ Version string } - err = json.Unmarshal(body, &version) - if err != nil { - return "", err + proxies := strings.Split(goproxy, ",") + for _, proxy := range proxies { + proxy = strings.TrimSpace(proxy) + proxy = strings.TrimRight(proxy, "/") + if proxy == "direct" { + continue + } + + url := fmt.Sprintf("%s/github.com/%s/%s/@latest", proxy, repoOwner, repoName) + resp, err := http.Get(url) + if err != nil { + continue + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var version struct{ Version string } + if err = json.Unmarshal(body, &version); err != nil { + return "", err + } + + return version.Version, nil } - return version.Version, nil + return "", fmt.Errorf("failed to fetch latest version from proxies") }