更新 main.go
This commit is contained in:
51
main.go
51
main.go
@@ -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
|
var data []byte
|
||||||
client := &http.Client{
|
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++ {
|
for i := 0; i < maxRetries; i++ {
|
||||||
url := urlPattern
|
resp, err := client.Get(chromeURL)
|
||||||
if strings.Contains(url, "%s") {
|
|
||||||
// 跳过需要 hash 的 URL
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Trying URL: %s", url)
|
|
||||||
resp, err := client.Get(url)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Attempt %d failed: %v", i+1, err)
|
log.Printf("Attempt %d failed: %v", i+1, err)
|
||||||
time.Sleep(time.Duration(i+1) * 10 * time.Second)
|
time.Sleep(time.Duration(i+1) * 10 * time.Second)
|
||||||
continue
|
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)
|
data, err = io.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
if err == nil && len(data) > 10000000 && !strings.Contains(contentType, "text/html") {
|
if err == nil && len(data) > 10000000 {
|
||||||
goto success
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Invalid response (size: %d bytes)", len(data))
|
log.Printf("Attempt %d: invalid response (size: %d bytes)", i+1, len(data))
|
||||||
time.Sleep(time.Duration(i+1) * 10 * time.Second)
|
time.Sleep(time.Duration(i+1) * 10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if len(data) < 10000000 {
|
||||||
log.Println("All download attempts failed")
|
log.Println("All download attempts failed")
|
||||||
return
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user