Fix: Close the gaps the WebSocket transport review found
Three of the findings only bite in deployment. First, the client's TLS and WebSocket handshakes run synchronously on the reconnect thread with no socket timeout, so a peer that accepts TCP and then goes quiet (black-holed proxy upstream, middlebox that only receives) blocks recv() forever and stalls the reconnect loop until the process is restarted. Set a 15 s SO_RCVTIMEO and SO_SNDTIMEO for the handshake window - the same value both server handshake threads use - and clear them once the connection is up so the worker thread's select plus blocking recv keeps working as before. Second, per-IP rate limiting counted the getpeername address before the real device was known. Behind a same-host reverse proxy that address is loopback, which IPWhitelist always trusts, so WS traffic was neither counted nor banned; behind a remote proxy every device shares one bucket, and tripping the threshold disconnected all of them. Count and ban by the forwarded device IP once the upgrade resolves it, mirroring the Go server's limitDirectPeer and handleConnection split. Direct connections keep their accept-time count and are not counted twice. Third, the protocol switch disarmed its five-minute auto-revert as soon as the new entry connected, which only proves TCP/TLS/WS handshaking finished. If login is never accepted there (wrong backend, reverse proxy pointing at some other WS service), the revert could never fire and the client would keep retrying a dead entry. Disarm on the first CMD_MASTERSETTING instead, which the server only sends after adding the host to its online list, and re-check it on every heartbeat so a late one still counts. The smaller items came along: free the handshake parameter when CreateThread fails in both handshake starters, move the WS_Handshaking fail-closed guard ahead of the TLS branch so it also covers WSS (where tlsState is already TLS_Ready), generate the frame mask from a per-channel PRNG inside the send lock instead of an unseeded global rand(), make the transport flags and the WS session's ready flag atomic, drop rather than send unframed application bytes when a WS session has closed, and abort a protocol switch whose PEM fails to load instead of moving to an entry that cannot connect. The Go server now bounds the upgrade sniff even when ReadTimeout is zero, and the design doc records the two-stage limiter, the login-based disarm and the review items that stay as known limits. The protocol switch's input prompt is cut down to "reverse-proxy entry (host:port)" - the label static is only 81 dialog units wide, and the old sentence ran to roughly three times that. The three language files follow: en_US and zh_TW gain the shortened prompt and drop the long key it replaced, the German file gains the protocol-switch block it never had, and its WebSocket tip key is corrected - it had picked up a Go-server clause the source string never carried, so the tip was never translated at all. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
99db28e2e2
commit
50a489e316
12 files changed
+264
-40
No files matched your search
+33
-3
@@ -18,6 +18,9 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -320,13 +323,18 @@ private:
|
||||
hn = 10;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_sendMtx);
|
||||
// 掩码在锁内、用本实例私有的 PRNG 生成:发送线程与接收线程(pong / close)都会
|
||||
// 写帧,锁外取掩码会让两个线程并发推进共享状态;而全局 rand() 更不能用了——
|
||||
// HttpMask 的构造函数里有 srand(time(nullptr)),会在同进程内重置全局序列。
|
||||
// xorshift64 对本场景足够(RFC 6455 §5.3 只要求掩码来自概率性熵源,不承担安全职责)。
|
||||
unsigned long long r = NextMask64();
|
||||
unsigned char mask[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
mask[i] = (unsigned char)(rand() & 0xFF);
|
||||
mask[i] = (unsigned char)((r >> (8 * i)) & 0xFF);
|
||||
memcpy(hdr + hn, mask, 4);
|
||||
hn += 4;
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_sendMtx);
|
||||
// 复用同一个缓冲区:发送热路径上每帧不再分配一个新 vector。
|
||||
m_frame.resize((size_t)hn + (size_t)len);
|
||||
char* frame = m_frame.data();
|
||||
@@ -341,7 +349,29 @@ private:
|
||||
#ifdef YAMA_TLS_CHANNEL
|
||||
TlsChannel* m_tls = NULL; // WSS 时指向 IOCPClient 的 TLS 会话
|
||||
#endif
|
||||
bool m_ready = false;
|
||||
// 会话是否就绪。收、发分处两个线程,且 Disconnect 可能从接收线程侧走 Reset(),
|
||||
// 故用原子量:x86 上裸 bool 实践无害,但换架构就不能再依赖这个巧合。
|
||||
std::atomic<bool> m_ready{false};
|
||||
|
||||
// 写帧掩码用的私有 PRNG(只在 m_sendMtx 内推进)。种子取地址 + 时间,够用即可:
|
||||
// 掩码只用于让帧在链路上不再是固定形态,安全性由 TLS 与隧道内登录承担。
|
||||
unsigned long long NextMask64()
|
||||
{
|
||||
unsigned long long x = m_maskState;
|
||||
if (x == 0) {
|
||||
x = (unsigned long long)(uintptr_t)this;
|
||||
x = x * 0x9E3779B97F4A7C15ULL ^ (unsigned long long)time(nullptr);
|
||||
if (x == 0)
|
||||
x = 0x2545F4914F6CDD1DULL;
|
||||
}
|
||||
// xorshift64*
|
||||
x ^= x >> 12;
|
||||
x ^= x << 25;
|
||||
x ^= x >> 27;
|
||||
m_maskState = x;
|
||||
return x * 0x2545F4914F6CDD1DULL;
|
||||
}
|
||||
unsigned long long m_maskState = 0;
|
||||
|
||||
// 写方向串行化(发送线程与接收线程的 pong/close 都会写帧),以及复用的整帧缓冲。
|
||||
std::mutex m_sendMtx;
|
||||
|
||||
Reference in new issue
Block a user