更新 main.go

This commit is contained in:
XOF
2025-12-17 01:39:13 +08:00
parent 1611d40296
commit c1b4f667c0

87
main.go
View File

@@ -23,6 +23,7 @@ const (
checkInterval = 24 * time.Hour checkInterval = 24 * time.Hour
maxRetries = 3 maxRetries = 3
versionAPI = "https://versionhistory.googleapis.com/v1/chrome/platforms/win64/channels/stable/versions" 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 ( var (
@@ -39,7 +40,6 @@ type Version struct {
} }
type ChromeVersion struct { type ChromeVersion struct {
Name string `json:"name"`
Version string `json:"version"` Version string `json:"version"`
} }
@@ -109,71 +109,52 @@ func getLatestVersion() (string, error) {
func checkAndDownload() { func checkAndDownload() {
version, err := getLatestVersion() version, err := getLatestVersion()
if err != nil { if err != nil {
log.Printf("Failed to get latest version: %v", err) log.Printf("Failed to get version: %v", err)
return return
} }
log.Printf("Latest Chrome version: %s", version) log.Printf("Latest Chrome version: %s", version)
// 尝试多个可能的下载 URL 模式 // 检查是否已存在该版本
urls := []string{ files, _ := os.ReadDir(downloadDir)
fmt.Sprintf("https://dl.google.com/release2/chrome/%%s_%s/%s_chrome_installer.exe", version, version), for _, f := range files {
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"), if strings.HasPrefix(f.Name(), "chrome_"+version+"_") {
} log.Printf("Version %s already exists, skipping download", version)
return
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)
} }
} }
log.Println("All download attempts failed") var data []byte
return 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)) hash := fmt.Sprintf("%x", md5.Sum(data))
filename := fmt.Sprintf("chrome_%s_%s.exe", version, hash[:8]) filename := fmt.Sprintf("chrome_%s_%s.exe", version, hash[:8])
filepath := filepath.Join(downloadDir, filename) 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 { if err := os.WriteFile(filepath, data, 0644); err != nil {
log.Printf("Error saving file: %v", err) log.Printf("Error saving file: %v", err)
return return