From c1b4f667c09fb6cba8238b6f1d8c416ebb696863 Mon Sep 17 00:00:00 2001 From: XOF Date: Wed, 17 Dec 2025 01:39:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20main.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 87 ++++++++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 53 deletions(-) diff --git a/main.go b/main.go index 5ca9c52..0774ddd 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ const ( checkInterval = 24 * time.Hour maxRetries = 3 versionAPI = "https://versionhistory.googleapis.com/v1/chrome/platforms/win64/channels/stable/versions" + chromeURL = "https://dl.google.com/tag/s/appguid%3D%7B8A69D345-D564-463C-AFF1-A69D9E530F96%7D%26iid%3D%7B00000000-0000-0000-0000-000000000000%7D%26lang%3Dzh-CN%26browser%3D4%26usagestats%3D0%26appname%3DGoogle%2520Chrome%26needsadmin%3Dprefers%26ap%3Dx64-stable-statsdef_1%26installdataindex%3Dempty/chrome/install/ChromeStandaloneSetup64.exe" ) var ( @@ -39,7 +40,6 @@ type Version struct { } type ChromeVersion struct { - Name string `json:"name"` Version string `json:"version"` } @@ -109,71 +109,52 @@ func getLatestVersion() (string, error) { func checkAndDownload() { version, err := getLatestVersion() if err != nil { - log.Printf("Failed to get latest version: %v", err) + log.Printf("Failed to get version: %v", err) return } log.Printf("Latest Chrome version: %s", version) - // 尝试多个可能的下载 URL 模式 - urls := []string{ - fmt.Sprintf("https://dl.google.com/release2/chrome/%%s_%s/%s_chrome_installer.exe", version, version), - fmt.Sprintf("https://dl.google.com/tag/s/appguid%%3D%%7B8A69D345-D564-463C-AFF1-A69D9E530F96%%7D%%26iid%%3D%%7B00000000-0000-0000-0000-000000000000%%7D%%26lang%%3Dzh-CN%%26browser%%3D4%%26usagestats%%3D0%%26appname%%3DGoogle%%2520Chrome%%26needsadmin%%3Dprefers%%26ap%%3Dx64-stable-statsdef_1%%26installdataindex%%3Dempty/chrome/install/ChromeStandaloneSetup64.exe"), - } - - var data []byte - client := &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { - log.Printf("Redirecting to: %s", req.URL.String()) - return nil - }, - } - - for _, urlPattern := range urls { - for i := 0; i < maxRetries; i++ { - url := urlPattern - if strings.Contains(url, "%s") { - // 跳过需要 hash 的 URL - continue - } - - log.Printf("Trying URL: %s", url) - resp, err := client.Get(url) - if err != nil { - log.Printf("Attempt %d failed: %v", i+1, err) - time.Sleep(time.Duration(i+1) * 10 * time.Second) - continue - } - - contentType := resp.Header.Get("Content-Type") - contentLength := resp.Header.Get("Content-Length") - log.Printf("Content-Type: %s, Content-Length: %s", contentType, contentLength) - - data, err = io.ReadAll(resp.Body) - resp.Body.Close() - - if err == nil && len(data) > 10000000 && !strings.Contains(contentType, "text/html") { - goto success - } - - log.Printf("Invalid response (size: %d bytes)", len(data)) - time.Sleep(time.Duration(i+1) * 10 * time.Second) + // 检查是否已存在该版本 + files, _ := os.ReadDir(downloadDir) + for _, f := range files { + if strings.HasPrefix(f.Name(), "chrome_"+version+"_") { + log.Printf("Version %s already exists, skipping download", version) + return } } - log.Println("All download attempts failed") - return + var data []byte + client := &http.Client{} + + for i := 0; i < maxRetries; i++ { + resp, err := client.Get(chromeURL) + if err != nil { + log.Printf("Attempt %d failed: %v", i+1, err) + time.Sleep(time.Duration(i+1) * 10 * time.Second) + continue + } + + data, err = io.ReadAll(resp.Body) + resp.Body.Close() + + if err == nil && len(data) > 10000000 { + break + } + + log.Printf("Attempt %d: invalid response (size: %d bytes)", i+1, len(data)) + time.Sleep(time.Duration(i+1) * 10 * time.Second) + } + + if len(data) < 10000000 { + log.Println("All download attempts failed") + return + } -success: hash := fmt.Sprintf("%x", md5.Sum(data)) filename := fmt.Sprintf("chrome_%s_%s.exe", version, hash[:8]) filepath := filepath.Join(downloadDir, filename) - if _, err := os.Stat(filepath); err == nil { - log.Println("File already exists, skipping") - return - } - if err := os.WriteFile(filepath, data, 0644); err != nil { log.Printf("Error saving file: %v", err) return