优化抢购
This commit is contained in:
+68
-30
@@ -216,7 +216,7 @@ func (c *DouyuClient) Score(cookie string) (int, error) {
|
||||
Balance int `json:"balance"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := c.doJSON(req, &result, "积分余额"); err != nil {
|
||||
if _, err := c.doJSON(req, &result, "积分余额"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
@@ -245,44 +245,60 @@ func (c *DouyuClient) GenerateCSRF(cookie string) (string, error) {
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
logNetworkError("生成CSRF", req, time.Since(start), err)
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
logNetworkHTTPError("生成CSRF", req, resp.StatusCode, time.Since(start), body)
|
||||
return "", fmt.Errorf("CSRF HTTP %d: %s", resp.StatusCode, trimForLog(string(body), 120))
|
||||
}
|
||||
|
||||
var api APIResponse
|
||||
if err := json.Unmarshal(body, &api); err != nil {
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, body, err.Error(), 0, "")
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, err.Error(), 0, "")
|
||||
logNetworkParseError("生成CSRF", req, time.Since(start), body, err)
|
||||
return "", fmt.Errorf("CSRF 响应解析失败: %w", err)
|
||||
}
|
||||
traceErr := ""
|
||||
if api.Error != 0 {
|
||||
traceErr = api.Msg
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, body, traceErr, api.Error, api.Msg)
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, traceErr, api.Error, api.Msg)
|
||||
logNetworkAPIResult("生成CSRF", req, time.Since(start), api.Error, api.Msg)
|
||||
return "", fmt.Errorf("CSRF 接口失败: %s", api.Msg)
|
||||
}
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "cvl_csrf_token" && ck.Value != "" {
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, body, "", api.Error, api.Msg)
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, "", api.Error, api.Msg)
|
||||
logNetworkSuccess("生成CSRF", req, time.Since(start), 0, "")
|
||||
return ck.Value, nil
|
||||
}
|
||||
}
|
||||
c.trace.record("生成CSRF", req, resp.StatusCode, time.Since(start), reqBody, body, "响应没有 cvl_csrf_token", api.Error, api.Msg)
|
||||
c.trace.recordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, "响应没有 cvl_csrf_token", api.Error, api.Msg)
|
||||
logEvent("网络", "warn", "接口返回异常: 生成CSRF %s %s 耗时=%s msg=响应没有 cvl_csrf_token",
|
||||
req.Method, requestPath(req), roundDuration(time.Since(start)))
|
||||
return "", errors.New("CSRF 响应没有 cvl_csrf_token")
|
||||
}
|
||||
|
||||
func csrfFromResponse(resp *http.Response) string {
|
||||
if resp == nil {
|
||||
return ""
|
||||
}
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "cvl_csrf_token" && ck.Value != "" {
|
||||
return ck.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// BatchExchangeLimit 请求兑换限制接口,返回本次允许的批量上限。
|
||||
func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID, csrf string) (int, error) {
|
||||
func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID string, csrf *string) (int, error) {
|
||||
currentCSRF := ""
|
||||
if csrf != nil {
|
||||
currentCSRF = *csrf
|
||||
}
|
||||
var result struct {
|
||||
APIResponse
|
||||
Data struct {
|
||||
@@ -293,7 +309,7 @@ func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID, csrf string) (int,
|
||||
"manualID": manualID,
|
||||
"commodityID": commodityID,
|
||||
"rid": roomID,
|
||||
"csrfToken": csrf,
|
||||
"csrfToken": currentCSRF,
|
||||
}, &result); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -308,14 +324,18 @@ func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID, csrf string) (int,
|
||||
}
|
||||
|
||||
// PreExchange 按抓包流程执行预兑换检查。
|
||||
func (c *DouyuClient) PreExchange(cookie, commodityID, csrf string) error {
|
||||
func (c *DouyuClient) PreExchange(cookie, commodityID string, csrf *string) error {
|
||||
currentCSRF := ""
|
||||
if csrf != nil {
|
||||
currentCSRF = *csrf
|
||||
}
|
||||
var result APIResponse
|
||||
if err := c.postMultipart(preExchangeURL, cookie, csrf, "预兑换", map[string]string{
|
||||
"manualID": manualID,
|
||||
"rid": roomID,
|
||||
"commodityID": commodityID,
|
||||
"num": "1",
|
||||
"csrfToken": csrf,
|
||||
"csrfToken": currentCSRF,
|
||||
}, &result); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -326,7 +346,11 @@ func (c *DouyuClient) PreExchange(cookie, commodityID, csrf string) error {
|
||||
}
|
||||
|
||||
// Exchange 正式兑换一个商品,num 固定为 1,和浏览器抓包一致。
|
||||
func (c *DouyuClient) Exchange(cookie, commodityID, csrf string) (string, error) {
|
||||
func (c *DouyuClient) Exchange(cookie, commodityID string, csrf *string) (string, error) {
|
||||
currentCSRF := ""
|
||||
if csrf != nil {
|
||||
currentCSRF = *csrf
|
||||
}
|
||||
var result struct {
|
||||
APIResponse
|
||||
Data struct {
|
||||
@@ -340,7 +364,7 @@ func (c *DouyuClient) Exchange(cookie, commodityID, csrf string) (string, error)
|
||||
"commodityID": commodityID,
|
||||
"num": "1",
|
||||
"rid": roomID,
|
||||
"csrfToken": csrf,
|
||||
"csrfToken": currentCSRF,
|
||||
}, &result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -379,11 +403,12 @@ func (c *DouyuClient) getJSON(url, cookie, label string, out any) error {
|
||||
return err
|
||||
}
|
||||
c.setBrowserHeaders(req, cookie)
|
||||
return c.doJSON(req, out, label)
|
||||
_, err = c.doJSON(req, out, label)
|
||||
return err
|
||||
}
|
||||
|
||||
// postMultipart 发送 multipart/form-data 请求并解析 JSON。
|
||||
func (c *DouyuClient) postMultipart(url, cookie, csrf, label string, fields map[string]string, out any) error {
|
||||
// postMultipart 发送 multipart/form-data 请求并解析 JSON,并同步响应里的 CSRF Cookie。
|
||||
func (c *DouyuClient) postMultipart(url, cookie string, csrf *string, label string, fields map[string]string, out any) error {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
for key, value := range fields {
|
||||
@@ -399,49 +424,62 @@ func (c *DouyuClient) postMultipart(url, cookie, csrf, label string, fields map[
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.setBrowserHeaders(req, mergeCookie(cookie, "cvl_csrf_token", csrf))
|
||||
currentCSRF := ""
|
||||
if csrf != nil {
|
||||
currentCSRF = *csrf
|
||||
}
|
||||
c.setBrowserHeaders(req, mergeCookie(cookie, "cvl_csrf_token", currentCSRF))
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
return c.doJSON(req, out, label)
|
||||
resp, err := c.doJSON(req, out, label)
|
||||
if resp != nil {
|
||||
if nextCSRF := csrfFromResponse(resp); nextCSRF != "" && csrf != nil {
|
||||
if nextCSRF != *csrf {
|
||||
logEvent("网络", "info", "响应刷新CSRF已同步: %s %s %s", label, req.Method, requestPath(req))
|
||||
}
|
||||
*csrf = nextCSRF
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// doJSON 执行请求、检查 HTTP 状态码并解析 JSON。
|
||||
func (c *DouyuClient) doJSON(req *http.Request, out any, label string) error {
|
||||
func (c *DouyuClient) doJSON(req *http.Request, out any, label string) (*http.Response, error) {
|
||||
start := time.Now()
|
||||
reqBody := traceRequestBody(c.trace, req)
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
c.trace.record(label, req, 0, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
logNetworkError(label, req, time.Since(start), err)
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, nil, err.Error(), 0, "")
|
||||
logNetworkError(label, req, time.Since(start), err)
|
||||
return err
|
||||
return resp, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
logNetworkHTTPError(label, req, resp.StatusCode, time.Since(start), body)
|
||||
return fmt.Errorf("HTTP %d: %s", resp.StatusCode, trimForLog(string(body), 160))
|
||||
return resp, fmt.Errorf("HTTP %d: %s", resp.StatusCode, trimForLog(string(body), 160))
|
||||
}
|
||||
if err := json.Unmarshal(body, out); err != nil {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, body, err.Error(), 0, "")
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, body, err.Error(), 0, "")
|
||||
logNetworkParseError(label, req, time.Since(start), body, err)
|
||||
return fmt.Errorf("JSON 解析失败: %w, body=%s", err, trimForLog(string(body), 160))
|
||||
return resp, fmt.Errorf("JSON 解析失败: %w, body=%s", err, trimForLog(string(body), 160))
|
||||
}
|
||||
if apiError, apiMsg, ok := extractAPIStatus(out); ok && apiError != 0 {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, body, apiMsg, apiError, apiMsg)
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, body, apiMsg, apiError, apiMsg)
|
||||
logNetworkAPIResult(label, req, time.Since(start), apiError, apiMsg)
|
||||
} else if shouldLogNetworkSuccess(label) {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
logNetworkSuccess(label, req, time.Since(start), 0, "")
|
||||
} else {
|
||||
c.trace.record(label, req, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
c.trace.recordResponse(label, req, resp, resp.StatusCode, time.Since(start), reqBody, body, "", 0, "")
|
||||
}
|
||||
return nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func shouldLogNetworkSuccess(label string) bool {
|
||||
|
||||
Reference in New Issue
Block a user