Files
OpenFlare/internal/apps/openflare/websocket/read_pump.go
T
ryan 1e5f35b9a3 fix(openflare): refresh WS read deadline on JSON pong; capture frpc stderr
- read_pump: 收到客户端 JSON {"type":"pong"} 时调用 conn.SetReadDeadline 刷新
  服务端读超时。修复前,服务端仅在 WebSocket 协议层 Pong 帧时刷新 deadline,
  而客户端使用 JSON 应用层 pong 回复,导致服务端 90s 后超时关闭连接,
  客户端收到 EOF 并触发无限重连循环。在 Cloudflare 代理场景下,
  100s 空闲超时进一步加剧了此问题。

- frpc/manager: 捕获 frpc 子进程 stderr 并在进程异常退出时
  将其内容记录到结构化日志 stderr 字段,便于诊断 exit status 1 的
  具体原因(如配置格式错误、Auth Token 失败、relay 服务端不可达等)。
2026-06-22 14:22:20 +08:00

57 lines
1.4 KiB
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package websocket
import (
"encoding/json"
"log/slog"
"time"
"github.com/gorilla/websocket"
)
func runReadPump(
nodeID string,
conn *websocket.Conn,
closeFn func(),
logLabel string,
sendPong func(string) bool,
clientPongType string,
) {
defer closeFn()
_ = conn.SetReadDeadline(time.Now().Add(wsReadDeadline))
conn.SetPongHandler(func(string) error {
return conn.SetReadDeadline(time.Now().Add(wsReadDeadline))
})
for {
_, data, err := conn.ReadMessage()
if err != nil {
slog.Debug(logLabel+" read closed", "node_id", nodeID, "error", err)
return
}
var message Message
if err = json.Unmarshal(data, &message); err != nil {
slog.Debug(logLabel+" invalid message", "node_id", nodeID, "error", err)
continue
}
switch message.Type {
case messageTypePing:
_ = sendPong(nodeID)
case clientPongType:
// Refresh read deadline when the client replies with a JSON pong.
// This keeps the connection alive when the WebSocket is proxied
// through Cloudflare, which enforces a 100-second idle timeout on
// the TCP stream. Without this refresh, the server's 90-second read
// deadline expires and terminates the connection even though the
// client is actively responding to pings.
_ = conn.SetReadDeadline(time.Now().Add(wsReadDeadline))
default:
slog.Debug(logLabel+" unsupported message", "node_id", nodeID, "type", message.Type)
}
}
}