Fix: Make the WebSocket transport reachable and bounded on every platform
The POSIX clients were built without a connection config - Linux, macOS and Android all construct IOCPClient without the injected CONNECT_ADDRESS - so ConnectServer saw protoType -1 and quietly dialled the master port in plain TCP. Setting protoType to WS in their settings did nothing and logged nothing, and since CLIENT_CAP_PROTO_SWITCH is Windows-only the server could not move them either. Give the client a SetDefaultProto() for that construction path and call it from all three mains before the reconnect loop, with a one-shot log when neither a conn nor a default protocol is available so the silent fallback becomes visible next time. Bound what a peer can make the client read. ReadHeader accepted the 8-byte extended length and rejected only the sign bit, so a frame length of nearly 2^63 kept the connection streaming into a frame that would never end - alive but never delivering another packet. Cap single frames at 256 MB, the same value ServerWsSession already applies to client frames, and drop the connection past it; real traffic is far below that. Stop Reset() from writing the receive thread's frame state. Disconnect() can run from the heartbeat or command thread, not just the receive thread, so Reset() now only flips the atomic ready flag and a new ResetFrames() clears m_remaining, m_masked and m_maskPos from Handshake() alone, before the receive thread starts reading the socket. Those fields keep a single owner without adding a lock to the receive hot path. Finish a dead WS session instead of leaving it to the keepalive timer. When the session had closed but wsState was still WS_Ready, sends were dropped and nothing woke the pending recv, so a peer that closed and then went quiet held the context for the 3-minute keepalive. shutdown() the socket there so the recv returns zero bytes and the normal -2 teardown runs; RemoveStaleContext would deadlock against the caller's own IoRefCount and can hand back a reused context. That shutdown only finishes the connection if a zero-byte read still tears it down, and on the plain path it no longer did. DispatchPlain answers an empty buffer with "handled", so a peer's FIN left the connection in CLOSE_WAIT until keepalive - holding an IOCP context, a pending WSARecv and a rate-limit slot - where the pre-WS code called ParseReceivedData and tore down at once. Restore that teardown on the TLS_Plain branch, matching the other two. Checked against a Debug server: a plain socket that half-closes used to sit in CLOSE_WAIT with the peer's recv timing out, and now disappears immediately with recv returning zero. Two smaller cleanups from the same review. Give the WS upgrade its own prefix buffer (wsPreClassify) instead of borrowing the TLS sniffer's, which was serially reused for two state machines and read as if it were one. And stop deriving the build dialog's protoType from the combo box index: items now carry the protocol value as item data, keyed off the index AddString returns, so inserting an option in the middle can no longer write the wrong number into the generated client without any error. ApplyProtoSwitch loses its bool return, which contradicted its own side effects and was ignored by every caller. Co-Authored-By: deepseek-flash
This commit was merged in pull request #5.
This commit is contained in:
1 parent
50a489e316
commit
46f4bf2dfb
12 files changed
+149
-38
No files matched your search
@@ -79,6 +79,10 @@ XOR、zstd、HTTP 伪装层)。WS 与 HTTP 伪装的 `MaskType` 互斥:选 W
|
||||
线程都会写帧。
|
||||
- 不校验 `Sec-WebSocket-Accept`(设计上由 TLS + 隧道内登录承担)。明文 `PROTO_WS` 下这意味着
|
||||
中间人可以伪造 101 接管字节流——这正是「裸 WS 只适合内网」的另一半理由。
|
||||
- 没有 `conn` 的构造路径(Linux / macOS / Android 的 `main` 都是 `new IOCPClient(exit, false)`)
|
||||
拿不到外壳注入的 `CONNECT_ADDRESS`,协议由 `IOCPClient::SetDefaultProto()` 显式给出;
|
||||
三个平台的 `main` 都在进入重连循环前调一次。不调则保持 `-1`、按明文直连——那正是
|
||||
「配置成 WS 却毫无反应」的来源,所以这里留了一行日志便于排查。
|
||||
|
||||
### 3.1 信任模型:不换锚,靠 SNI 分流
|
||||
|
||||
@@ -197,7 +201,8 @@ WS 叠在同一层状态机之上,**与 TLS 是分层而不是并列**(`wss`
|
||||
- 状态机:`ServerWsState { WS_Undecided → WS_Handshaking → WS_Ready | WS_None }` 与既有的
|
||||
`ServerTlsState` 并存;`CONTEXT_OBJECT` 复用(`InitMember`)时一并复位并释放会话。
|
||||
- 判定点:**明文的第一段有 ≥6 字节**才开始分类(与 ClientHello 嗅探同一下限),
|
||||
避免把「请求头还没到齐」误判掉;阈值内的字节攒在 `tlsPreClassify` 里。
|
||||
避免把「请求头还没到齐」误判掉;阈值内的字节攒在 `wsPreClassify` 里(与 TLS 嗅探的
|
||||
`tlsPreClassify` 分开,两者服务不同状态机,串行使用但字段不共用)。
|
||||
- 升级线程与 TLS 握手线程同构:`FIONBIO=0`(该套接字常是非阻塞的,`ioctlsocket` 会以
|
||||
`WSAEINVAL` 失败),15 s 收发超时,遇 `WSAEWOULDBLOCK` 用 `select` 等可读/可写再试;
|
||||
线程持有一枚 `IoRefCount`,结束后把 socket 交还 IOCP(`PostRecv`)。升级响应之后同批
|
||||
@@ -218,8 +223,15 @@ WS 叠在同一层状态机之上,**与 TLS 是分层而不是并列**(`wss`
|
||||
- fail-closed 的两处:握手期间(`wsState == WS_Handshaking`)收到 IORead 直接断开(判断
|
||||
放在 `OnClientReceiving` 最开头,WSS 下 `tlsState` 已是 `TLS_Ready`,放在 TLS 分支之后
|
||||
就不生效);`wsState == WS_Ready` 但会话已关时,应用字节**丢弃而不是裸发**,否则对端
|
||||
解帧错位。
|
||||
解帧错位;同时对该 socket 做一次 `shutdown(SD_BOTH)`,让挂起的 recv 立刻返回 0 字节,
|
||||
由接收路径走正常的 `-2` 收尾(不在这里直接 `RemoveStaleContext`——调用方往往本身就
|
||||
持有 `IoRefCount`,自己等自己会超时,且可能把已回收复用的上下文交回给调用者)。
|
||||
- 既有客户端零影响:非 `GET ` 首包在攒够 6 字节后原样进入 `ParseReceivedData`。
|
||||
- 收尾一致:`dwTrans == 0`(对端 FIN)在 `TLS_Unclassified` / `TLS_Ready` / `TLS_Plain`
|
||||
三条分支上都必须 `RemoveStaleContext`。WS 改造把 `TLS_Plain` 的 `ParseReceivedData`
|
||||
换成 `DispatchPlain` 后,`DispatchPlain` 对空数据返回「已处理」,这个判断一度丢了,
|
||||
连接会停在 CLOSE_WAIT 直到 keepalive(3 分钟),期间占着 IOCP 上下文、一枚挂起的
|
||||
`WSARecv` 和限流槽。现已补回。
|
||||
|
||||
## 5. nginx 最小改动
|
||||
|
||||
@@ -426,7 +438,9 @@ server {
|
||||
- 客户端落盘(`settings` 下):`proto_override` / `entry_host` / `entry_port` /
|
||||
`entry_try_at` 为当前覆盖;`base_*` 是首次切换时记下的安装入口(「还原」用它);`prev_*`
|
||||
是每次切换前的入口(自动回退用它)。重连循环在建连前 `ApplyProtoSwitch` 覆盖入口——位置
|
||||
在「外壳注入完配置之后、建连之前」,因此换 DLL 也压不回去。
|
||||
在「外壳注入完配置之后、建连之前」,因此换 DLL 也压不回去。(该函数返回 `void`:它既写
|
||||
配置也改 `CONNECT_ADDRESS`,返回值无法表达「部分完成」,早期版本里的 `return false`
|
||||
只是提前退出,调用方一直忽略它。)
|
||||
- 撤销自动回退的判据是**登录被接受**,不是「连接建立」。客户端在收到
|
||||
`CMD_MASTERSETTING` 时把当前连接标记为已登录(服务端只有在把本机加入在线列表之后才下发
|
||||
它),重连循环每轮心跳都检查一次,命中才 `ConfirmProtoSwitch` 把 `entry_try_at` 清零。
|
||||
@@ -444,13 +458,21 @@ server {
|
||||
|
||||
## 10. 审查遗留(已知边界,未改动)
|
||||
|
||||
以下条目来自 PR 审查,评估后保留现状,记录在此以免重复讨论:
|
||||
以下条目来自 PR 审查,记录在此以免重复讨论。带「已处理」的是复查后**已经改掉**的,
|
||||
留在这里说明当时的判断,其余评估后保留现状:
|
||||
|
||||
- **`GetPeerName(SOCKET)` 只走 `sockaddr_in`(IPv4)**:`ServerWsSession.cpp::IsLoopbackPeer`
|
||||
里的 `::1` / `::ffff:127.0.0.1` 分支目前是死代码。将来若监听 IPv6,转发头信任也需要跟着
|
||||
改,否则会静默失效。
|
||||
- **`tlsPreClassify` 名不副实**:它现在同时承担 ClientHello 嗅探与 WS 前缀("GET ")buffering。
|
||||
改名会牵动状态机注释与多处引用,暂保留原名字,语义见 §4.2。
|
||||
- **`tlsPreClassify` 名不副实**:已处理——WS 的 "GET " 前缀缓冲拆成独立的 `wsPreClassify`,
|
||||
`tlsPreClassify` 回归 ClientHello 嗅探单一职责(两条路径仍按先后串行,互不重叠)。
|
||||
- **`WsChannel::Reset()` 与接收线程的帧状态**:已处理——`Disconnect()` 可能来自心跳/命令线程,
|
||||
不只是接收线程,所以 `Reset()` 现在只翻转原子的 `m_ready`;`m_remaining` / `m_masked` /
|
||||
`m_maskPos` 改由 `ResetFrames()` 清,且只在 `Handshake()` 里(连接线程、接收线程尚未起来)
|
||||
调用。这样这几个字段始终只有一个线程访问,不用加锁。
|
||||
- **客户端单帧载荷上限 256 MB**(`WsChannel::kMaxFramePayload`,与 `ServerWsSession` 同值):
|
||||
超过即按错误断开。没有上限时一个错误长度字段就能把 `m_remaining` 定在天文数字上,
|
||||
连接「活着但永远收不到新包」。正常业务的最大单包远小于此值。
|
||||
- **`DispatchPlain` 对明文首包引入「攒够 6 字节再解析」**:这是既有路径的行为变更。当前首包
|
||||
是登录包(远大于 6 字节),实际无影响;极端情况下(<6 字节的合法首包)会多等一轮收包。
|
||||
- **客户端不校验 `Sec-WebSocket-Accept`**:见 §3;明文 WS 下中间人可伪造 101,已在 §8 明确
|
||||
|
||||
Reference in new issue
Block a user