Skip to content

Commit c1668ce

Browse files
committed
更改UA,增加日志
1 parent 59546ae commit c1668ce

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

core/download.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ func DownloadFile(url, filePath string) error {
5555
if start > 0 {
5656
req.Header.Set("Range", "bytes="+strconv.FormatInt(start, 10)+"-")
5757
}
58-
var gitversion string
59-
req.Header.Set("User-Agent", "AutoInstall/"+gitversion)
58+
req.Header.Set("User-Agent", "autoinst/1.3.0")
6059
client := &http.Client{}
6160
resp, err := client.Do(req)
6261

core/logger.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"sync"
8+
"time"
9+
)
10+
11+
type DownloadError struct {
12+
URL string
13+
Err error
14+
Response string
15+
}
16+
17+
var (
18+
LogFile *os.File
19+
DownloadErrors []DownloadError
20+
ErrorMutex sync.Mutex
21+
)
22+
23+
func SetupLogger() error {
24+
logDir := filepath.Join(".autoinst", "logs")
25+
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
26+
return err
27+
}
28+
dateStr := time.Now().Format("2006-01-02")
29+
logPath := filepath.Join(logDir, dateStr+".log")
30+
31+
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
32+
if err != nil {
33+
return err
34+
}
35+
LogFile = f
36+
return nil
37+
}
38+
39+
func RecordError(url string, err error, response string) {
40+
ErrorMutex.Lock()
41+
defer ErrorMutex.Unlock()
42+
DownloadErrors = append(DownloadErrors, DownloadError{
43+
URL: url,
44+
Err: err,
45+
Response: response,
46+
})
47+
48+
if LogFile != nil {
49+
timestamp := time.Now().Format("15:04:05")
50+
msg := fmt.Sprintf("[%s] Error downloading %s: %v | Response: %s\n", timestamp, url, err, response)
51+
LogFile.WriteString(msg)
52+
}
53+
}
54+
55+
func CloseLogger() {
56+
if LogFile != nil {
57+
LogFile.Close()
58+
}
59+
}

main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func main() {
1616
if gitversion == "" {
1717
gitversion = "NaN"
1818
}
19+
if err := core.SetupLogger(); err != nil {
20+
fmt.Println("无法初始化日志系统:", err)
21+
}
22+
defer core.CloseLogger()
23+
1924
cleaninst := core.Argument(gitversion)
2025
core.Argument(gitversion)
2126
os.MkdirAll(".autoinst/cache", os.ModePerm)
@@ -51,4 +56,14 @@ func main() {
5156
} else {
5257
fmt.Println("无法访问 inst.json 文件:", err)
5358
}
59+
60+
if len(core.DownloadErrors) > 0 {
61+
fmt.Println("安装过程中出现错误,详情请查看日志。")
62+
if core.LogFile != nil {
63+
core.LogFile.WriteString("\n--- 错误汇总 ---\n")
64+
for _, e := range core.DownloadErrors {
65+
core.LogFile.WriteString(fmt.Sprintf("URL: %s\nError: %v\nResponse: %s\n----------------\n", e.URL, e.Err, e.Response))
66+
}
67+
}
68+
}
5469
}

pkg/cf.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func resolveCFDownloadURL(projectID, fileID int) (string, error) {
4545
}
4646
req.Header.Set("Accept", "application/json")
4747
req.Header.Set("x-api-key", cfapiKey)
48+
req.Header.Set("User-Agent", "autoinst/1.3.0")
4849
resp, err := http.DefaultClient.Do(req)
4950
if err != nil {
5051
return "", err
@@ -176,6 +177,8 @@ func CurseForge(file string, MaxCon int, Args string) {
176177

177178
url, err := resolveCFDownloadURL(entry.ProjectID, entry.FileID)
178179
if err != nil {
180+
apiUrl := fmt.Sprintf("https://api.curseforge.com/v1/mods/%d/files/%d/download-url", entry.ProjectID, entry.FileID)
181+
core.RecordError(apiUrl, err, err.Error())
179182
errChan <- err
180183
return
181184
}
@@ -192,6 +195,7 @@ func CurseForge(file string, MaxCon int, Args string) {
192195
}
193196
fmt.Println("尝试下载:", url)
194197
if err := core.DownloadFile(url, dst); err != nil {
198+
core.RecordError(url, err, "Download failed")
195199
errChan <- fmt.Errorf("下载失败(Project %d, File %d): %v", entry.ProjectID, entry.FileID, err)
196200
return
197201
}

0 commit comments

Comments
 (0)