From 8180ee9bea72e2859aa74b42150e2e7ad51eee6e Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 17:45:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(proxy):=20=E4=BF=AE=E5=A4=8D=20WebSocket=20?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=A4=B1=E8=B4=A5=E6=97=B6=E7=9A=84=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在升级失败(状态码非 101)和写入响应失败时,确保 resp.Body.Close()。 Co-Authored-By: Claude Opus 4.6 --- internal/proxy/websocket.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/proxy/websocket.go b/internal/proxy/websocket.go index aa306d8..481dab9 100644 --- a/internal/proxy/websocket.go +++ b/internal/proxy/websocket.go @@ -393,14 +393,21 @@ func WebSocket(ctx *fasthttp.RequestCtx, target *loadbalance.Target, timeout tim // 步骤5: 检查响应状态码(期望 101 Switching Protocols) if resp.StatusCode != http.StatusSwitchingProtocols { + // 关闭响应 body(升级失败时) + _ = resp.Body.Close() return fmt.Errorf("backend rejected WebSocket upgrade: %s", resp.Status) } // 步骤6: 将升级响应发送回客户端 if err := writeUpgradeResponse(clientConn, resp); err != nil { + // 关闭响应 body(写入失败时) + _ = resp.Body.Close() return fmt.Errorf("failed to send upgrade response to client: %w", err) } + // 注意: WebSocket 升级成功后,resp.Body 不需要显式关闭 + // 因为底层连接已被 bridge 用于双向数据传输 + // 步骤7: 启动桥接(阻塞直到连接关闭) return bridge.Bridge() }