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
402 lines
15 KiB
C++
402 lines
15 KiB
C++
// WsChannel.h — WebSocket 传输层(客户端侧,头文件实现,无需改工程文件)
|
|
//
|
|
// 用途:当出站被限制在 80/443/8080 等少数端口时,客户端经反代(如 nginx 的 443)
|
|
// 以 WebSocket 进入服务端。应用协议完全不变:本层只把字节流套进 WS 帧里,位置与
|
|
// TLS 相同(TLS 之下、应用协议之上)。
|
|
//
|
|
// 两种形态(见 common/commands.h 的 ProtoType):
|
|
// PROTO_WS 裸 WebSocket(反代把明文 upgrade 转发到主控端口)
|
|
// PROTO_WSS WebSocket over TLS(反代终止 TLS;客户端仍用既有信任锚校验证书,
|
|
// 并通过 SNI 让反代按域名分流到正确的 server 块)
|
|
//
|
|
// 线程模型:Handshake 在连接线程上、m_bConnected 置位前调用;之后 Recv 只由接收
|
|
// 线程调用、Send 只由发送线程调用。但 Recv 里要为 ping/close 回写控制帧,收、发两个
|
|
// 线程会同时写 socket,因此写方向必须用 m_sendMtx 串行化,且一帧的头与体要一次写出,
|
|
// 否则两帧会互相穿插、对端解帧错位。TLS 自身的互斥由 TlsChannel 负责。
|
|
#pragma once
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <cstdint>
|
|
#include <ctime>
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#ifdef _WIN32
|
|
#include <WinSock2.h>
|
|
#else
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
// 本头在 IOCPClient.h 里先于 common/commands.h 展开(后者是 Linux 版 SOCKET 的来源),
|
|
// 这里补一份同类型定义;重复 typedef 合法。
|
|
typedef int SOCKET;
|
|
#endif
|
|
#ifndef SOCKET_ERROR
|
|
#define SOCKET_ERROR -1
|
|
#endif
|
|
|
|
#include "TlsChannel.h"
|
|
|
|
class WsChannel
|
|
{
|
|
public:
|
|
WsChannel() {}
|
|
|
|
#ifdef YAMA_TLS_CHANNEL
|
|
// 绑定底层加密通道:WSS 指向 IOCPClient 的 TlsChannel,裸 WS 传 NULL。
|
|
// 设置后本类的所有原始收发都经由它(或直接 send/recv)。
|
|
void SetTls(TlsChannel* tls) { m_tls = tls; }
|
|
#endif
|
|
|
|
// 让 Recv/Send 立刻失效。**可由任意线程调用**(Disconnect 可能来自心跳/命令线程,
|
|
// 不只是接收线程),因此这里只碰原子量;帧解析状态留给 ResetFrames()——它只在
|
|
// Handshake 里、接收线程还没起来时清。
|
|
void Reset()
|
|
{
|
|
m_ready = false;
|
|
}
|
|
|
|
bool IsReady() const { return m_ready; }
|
|
|
|
// 在已连通的 socket 上完成 HTTP Upgrade 握手。host 用于 Host 头(WSS 的 SNI 由
|
|
// TlsChannel::Handshake 负责);path 是 Upgrade 请求的路径。失败时 err 给出原因。
|
|
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());
|
|
else
|
|
snprintf(hostPort, sizeof(hostPort), "%s:%d", host.c_str(), port);
|
|
|
|
// Sec-WebSocket-Key 用固定值:它只用于让中间设备确认这是 WS 握手(服务端会原样
|
|
// 做 SHA1/Base64 回显),不承担任何安全职责;真正的身份由隧道内登录和 TLS 承担。
|
|
std::string req;
|
|
req.reserve(256);
|
|
req += "GET ";
|
|
req += path;
|
|
req += " HTTP/1.1\r\nHost: ";
|
|
req += hostPort;
|
|
req += "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n";
|
|
req += "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n";
|
|
req += "Sec-WebSocket-Version: 13\r\n\r\n";
|
|
|
|
if (!SendAll(sock, req.data(), (int)req.size())) {
|
|
if (err) *err = "send upgrade request failed";
|
|
return false;
|
|
}
|
|
|
|
// 逐字节读响应头,读到空行即停(多读的字节会被当作 WS 帧,而服务端在我们发首包
|
|
// 前不会主动发数据,所以这里读到空行为止是安全的)。
|
|
std::string head;
|
|
bool complete = false;
|
|
char c = 0;
|
|
while (head.size() < 4096) {
|
|
int n = RawRecv(sock, &c, 1);
|
|
if (n <= 0) {
|
|
if (err) *err = "connection closed during upgrade";
|
|
return false;
|
|
}
|
|
head += c;
|
|
if (head.size() >= 4 && head.compare(head.size() - 4, 4, "\r\n\r\n") == 0) {
|
|
complete = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!complete) {
|
|
if (err) *err = "upgrade response head too large";
|
|
return false;
|
|
}
|
|
if (head.compare(0, 12, "HTTP/1.1 101") != 0) {
|
|
if (err) {
|
|
std::string line = head.substr(0, head.find("\r\n"));
|
|
*err = "upgrade rejected: " + line;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
m_ready = true;
|
|
return true;
|
|
}
|
|
|
|
// 读取应用层字节:>0 为读到的字节数;0 表示对端正常关闭;SOCKET_ERROR 表示出错。
|
|
// 与 recv 语义一致,调用方按同样方式处理。
|
|
int Recv(SOCKET sock, char* buf, int len)
|
|
{
|
|
if (!m_ready || len <= 0)
|
|
return SOCKET_ERROR;
|
|
|
|
for (;;) {
|
|
if (m_remaining > 0) {
|
|
int want = (m_remaining < (long long)len) ? (int)m_remaining : len;
|
|
int n = RawRecv(sock, buf, want);
|
|
if (n <= 0)
|
|
return n; // 0 = 对端关闭;SOCKET_ERROR = 出错(TLS 已记录原因)
|
|
if (m_masked) {
|
|
for (int i = 0; i < n; i++) {
|
|
buf[i] ^= (char)m_mask[m_maskPos];
|
|
m_maskPos = (m_maskPos + 1) & 3;
|
|
}
|
|
}
|
|
m_remaining -= n;
|
|
return n;
|
|
}
|
|
|
|
unsigned char opcode = 0;
|
|
long long frameLen = 0;
|
|
if (!ReadHeader(sock, opcode, frameLen))
|
|
return SOCKET_ERROR;
|
|
|
|
if (opcode >= 0x8) {
|
|
// 控制帧:载荷 ≤125 字节,必须在数据帧之间处理
|
|
char ctl[125];
|
|
if (frameLen > (long long)sizeof(ctl))
|
|
return SOCKET_ERROR;
|
|
if (frameLen > 0) {
|
|
if (!ReadExact(sock, ctl, (int)frameLen))
|
|
return SOCKET_ERROR;
|
|
Unmask(ctl, (int)frameLen);
|
|
}
|
|
if (opcode == 0x8) { // close:回一个 close,然后按正常关闭处理
|
|
(void)WriteFrame(sock, 0x8, ctl, (int)frameLen);
|
|
m_ready = false;
|
|
return 0;
|
|
}
|
|
if (opcode == 0x9) { // ping → pong
|
|
if (!WriteFrame(sock, 0xA, ctl, (int)frameLen))
|
|
return SOCKET_ERROR;
|
|
}
|
|
// 0xA (pong) 与其它的直接忽略
|
|
continue;
|
|
}
|
|
|
|
if (opcode != 0x0 && opcode != 0x1 && opcode != 0x2)
|
|
return SOCKET_ERROR;
|
|
if (frameLen == 0)
|
|
continue;
|
|
|
|
m_remaining = frameLen;
|
|
m_maskPos = 0;
|
|
}
|
|
}
|
|
|
|
// 发送应用层字节:全部发出返回 len;否则 SOCKET_ERROR。每包一帧(FIN + binary),
|
|
// 与主控端 wsConn 的「一次 Write 一帧」对齐,保证包边界与发出时一致。
|
|
int Send(SOCKET sock, const char* buf, int len)
|
|
{
|
|
if (!m_ready)
|
|
return SOCKET_ERROR;
|
|
if (len <= 0)
|
|
return 0;
|
|
return WriteFrame(sock, 0x2, buf, len) ? len : SOCKET_ERROR;
|
|
}
|
|
|
|
// 是否有已收到但尚未交付的应用层字节。两处来源:帧载荷只解析了一半(m_remaining),
|
|
// 或 TLS 会话内部还压着已解密明文(WSS)。接收线程在 select 之前要优先消费,
|
|
// 否则 select 看不到这些字节会被 2 秒超时卡住。
|
|
bool HasPending() const
|
|
{
|
|
if (m_remaining > 0)
|
|
return true;
|
|
#ifdef YAMA_TLS_CHANNEL
|
|
if (m_tls)
|
|
return m_tls->HasPending();
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
// ---- 原始收发:有 TLS 会话时全程走 TLS,否则裸 socket ----
|
|
|
|
int RawRecv(SOCKET sock, char* buf, int len)
|
|
{
|
|
#ifdef YAMA_TLS_CHANNEL
|
|
if (m_tls)
|
|
return m_tls->Recv(sock, buf, len);
|
|
#endif
|
|
return ::recv(sock, buf, len, 0);
|
|
}
|
|
|
|
int RawSend(SOCKET sock, const char* buf, int len)
|
|
{
|
|
#ifdef YAMA_TLS_CHANNEL
|
|
if (m_tls)
|
|
return m_tls->Send(sock, buf, len);
|
|
#endif
|
|
return ::send(sock, buf, len, 0);
|
|
}
|
|
|
|
bool SendAll(SOCKET sock, const char* buf, int len)
|
|
{
|
|
int sent = 0;
|
|
while (sent < len) {
|
|
int n = RawSend(sock, buf + sent, len - sent);
|
|
if (n <= 0)
|
|
return false;
|
|
sent += n;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ReadExact(SOCKET sock, void* buf, int len)
|
|
{
|
|
char* p = (char*)buf;
|
|
int got = 0;
|
|
while (got < len) {
|
|
int n = RawRecv(sock, p + got, len - got);
|
|
if (n <= 0)
|
|
return false;
|
|
got += n;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 读帧头(含扩展长度与掩码键)。服务端发来的帧不应带掩码,带了也照样解。
|
|
static const long long kMaxFramePayload = 256LL * 1024 * 1024;
|
|
|
|
bool ReadHeader(SOCKET sock, unsigned char& opcode, long long& frameLen)
|
|
{
|
|
unsigned char h[2] = { 0, 0 };
|
|
if (!ReadExact(sock, h, 2))
|
|
return false;
|
|
if (h[0] & 0x70)
|
|
return false; // 未协商扩展却置了 RSV 位
|
|
|
|
opcode = h[0] & 0x0F;
|
|
m_masked = (h[1] & 0x80) != 0;
|
|
frameLen = h[1] & 0x7F;
|
|
|
|
if (frameLen == 126) {
|
|
unsigned char ext[2];
|
|
if (!ReadExact(sock, ext, 2))
|
|
return false;
|
|
frameLen = ((long long)ext[0] << 8) | ext[1];
|
|
} else if (frameLen == 127) {
|
|
unsigned char ext[8];
|
|
if (!ReadExact(sock, ext, 8))
|
|
return false;
|
|
frameLen = 0;
|
|
for (int i = 0; i < 8; i++)
|
|
frameLen = (frameLen << 8) | ext[i];
|
|
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;
|
|
return true;
|
|
}
|
|
|
|
void Unmask(char* buf, int len)
|
|
{
|
|
if (!m_masked)
|
|
return;
|
|
for (int i = 0; i < len; i++) {
|
|
buf[i] ^= (char)m_mask[m_maskPos];
|
|
m_maskPos = (m_maskPos + 1) & 3;
|
|
}
|
|
}
|
|
|
|
// 写一帧。客户端发往服务端的帧必须带掩码(RFC 6455 §5.3)。整帧(帧头 + 掩码后的
|
|
// 载荷)在 m_sendMtx 内一次写出:接收线程可能在同一时刻回 pong/close,若两帧各自
|
|
// 分「头」「体」两次发送就会交错,对端从此解帧错位。
|
|
bool WriteFrame(SOCKET sock, unsigned char opcode, const char* payload, int len)
|
|
{
|
|
unsigned char hdr[14];
|
|
int hn = 2;
|
|
hdr[0] = (unsigned char)(0x80 | opcode);
|
|
if (len < 126) {
|
|
hdr[1] = (unsigned char)(0x80 | len);
|
|
} else if (len <= 0xFFFF) {
|
|
hdr[1] = 0x80 | 126;
|
|
hdr[2] = (unsigned char)((len >> 8) & 0xFF);
|
|
hdr[3] = (unsigned char)(len & 0xFF);
|
|
hn = 4;
|
|
} else {
|
|
hdr[1] = 0x80 | 127;
|
|
unsigned long long v = (unsigned long long)len;
|
|
for (int i = 0; i < 8; i++)
|
|
hdr[2 + i] = (unsigned char)((v >> (56 - 8 * i)) & 0xFF);
|
|
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)((r >> (8 * i)) & 0xFF);
|
|
memcpy(hdr + hn, mask, 4);
|
|
hn += 4;
|
|
|
|
// 复用同一个缓冲区:发送热路径上每帧不再分配一个新 vector。
|
|
m_frame.resize((size_t)hn + (size_t)len);
|
|
char* frame = m_frame.data();
|
|
memcpy(frame, hdr, hn);
|
|
char* dst = frame + hn;
|
|
for (int i = 0; i < len; i++)
|
|
dst[i] = (char)(payload[i] ^ (char)mask[i & 3]);
|
|
|
|
return SendAll(sock, frame, hn + len);
|
|
}
|
|
|
|
#ifdef YAMA_TLS_CHANNEL
|
|
TlsChannel* m_tls = NULL; // WSS 时指向 IOCPClient 的 TLS 会话
|
|
#endif
|
|
// 会话是否就绪。收、发分处两个线程,且 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;
|
|
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 };
|
|
int m_maskPos = 0;
|
|
};
|