Fix(client): rotate HELL header encoding across reconnects

A Release client drew one of the twelve HELL header encodings in the
constructor and kept it for life. The Go fallback server latches a type
from the first packet and only decodes None..V6, so every client that
drew V7..V10 could never log in to it — a third of them, at random, for
good.

The main connection now advances to the next type when it reconnects
after a connection that was never login-acked (CMD_MASTERSETTING), and
republishes the accepted type into CONNECT_ADDRESS::szFuture. A type that
did log in is kept, so a healthy client no longer churns through values
the fallback would reject. Sub-connections, including one-shot ones
(file/process/screen), inherit the published value at construction and
on every reconnect instead of rolling their own dice. Debug keeps its
None..V6-only range; the initial Release type now comes from
random_device rather than time() % N, which handed a whole batch started
in the same second the same value.

The slot sits at szFuture+16, after LOCAL_TEST_CLIENT_MAGIC, guarded by
static_asserts; it is memory-only and never persisted or sent. Swapping
the encoder is a single atomic function-pointer store, so the send and
receive threads see either the old or the new encoder and never a
dropped object.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-13 16:47:53 +01:00
1 parent a818ace83c
commit 9fadb58c76
5 files changed
+178 -10

No files matched your search

+37
View File
@@ -10,6 +10,7 @@
#include <ctime>
#include <chrono>
#include <climits>
#include <cstddef> // offsetof:CONNECT_ADDRESS::szFuture 运行态槽位的对齐断言
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@@ -1157,6 +1158,42 @@ inline bool IsLocalTestClient(const CONNECT_ADDRESS* conn)
sizeof(LOCAL_TEST_CLIENT_MAGIC) - 1) == 0;
}
// szFuture 前 sizeof(LOCAL_TEST_CLIENT_MAGIC) 字节被上面的本地测试标记占用;其后从
// HEADER_ENC_SELECT_OFFSET 起放一个运行态字段(只在内存里有效,不落盘、不参与分享/
// 转移序列化):
// int 头加密下标 + 1,0 = 尚未发布(烘焙的 EXE 里 szFuture 全 0)
//
// 用途:主连接收到 CMD_MASTERSETTING(登录已被后端接受)后,把当前 HELL 头加密类型写
// 进来;文件/进程/屏幕等子连接在构造和重连时优先继承它,避免自己随机到 Go fallback
// 服务端尚未实现的 HeaderEncV7~V10。该值只当「提示」用:读方必须做范围校验,坏值回退
// 随机(下标越界会读穿 GetHeaderEncoder 的方法表)。
//
// 跨线程:写方是主连接的接收线程(CKernelManager::OnReceive → PublishHeaderEncoder),
// 读方是各子连接自己的线程(构造/重连),这里**不做同步**,也不保证读到最新值——
// 读到旧值等价于「尚未发布」,回退随机,行为正确。前提是该槽位只承载这种提示语义:
// 不要拿它做控制逻辑,也不要放大成多字段协议。目标平台(x86/x64/ARM64)上 4 字节
// 对齐的读写不会撕裂,配合上面的范围校验即可。
#define HEADER_ENC_SELECT_OFFSET sizeof(LOCAL_TEST_CLIENT_MAGIC)
static_assert(HEADER_ENC_SELECT_OFFSET % sizeof(int) == 0, "头加密运行态槽位需 4 字节对齐");
static_assert(sizeof(((CONNECT_ADDRESS*)0)->szFuture) >= HEADER_ENC_SELECT_OFFSET + sizeof(int),
"CONNECT_ADDRESS::szFuture 空间不足");
static_assert(offsetof(CONNECT_ADDRESS, szFuture) % alignof(int) == 0,
"CONNECT_ADDRESS::szFuture 需 4 字节对齐");
inline int GetHeaderEncSelect(const CONNECT_ADDRESS* conn)
{
int value = 0;
if (conn) memcpy(&value, conn->szFuture + HEADER_ENC_SELECT_OFFSET, sizeof(value));
return value;
}
inline void SetHeaderEncSelect(CONNECT_ADDRESS* conn, int index)
{
if (!conn) return;
int value = index + 1;
memcpy(conn->szFuture + HEADER_ENC_SELECT_OFFSET, &value, sizeof(value));
}
#define FOREVER_RUN 2
// 客户端程序线程信息结构体, 包含5个成员: