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

+32 -3
View File
@@ -70,6 +70,8 @@ public:
}
virtual void Encode(unsigned char* data, int len, unsigned char* param = 0) {}
virtual void Decode(unsigned char* data, int len, unsigned char* param = 0) {}
// 只有 HellEncoder 有可替换的头加密函数;Shine 等实现忽略。
virtual void SetHeaderEnc(EncFun) {}
virtual EncFun GetHeaderEncoder() const
{
return nullptr;
@@ -79,21 +81,27 @@ public:
class HellEncoder : public ProtocolEncoder
{
private:
EncFun m_HeaderEnc;
// 原子量:主连接重连时会在发送线程/接收线程可能同时使用编码器的时机更换头加密函数,
// 只换函数指针(零分配、零释放),读方永远拿到新旧之一,不会 UAF。
std::atomic<EncFun> m_HeaderEnc;
Encoder *m_BodyEnc;
public:
HellEncoder(EncFun head, Encoder *body)
{
m_HeaderEnc = head;
m_HeaderEnc.store(head, std::memory_order_relaxed);
m_BodyEnc = body;
}
~HellEncoder()
{
SAFE_DELETE(m_BodyEnc);
}
virtual void SetHeaderEnc(EncFun enc) override
{
m_HeaderEnc.store(enc, std::memory_order_relaxed);
}
virtual HeaderFlag GetHead() const override
{
return ::GetHead(m_HeaderEnc);
return ::GetHead(m_HeaderEnc.load(std::memory_order_relaxed));
}
virtual int GetHeadLen() const override
{
@@ -292,6 +300,17 @@ public:
m_subConnAuthClientID = clientID;
}
// 标记本对象是主连接。只有 StartClient 里 NewNetClient() 拿到的那一个会调用;
// 只有主连接会在登录被接受后发布头加密类型、并在上一条连接未被登录接受时轮转
// 探测,子连接一律跟随发布值(见 PrepareHeaderEncoderForConnect / PublishHeaderEncoder)。
void MarkMainLink() { m_bMainLink = true; }
// 把当前 HELL 头加密类型发布到 CONNECT_ADDRESS 的共享槽位(仅主连接有效)。
// 必须在服务端**真正接受登录之后**调用(CMD_MASTERSETTING),不能在 ConnectServer
// 返回时就调:握手成功不代表后端解析得了这个头(Go fallback 是读到登录包才拒绝),
// 而且子连接的 ConnectServer 也会走到那里,会用自己乱撞的值覆盖发布值。
void PublishHeaderEncoder();
// 内部:在收到的数据帧分发到 manager 之前,尝试识别并消费 TOKEN_CONN_AUTH ack。
// 仅在我们正在等待 auth 响应时(m_authPending=true)才消费;否则透传给 manager。
bool TryHandleAuthResponse(PBYTE buf, ULONG len);
@@ -334,6 +353,12 @@ public:
virtual VOID Disconnect(); // 函数支持 TCP/UDP
protected:
// 每次建连前调整 HELL 头加密函数:主连接上一条没被登录接受时换下一种类型轮转探测,
// 子连接跟随主连接已发布的类型。必须在建 socket / 启动收发线程之前完成——后端只在
// 每条连接的**第一个**包上识别并锁定头加密方式(Go 首包锁存、认不出即断开),所以
// 换类型只对「下一条连接的首包」生效;这一步同时读上一条连接的 m_loginAcked,
// 因此必须先于 ConnectServer 里的登录标志清零。
void PrepareHeaderEncoderForConnect();
virtual int ReceiveData(char* buffer, int bufSize, int flags)
{
// WebSocket 传输:字节流在 WS 帧里,帧层自己处理 TLS(WSS)或裸 socket(WS)
@@ -428,6 +453,10 @@ protected:
bool m_exit_while_disconnect;
PkgMask* m_masker;
BOOL m_EncoderType;
// HELL 头加密运行态(见 PrepareHeaderEncoderForConnect / PublishHeaderEncoder)
std::atomic<int> m_headerEncIdx{0}; // HeaderEncType 下标(仅 HELL 有意义)
std::atomic<int> m_headerEncAttempts{0}; // ConnectServer 已调用次数
bool m_bMainLink = false; // StartClient 的主连接标记
std::string m_sLocPublicIP;
CONNECT_ADDRESS *m_conn = NULL;