更新 main.go

This commit is contained in:
XOF
2025-12-17 02:05:17 +08:00
parent 1aa29a6262
commit e3f2409c23

65
main.go
View File

@@ -1,11 +1,8 @@
package main package main
import ( import (
"crypto/md5"
"crypto/subtle" "crypto/subtle"
"encoding/base64"
"encoding/xml" "encoding/xml"
"fmt"
"html/template" "html/template"
"io" "io"
"log" "log"
@@ -37,6 +34,7 @@ type Version struct {
Filename string Filename string
Size int64 Size int64
Time time.Time Time time.Time
SHA256 string
} }
type UpdateResponse struct { type UpdateResponse struct {
@@ -166,13 +164,14 @@ func checkAndDownload() {
return return
} }
// 检查是否已存在该版本 parts := strings.Split(downloadURL, "/")
files, _ := os.ReadDir(downloadDir) filename := parts[len(parts)-1]
for _, f := range files { filePath := filepath.Join(downloadDir, filename)
if strings.HasPrefix(f.Name(), "chrome_"+version+"_") { sha256File := filePath + ".sha256"
log.Printf("Version %s already exists, skipping download", version)
return if _, err := os.Stat(filePath); err == nil {
} log.Printf("File %s already exists, skipping download", filename)
return
} }
var data []byte var data []byte
@@ -202,22 +201,20 @@ func checkAndDownload() {
return return
} }
hash := fmt.Sprintf("%x", md5.Sum(data)) if err := os.WriteFile(filePath, data, 0644); err != nil {
filename := fmt.Sprintf("chrome_%s_%s.exe", version, hash[:8])
filepath := filepath.Join(downloadDir, filename)
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
} }
os.WriteFile(sha256File, []byte(sha256), 0644)
log.Printf("Downloaded: %s (%.2f MB)", filename, float64(len(data))/1024/1024) log.Printf("Downloaded: %s (%.2f MB)", filename, float64(len(data))/1024/1024)
cleanupOldVersions() cleanupOldVersions()
} }
func cleanupOldVersions() { func cleanupOldVersions() {
files, _ := os.ReadDir(downloadDir) files, _ := os.ReadDir(downloadDir)
if len(files) <= keepVersions { if len(files) <= keepVersions*2 {
return return
} }
@@ -239,6 +236,7 @@ func cleanupOldVersions() {
for i := keepVersions; i < len(versions); i++ { for i := keepVersions; i < len(versions); i++ {
os.Remove(filepath.Join(downloadDir, versions[i].Filename)) os.Remove(filepath.Join(downloadDir, versions[i].Filename))
os.Remove(filepath.Join(downloadDir, versions[i].Filename+".sha256"))
log.Printf("Removed old version: %s", versions[i].Filename) log.Printf("Removed old version: %s", versions[i].Filename)
} }
} }
@@ -263,10 +261,12 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
for _, f := range files { for _, f := range files {
if strings.HasSuffix(f.Name(), ".exe") { if strings.HasSuffix(f.Name(), ".exe") {
info, _ := f.Info() info, _ := f.Info()
sha256, _ := os.ReadFile(filepath.Join(downloadDir, f.Name()+".sha256"))
versions = append(versions, Version{ versions = append(versions, Version{
Filename: f.Name(), Filename: f.Name(),
Time: info.ModTime(), Time: info.ModTime(),
Size: info.Size(), Size: info.Size(),
SHA256: string(sha256),
}) })
} }
} }
@@ -279,14 +279,35 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("index").Parse(` tmpl := template.Must(template.New("index").Parse(`
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head><title>Chrome Offline Installer</title></head> <head>
<title>Chrome Offline Installer</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
.sha256 { font-family: monospace; font-size: 0.9em; word-break: break-all; }
</style>
</head>
<body> <body>
<h1>Chrome Offline Versions</h1> <h1>Chrome Offline Versions (x64)</h1>
<ul> <table>
<tr>
<th>Filename</th>
<th>Size</th>
<th>Date</th>
<th>SHA256</th>
</tr>
{{range .Versions}} {{range .Versions}}
<li><a href="/download/{{.Filename}}{{if $.Token}}?token={{$.Token}}{{end}}">{{.Filename}}</a> ({{printf "%.2f" .SizeMB}} MB, {{.Time.Format "2006-01-02 15:04"}})</li> <tr>
<td><a href="/download/{{.Filename}}{{if $.Token}}?token={{$.Token}}{{end}}">{{.Filename}}</a></td>
<td>{{printf "%.2f" .SizeMB}} MB</td>
<td>{{.Time.Format "2006-01-02 15:04"}}</td>
<td class="sha256">{{.SHA256}}</td>
</tr>
{{end}} {{end}}
</ul> </table>
</body> </body>
</html> </html>
`)) `))
@@ -295,6 +316,7 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
Filename string Filename string
SizeMB float64 SizeMB float64
Time time.Time Time time.Time
SHA256 string
} }
var displayVersions []VersionDisplay var displayVersions []VersionDisplay
@@ -303,6 +325,7 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
Filename: v.Filename, Filename: v.Filename,
SizeMB: float64(v.Size) / 1024 / 1024, SizeMB: float64(v.Size) / 1024 / 1024,
Time: v.Time, Time: v.Time,
SHA256: v.SHA256,
}) })
} }