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
@@ -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;
|
||||
}
|
||||
|
||||
// 新入口登录成功:撤销自动回退计时。入口覆盖本身保留——后续重连一直用它。
|
||||
|
||||
@@ -496,7 +496,15 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
// WebSocket 传输(PROTO_WS / PROTO_WSS):出站被限制在 80/443/8080 等端口时,经反代
|
||||
// 分流进来。与应用协议无关,位置和 TLS 相同(TLS 之下、应用协议之上);须在
|
||||
// m_bConnected 置位前完成,确保工作线程不会在 Upgrade 尚未完成时就开始 recv。
|
||||
const int protoType = m_conn ? (int)(unsigned char)m_conn->protoType : -1;
|
||||
// Linux / macOS / Android 不传 conn,协议由 SetDefaultProto 给出(见 IOCPClient.h)。
|
||||
// 两边都没有时按明文直连,并留一行日志——否则「改成 WS 却毫无反应」无从排查。
|
||||
const int protoType = m_conn ? (int)(unsigned char)m_conn->protoType : m_defaultProto;
|
||||
if (!m_conn && m_defaultProto < 0) {
|
||||
// 只记一次:重连循环每轮都会走到这里,否则日志会被刷满。
|
||||
static std::atomic<bool> logged{false};
|
||||
if (!logged.exchange(true))
|
||||
Mprintf("[WS] 无连接配置(m_conn 为空)且未设置默认协议,本次按明文 TCP 直连\n");
|
||||
}
|
||||
const bool wsProto = (protoType == PROTO_WS || protoType == PROTO_WSS);
|
||||
const bool wsTls = (protoType == PROTO_WSS);
|
||||
m_useWs = false;
|
||||
|
||||
@@ -208,6 +208,12 @@ public:
|
||||
m_nHostPort = uPort;
|
||||
}
|
||||
|
||||
// 没有 conn 的构造路径(Linux / macOS / Android 的 main 都是 new IOCPClient(exit, false))
|
||||
// 用它给出传输协议:直接取外壳注入的 CONNECT_ADDRESS.protoType。不调用则保持 -1,
|
||||
// ConnectServer 按明文 TCP 直连——那正是「配置成 WS 却毫无反应」的来源,故三个平台
|
||||
// 的 main 都要在进入重连循环前调一次。
|
||||
void SetDefaultProto(int p) { m_defaultProto = p; }
|
||||
|
||||
std::string ServerIP() const
|
||||
{
|
||||
return m_sCurIP;
|
||||
@@ -355,6 +361,8 @@ protected:
|
||||
// 无害,但换成别的架构就不能再依赖这个巧合)。
|
||||
std::atomic<bool> m_useWs{false};
|
||||
std::atomic<bool> m_loginAcked{false};
|
||||
// m_conn 为空时的协议来源(见 SetDefaultProto)。-1 = 未设置,按明文 TCP 处理。
|
||||
int m_defaultProto = -1;
|
||||
#ifdef YAMA_TLS_CHANNEL
|
||||
TlsChannel m_tls; // TLS 会话(证书存在时才启用,见 TlsChannel.h)
|
||||
// 本连接是否走 TLS:在 ConnectServer 里握手成功时置位、Disconnect 时清除,Send/Recv
|
||||
|
||||
@@ -305,6 +305,8 @@ bool TlsChannel::VerifyServerCert()
|
||||
return false;
|
||||
|
||||
// 与 TlsReload 互斥:校验期间持有 g_tlsMtx,重建时释放的旧 store/engine 不会正被使用。
|
||||
// 持锁时间很短:建链全是本地操作(DISABLE_AIA 禁联网取中间证书,也不带任何
|
||||
// CERT_CHAIN_REVOCATION_CHECK_* 位),不会因为网络等待而卡住 TlsReload。
|
||||
std::lock_guard<std::mutex> lk(g_tlsMtx);
|
||||
if (!g_tlsStore || !g_tlsEngine) {
|
||||
CertFreeCertificateContext(remote);
|
||||
|
||||
+22
-6
@@ -51,13 +51,12 @@ public:
|
||||
void SetTls(TlsChannel* tls) { m_tls = tls; }
|
||||
#endif
|
||||
|
||||
// 每次连接前清理上一轮的帧状态。
|
||||
// 让 Recv/Send 立刻失效。**可由任意线程调用**(Disconnect 可能来自心跳/命令线程,
|
||||
// 不只是接收线程),因此这里只碰原子量;帧解析状态留给 ResetFrames()——它只在
|
||||
// Handshake 里、接收线程还没起来时清。
|
||||
void Reset()
|
||||
{
|
||||
m_ready = false;
|
||||
m_remaining = 0;
|
||||
m_masked = false;
|
||||
m_maskPos = 0;
|
||||
}
|
||||
|
||||
bool IsReady() const { return m_ready; }
|
||||
@@ -67,7 +66,7 @@ public:
|
||||
bool Handshake(SOCKET sock, const std::string& host, int port, const char* path, std::string* err = NULL)
|
||||
{
|
||||
Reset();
|
||||
|
||||
ResetFrames();
|
||||
char hostPort[120] = { 0 };
|
||||
if (port == 80 || port == 443)
|
||||
snprintf(hostPort, sizeof(hostPort), "%s", host.c_str());
|
||||
@@ -257,6 +256,8 @@ private:
|
||||
}
|
||||
|
||||
// 读帧头(含扩展长度与掩码键)。服务端发来的帧不应带掩码,带了也照样解。
|
||||
static const long long kMaxFramePayload = 256LL * 1024 * 1024;
|
||||
|
||||
bool ReadHeader(SOCKET sock, unsigned char& opcode, long long& frameLen)
|
||||
{
|
||||
unsigned char h[2] = { 0, 0 };
|
||||
@@ -284,6 +285,12 @@ private:
|
||||
if (frameLen < 0)
|
||||
return false;
|
||||
}
|
||||
// 单帧载荷上限,与主控端 ServerWsSession 的 kMaxFramePayload 取同一个值(256 MB)。
|
||||
// 没有上限时,一个错误/被篡改的长度字段就能把 m_remaining 定在天文数字上,此后
|
||||
// 所有字节都被当成该帧的内容吞掉,连接表面上还活着却永远收不到新包——比直接断线
|
||||
// 难查得多。正常业务的最大单包远小于此值,故不会挡住既有功能。
|
||||
if (frameLen > kMaxFramePayload)
|
||||
return false;
|
||||
if (m_masked && !ReadExact(sock, m_mask, 4))
|
||||
return false;
|
||||
m_maskPos = 0;
|
||||
@@ -377,7 +384,16 @@ private:
|
||||
std::mutex m_sendMtx;
|
||||
std::vector<char> m_frame;
|
||||
|
||||
// 当前数据帧的剩余载荷长度与掩码状态(只由接收线程读写,无需加锁)
|
||||
// 当前数据帧的剩余载荷长度与掩码状态(只由接收线程读写,无需加锁)。
|
||||
// 只能在接收线程未运行时清(见 public Reset()):Handshake 在连接线程上、m_bConnected
|
||||
// 置位之前调用,那时接收线程还没开始读 socket。
|
||||
void ResetFrames()
|
||||
{
|
||||
m_remaining = 0;
|
||||
m_masked = false;
|
||||
m_maskPos = 0;
|
||||
}
|
||||
|
||||
long long m_remaining = 0;
|
||||
bool m_masked = false;
|
||||
unsigned char m_mask[4] = { 0, 0, 0, 0 };
|
||||
|
||||
Reference in new issue
Block a user