From e0e99073b738cd26bf95d4801e4bccfdcf5dbca9 Mon Sep 17 00:00:00 2001 From: lcy Date: Wed, 24 Dec 2025 18:28:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20API=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=AF=BC=E8=87=B4=E8=8A=82=E7=82=B9=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优先检查错误和 HTTP 状态码,避免处理无效响应 - 只有在成功响应时才检查 hash,防止错误响应污染 hash - 修复当 API 返回 503 等错误时误判为配置变更的问题 --- api/panel/server.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/api/panel/server.go b/api/panel/server.go index d2ee184..bed5b01 100644 --- a/api/panel/server.go +++ b/api/panel/server.go @@ -98,9 +98,21 @@ func GetServerConfig(c *ClientV2) (*ServerConfigResponse, error) { ForceContentType("application/json"). Get(path) + // 优先检查错误,避免处理无效响应 + if err != nil { + return nil, fmt.Errorf("访问 %s 失败: %v", client.BaseURL+path, err.Error()) + } + + // 检查 HTTP 状态码 if r.StatusCode() == 304 { return nil, nil } + if r.StatusCode() >= 400 { + body := r.Body() + return nil, fmt.Errorf("访问 %s 失败: %s", client.BaseURL+path, string(body)) + } + + // 只有在成功响应时才检查 hash hash := sha256.Sum256(r.Body()) newBodyHash := hex.EncodeToString(hash[:]) if c.responseBodyHash == newBodyHash { @@ -108,13 +120,6 @@ func GetServerConfig(c *ClientV2) (*ServerConfigResponse, error) { } c.responseBodyHash = newBodyHash c.ServerConfigEtag = r.Header().Get("ETag") - if err != nil { - return nil, fmt.Errorf("访问 %s 失败: %v", client.BaseURL+path, err.Error()) - } - if r.StatusCode() >= 400 { - body := r.Body() - return nil, fmt.Errorf("访问 %s 失败: %s", client.BaseURL+path, string(body)) - } if r != nil { defer func() { if r.RawBody() != nil {