Skip to content

Commit bc7b680

Browse files
committed
fix(self-update): support GITHUB_PROXY for downloading install script
1 parent dd9b3b3 commit bc7b680

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

cmd/43.self-update.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,38 @@ func downloadToTempImpl(url, suffix string) (string, error) {
431431
return tmpFile.Name(), nil
432432
}
433433

434+
// applyGitHubProxy prepends the GITHUB_PROXY URL if the target is a GitHub resource.
435+
func applyGitHubProxy(rawURL string) string {
436+
if !strings.HasPrefix(rawURL, "https://github.com/") &&
437+
!strings.HasPrefix(rawURL, "https://objects.githubusercontent.com/") &&
438+
!strings.HasPrefix(rawURL, "https://raw.githubusercontent.com/") {
439+
return rawURL
440+
}
441+
442+
proxy := os.Getenv("GITHUB_PROXY")
443+
if proxy == "" {
444+
proxy = "https://gh-proxy.sn0wdr1am.com/"
445+
}
446+
return proxy + rawURL
447+
}
448+
449+
// downloadScriptWithProxyFallback attempts to download via proxy, falling back to direct URL.
450+
func downloadScriptWithProxyFallback(scriptURL, suffix string, formatter output.Formatter) (string, error) {
451+
proxiedURL := applyGitHubProxy(scriptURL)
452+
453+
// Try proxy first if it differs
454+
if proxiedURL != scriptURL {
455+
tmpScript, err := downloadToTempFn(proxiedURL, suffix)
456+
if err == nil {
457+
return tmpScript, nil
458+
}
459+
formatter.Warning(fmt.Sprintf("Failed to download via proxy (%s), falling back to direct URL...", proxiedURL), nil)
460+
}
461+
462+
// Direct download fallback
463+
return downloadToTempFn(scriptURL, suffix)
464+
}
465+
434466
// selfUpdateUnix downloads and executes the install.sh anchored to the given tag.
435467
func selfUpdateUnix(formatter output.Formatter, tag string) error {
436468
// Check if curl is available
@@ -445,7 +477,7 @@ func selfUpdateUnix(formatter output.Formatter, tag string) error {
445477
formatter.Info(fmt.Sprintf("Downloading install script for %s...", tag), nil)
446478

447479
// Download to temp file first (safe: avoids curl | sh pipe risk)
448-
tmpScript, err := downloadToTempFn(scriptURL, ".sh")
480+
tmpScript, err := downloadScriptWithProxyFallback(scriptURL, ".sh", formatter)
449481
if err != nil {
450482
formatter.Error("Failed to download install script", map[string]interface{}{"error": err.Error(), "url": scriptURL})
451483
return fmt.Errorf("download install script: %w", err)
@@ -486,7 +518,7 @@ func selfUpdateWindows(formatter output.Formatter, tag string) error {
486518
formatter.Info(fmt.Sprintf("Downloading install script for %s...", tag), nil)
487519

488520
// Download to temp file first
489-
tmpScript, err := downloadToTempFn(scriptURL, ".ps1")
521+
tmpScript, err := downloadScriptWithProxyFallback(scriptURL, ".ps1", formatter)
490522
if err != nil {
491523
formatter.Error("Failed to download install script", map[string]interface{}{"error": err.Error(), "url": scriptURL})
492524
return fmt.Errorf("download install script: %w", err)

0 commit comments

Comments
 (0)