feat: 添加Cloudflare Access支持,允许通过请求头传递Client ID和Client Secret

This commit is contained in:
xdf
2025-08-20 16:32:53 +09:00
parent aa461f2189
commit fb9828378b
6 changed files with 53 additions and 5 deletions

View File

@@ -70,12 +70,27 @@ func uploadTaskResult(taskID, result string, exitCode int, finishedAt time.Time)
jsonData, _ := json.Marshal(payload)
endpoint := flags.Endpoint + "/api/clients/task/result?token=" + flags.Token
resp, _ := http.Post(endpoint, "application/json", bytes.NewBuffer(jsonData))
// 创建HTTP请求以支持自定义头部
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("Failed to create task result request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
// 添加Cloudflare Access头部如果配置了
if flags.CFAccessClientID != "" && flags.CFAccessClientSecret != "" {
req.Header.Set("CF-Access-Client-Id", flags.CFAccessClientID)
req.Header.Set("CF-Access-Client-Secret", flags.CFAccessClientSecret)
}
client := &http.Client{}
resp, err := client.Do(req)
maxRetry := flags.MaxRetries
for i := 0; i < maxRetry && resp.StatusCode != http.StatusOK; i++ {
for i := 0; i < maxRetry && (err != nil || resp.StatusCode != http.StatusOK); i++ {
log.Printf("Failed to upload task result, retrying %d/%d", i+1, maxRetry)
time.Sleep(2 * time.Second) // Wait before retrying
resp, _ = http.Post(endpoint, "application/json", bytes.NewBuffer(jsonData))
resp, err = client.Do(req)
}
if resp != nil {
defer resp.Body.Close()