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:
yuanyuanxianganddeepseek-flash committed 2026-10-30 19:15:14 +01:00
1 parent 50a489e316
commit 46f4bf2dfb
12 files changed
+149 -38

No files matched your search

+8 -9
View File
@@ -620,10 +620,10 @@ void ClearAllProtoSwitchKeys(iniFile& cfg)
cfg.SetStr("settings", k, "");
}
// 在每次建连前应用入口覆盖;返回 true 表示本次用的覆盖入口。
// 覆盖撤销(还原命令 / 自动回退)→ 回到安装时的直连入口;
// 新入口超时(entry_try_at 未清零)→ 回退到切换前的入口并撤销覆盖。
bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
// 在每次建连前应用入口覆盖(就地改写 s,并落盘必要的状态)。没有返回值:它既会改 s 也会
// 改配置,用「是否用了覆盖入口」当返回值会与副作用矛盾——自动回退分支照样改写了 settings,
// 却没法用一个 bool 表达,调用方也确实不需要这个信息。
void ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
{
if (!g_bootCaptured) {
g_bootCaptured = true;
@@ -637,7 +637,7 @@ bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
ClearAllProtoSwitchKeys(cfg);
Mprintf("[proto-switch] injected entry changed (%s -> %s), cleared stale switch state\n",
baseHost.c_str(), g_bootHost.c_str());
return false;
return;
}
const std::string proto = cfg.GetStr("settings", "proto_override");
@@ -652,13 +652,13 @@ bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
s.protoType = (char)baseProto;
Mprintf("[proto-switch] restored direct entry %s:%d proto=%d\n", host.c_str(), port, baseProto);
}
return false;
return;
}
const int p = atoi(proto.c_str());
if (!IsSwitchableProto(p)) {
ClearAllProtoSwitchKeys(cfg); // 坏值:撤销覆盖,按注入配置走
return false;
return;
}
// 自动回退:切换后一直没登录成功(entry_try_at 由服务端下发时写入,登录成功后清零)
@@ -686,7 +686,7 @@ bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
Mprintf("[proto-switch] new entry unreachable and no previous entry, override dropped\n");
}
cfg.SetStr("settings", "entry_try_at", "0");
return false;
return;
}
const std::string host = cfg.GetStr("settings", "entry_host");
@@ -694,7 +694,6 @@ bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s)
if (!host.empty() && port > 0)
s.SetServer(host.c_str(), port);
s.protoType = (char)p;
return true;
}
// 新入口登录成功:撤销自动回退计时。入口覆盖本身保留——后续重连一直用它。