Files
SimpleRemoter/client/WsChannel.h
T
yuanyuanxianganddeepseek-flash 50a489e316 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
2026-10-30 18:11:34 +01:00

386 lines
14 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
// 每次连接前清理上一轮的帧状态。
void Reset()
{
m_ready = false;
m_remaining = 0;
m_masked = false;
m_maskPos = 0;
}
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();
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;
}
// 读帧头(含扩展长度与掩码键)。服务端发来的帧不应带掩码,带了也照样解。
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;
}
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;
// 当前数据帧的剩余载荷长度与掩码状态(只由接收线程读写,无需加锁)
long long m_remaining = 0;
bool m_masked = false;
unsigned char m_mask[4] = { 0, 0, 0, 0 };
int m_maskPos = 0;
};