Skip to content

Commit 58af79c

Browse files
Devocyclaude
andcommitted
feat: 为 topup 流程添加详细的 debug 日志
- 添加请求 URL、请求体、请求头的 debug 日志 - 添加响应状态码、响应体长度的 debug 日志 - 添加账号下载过程的详细日志 - 添加 auth_json 和 download_url 的处理日志 - 便于排查 topup 返回 0 个账号的问题 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b3552a1 commit 58af79c

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎internal/topup/client.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func (c *Client) sendRequest(url string, req *TopupRequest) (*TopupResponse, err
9191
return nil, fmt.Errorf("failed to marshal request: %w", err)
9292
}
9393

94+
logger.Debug("Topup 请求 URL: %s", url)
95+
logger.Debug("Topup 请求体: target_pool_size=%d, reports=%d, account_ids=%d",
96+
req.TargetPoolSize, len(req.Reports), len(req.AccountIDs))
97+
9498
// Create HTTP request
9599
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(data))
96100
if err != nil {
@@ -100,6 +104,7 @@ func (c *Client) sendRequest(url string, req *TopupRequest) (*TopupResponse, err
100104
// Set headers
101105
httpReq.Header.Set("Content-Type", "application/json")
102106
httpReq.Header.Set("X-User-Key", c.userKey)
107+
logger.Debug("Topup 请求头: X-User-Key=%s...%s", c.userKey[:8], c.userKey[len(c.userKey)-4:])
103108

104109
// Send request
105110
httpResp, err := c.httpClient.Do(httpReq)
@@ -108,23 +113,32 @@ func (c *Client) sendRequest(url string, req *TopupRequest) (*TopupResponse, err
108113
}
109114
defer httpResp.Body.Close()
110115

116+
logger.Debug("Topup 响应状态码: %d", httpResp.StatusCode)
117+
111118
// Read response
112119
body, err := io.ReadAll(httpResp.Body)
113120
if err != nil {
114121
return nil, fmt.Errorf("failed to read response: %w", err)
115122
}
116123

124+
logger.Debug("Topup 响应体长度: %d bytes", len(body))
125+
117126
// Check status code
118127
if httpResp.StatusCode != http.StatusOK {
128+
logger.Debug("Topup 错误响应: %s", string(body))
119129
return nil, fmt.Errorf("server returned status %d: %s", httpResp.StatusCode, string(body))
120130
}
121131

122132
// Parse response
123133
var resp TopupResponse
124134
if err := json.Unmarshal(body, &resp); err != nil {
135+
logger.Debug("Topup 响应解析失败: %s", string(body))
125136
return nil, fmt.Errorf("failed to parse response: %w", err)
126137
}
127138

139+
logger.Debug("Topup 响应: ok=%v, accounts=%d, auto_disabled=%v, abuse_auto_banned=%v, total_hold_limit=%d",
140+
resp.OK, len(resp.Accounts), resp.AutoDisabled, resp.AbuseAutoBanned, resp.TotalHoldLimit)
141+
128142
return &resp, nil
129143
}
130144

‎internal/topup/response.go‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,42 @@ import (
1616
func DownloadAccounts(resp *TopupResponse, accountsDir string, httpClient *http.Client) ([]account.Account, error) {
1717
var newAccounts []account.Account
1818

19-
for _, accInfo := range resp.Accounts {
20-
logger.Info("处理账号:%s", accInfo.FileName)
19+
logger.Debug("开始下载账号:总数=%d", len(resp.Accounts))
20+
21+
for i, accInfo := range resp.Accounts {
22+
logger.Info("处理账号 %d/%d:%s", i+1, len(resp.Accounts), accInfo.FileName)
23+
logger.Debug("账号信息: download_url=%s, auth_json_len=%d",
24+
accInfo.DownloadURL, len(accInfo.AuthJSON))
2125

2226
var acc account.Account
2327

2428
// Try to use auth_json first
2529
if len(accInfo.AuthJSON) > 0 {
30+
logger.Debug("使用 auth_json 解析账号")
2631
if err := json.Unmarshal(accInfo.AuthJSON, &acc); err != nil {
2732
logger.Warn("解析 %s 的 auth_json 失败:%v", accInfo.FileName, err)
33+
logger.Debug("auth_json 内容: %s", string(accInfo.AuthJSON))
2834
continue
2935
}
36+
logger.Debug("auth_json 解析成功: account_id=%s, email=%s", acc.AccountID, acc.Email)
3037
} else if accInfo.DownloadURL != "" {
3138
// Download from URL
39+
logger.Debug("从 URL 下载账号: %s", accInfo.DownloadURL)
3240
var err error
3341
acc, err = downloadFromURL(accInfo.DownloadURL, httpClient)
3442
if err != nil {
3543
logger.Warn("从 %s 下载账号失败:%v", accInfo.DownloadURL, err)
3644
continue
3745
}
46+
logger.Debug("URL 下载成功: account_id=%s, email=%s", acc.AccountID, acc.Email)
3847
} else {
3948
logger.Warn("%s 没有 auth_json 或 download_url", accInfo.FileName)
4049
continue
4150
}
4251

4352
// Set file path
4453
acc.FilePath = filepath.Join(accountsDir, accInfo.FileName)
54+
logger.Debug("设置文件路径: %s", acc.FilePath)
4555

4656
// Save account
4757
storage := account.NewStorage(accountsDir)
@@ -54,18 +64,24 @@ func DownloadAccounts(resp *TopupResponse, accountsDir string, httpClient *http.
5464
newAccounts = append(newAccounts, acc)
5565
}
5666

67+
logger.Debug("下载完成:成功=%d, 失败=%d", len(newAccounts), len(resp.Accounts)-len(newAccounts))
68+
5769
return newAccounts, nil
5870
}
5971

6072
func downloadFromURL(url string, httpClient *http.Client) (account.Account, error) {
6173
var acc account.Account
6274

75+
logger.Debug("开始从 URL 下载: %s", url)
76+
6377
resp, err := httpClient.Get(url)
6478
if err != nil {
6579
return acc, fmt.Errorf("failed to download: %w", err)
6680
}
6781
defer resp.Body.Close()
6882

83+
logger.Debug("下载响应状态码: %d", resp.StatusCode)
84+
6985
if resp.StatusCode != http.StatusOK {
7086
return acc, fmt.Errorf("download failed with status %d", resp.StatusCode)
7187
}
@@ -75,7 +91,10 @@ func downloadFromURL(url string, httpClient *http.Client) (account.Account, erro
7591
return acc, fmt.Errorf("failed to read response: %w", err)
7692
}
7793

94+
logger.Debug("下载响应体长度: %d bytes", len(body))
95+
7896
if err := json.Unmarshal(body, &acc); err != nil {
97+
logger.Debug("解析失败的 JSON: %s", string(body))
7998
return acc, fmt.Errorf("failed to parse account JSON: %w", err)
8099
}
81100

0 commit comments

Comments
 (0)