feat: 调试开关打开时记录全部接口响应

This commit is contained in:
Nixevol
2026-10-16 15:41:30 +08:00
parent d5dcd7086d
commit 5d18da470d
4 changed files with 83 additions and 19 deletions
+37 -9
View File
@@ -1,6 +1,7 @@
package cmnet
import (
"bytes"
"log"
"net/http"
"net/url"
@@ -21,6 +22,10 @@ func Debugf(format string, args ...any) {
debugf(format, args...)
}
func DebugOn() bool {
return debugEnabled
}
func (c *Client) debugCookies(where string) {
if !debugEnabled {
return
@@ -66,7 +71,7 @@ func debugBody(tag string, buf []byte) {
debugf("%s 响应空", tag)
return
}
const limit = 2000
const limit = 8000
if len(buf) > limit {
debugf("%s 响应 %d 字节 %s", tag, len(buf), string(buf[:limit]))
return
@@ -74,15 +79,38 @@ func debugBody(tag string, buf []byte) {
debugf("%s 响应 %s", tag, string(buf))
}
func debugRequest(req *http.Request) bool {
func debugHTTP(req *http.Request, resp *http.Response, buf []byte, reqErr error) {
if !debugEnabled || req == nil || req.URL == nil {
return false
return
}
switch req.URL.Path {
case "/login.htm", "/verifyCaptcha.htm", "/sendRandomCodeAction.action", "/loadToken.action",
"/chkNumberAction.action", "/chkqr.htm", "/i/v1/auth/getArtifact", "/i/v1/auth/loginfo", "/fkRisk.action":
return true
default:
return strings.Contains(req.URL.Path, "/fee/")
if reqErr != nil {
debugf("%s %s 请求失败 %v", req.Method, req.URL.String(), reqErr)
return
}
if resp == nil {
return
}
ctype := resp.Header.Get("Content-Type")
debugf("%s %s -> %d %s Set-Cookie [%s]", req.Method, req.URL.String(), resp.StatusCode, ctype, debugSetCookies(resp))
if debugSkipBody(req, ctype, buf) {
debugf("%s 响应 %d 字节", req.URL.Path, len(buf))
return
}
debugBody(req.URL.RequestURI(), buf)
}
func debugSkipBody(req *http.Request, ctype string, buf []byte) bool {
ct := strings.ToLower(ctype)
path := strings.ToLower(req.URL.Path)
if strings.Contains(ct, "image") || strings.Contains(ct, "javascript") || strings.Contains(ct, "text/css") {
return true
}
switch {
case strings.HasSuffix(path, ".js"), strings.HasSuffix(path, ".css"), strings.HasSuffix(path, ".png"),
strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"), strings.HasSuffix(path, ".gif"),
strings.HasSuffix(path, ".ico"), strings.HasSuffix(path, ".html"):
return true
}
trim := bytes.TrimSpace(buf)
return len(trim) > 0 && trim[0] == '<'
}