From 951b4072e5bf333cbeb30071c957693e5d9efa36 Mon Sep 17 00:00:00 2001
From: yuanyuanxiang <962914132@qq.com>
Date: Sun, 1 Nov 2026 18:17:42 +0100
Subject: [PATCH] Feature: Embed the master's TLS certificate in generated
clients
When the server has TLS enabled, the builder can now bake its certificate into
the client it produces, so a distributed client speaks TLS on its very first
connection instead of waiting for the anchor to be pushed after a plaintext
login. The dialog gets a checkbox (IDC_CHECK_TLS_ANCHOR) that is only enabled
for payloads and protocols where an embedded anchor makes sense; the choice is
remembered even when the control flips to disabled and back, so switching
protocols cannot silently undo a deliberate opt-out. Every label variant stays
short enough to fit the 196 DLU field, including the German ones. A template
that predates the anchor block no longer aborts generation: it warns, clears
the checkbox and produces a plain client, so a master upgraded without
rebuilding the ghost/ServerDll resources does not block every build.
The certificate lands in a fixed 4 KiB block (magic, version, length, FNV-1a
hash, master address, PEM) carved out of the payload at build time. The client
reads its trust anchor from four sources in order: the tls_disabled marker,
the certificate the server pushed into the registry, a PEM file next to the
executable, and finally the embedded block.
The embedded block only applies to a connection whose target matches the
master recorded at generation time, compared as an address set: an exact
string match when only one address is configured, or an intersection of
resolved IPv4 addresses otherwise. A transferred, shared or parallel instance
falls back to the old plaintext first-connect path instead of pinning itself
to a certificate it can never verify. That decision is made per connection,
not per process: shared and parallel instances are threads inside a single
client process, so a process-wide flag let whichever instance connected first
decide for all of them, and the first load of the TLS state cached that answer
for good. The TLS layer now keeps two cached states (embedded anchor allowed,
and not allowed), picks one per handshake, and reports the anchor source of
that handshake for the login capability bit. Matching by resolved address also
keeps parallel mode working, where the connection goes to the single IP the
domain pool selected while the configured master is a domain or a list.
Server side, generated and updated payloads get the block written in place;
the client reports the capability bit on login so the console can tell which
hosts are already TLS-only on first connect. Loading the anchor decodes every
PEM block in the chain rather than just the leaf, and an anchor that exists
but fails to load keeps the old fail-closed behaviour. A pushed anchor only
reconnects when the transport of the live connection actually changes, judged
against that connection's own target rather than the process-wide switch.
This was verified end to end against a freshly generated client with an empty
registry: the first connection was a TLS 1.2 ClientHello 0.2 s after start,
and a four-certificate fullchain loaded all four blocks. Re-verified after the
per-connection rework with the anchor block patched into a debug client: with
a matching master the first bytes are a ClientHello, with a foreign master the
client logs the skip and sends plaintext, and a domain master resolves to the
IP it connects to and still matches. A master domain that no longer resolves
falls back to plaintext within a second and is not looked up again for a
minute, instead of stalling every reconnect on a DNS timeout while holding the
lock the certificate verification needs.
Co-Authored-By: deepseek-flash
---
client/ClientDll.cpp | 10 +-
client/ClientDll_vs2015.vcxproj | 2 +
client/ClientDll_vs2015.vcxproj.filters | 2 +
client/EmbeddedTlsAnchor.cpp | 25 +
client/EmbeddedTlsAnchor.h | 18 +
client/IOCPClient.cpp | 23 +-
client/IOCPClient.h | 25 +
client/KernelManager.cpp | 85 +-
client/LoginServer.cpp | 16 +-
client/LoginServer.h | 8 +-
client/TlsChannel.cpp | 529 +++++++++---
client/TlsChannel.h | 43 +-
client/TlsChannelLinux.cpp | 3 +-
client/domain_pool.h | 6 +
client/ghost_vs2015.vcxproj | 2 +
client/ghost_vs2015.vcxproj.filters | 2 +
common/commands.h | 1 +
common/tls_anchor.h | 67 ++
docs/ClientEmbeddedTLS_Design.md | 759 ++++++++++++++++++
server/2015Remote/2015RemoteDlg.cpp | 57 +-
server/2015Remote/2015Remote_vs2015.vcxproj | 2 +
.../2015Remote_vs2015.vcxproj.filters | 2 +
server/2015Remote/BuildDlg.cpp | 114 +++
server/2015Remote/BuildDlg.h | 10 +
server/2015Remote/EmbeddedAnchor.cpp | 146 ++++
server/2015Remote/EmbeddedAnchor.h | 29 +
server/2015Remote/context.h | 10 +
server/2015Remote/lang/de_DE.ini | 12 +
server/2015Remote/lang/en_US.ini | 12 +
server/2015Remote/lang/zh_TW.ini | 12 +
server/2015Remote/resource.h | 1 +
31 files changed, 1911 insertions(+), 122 deletions(-)
create mode 100644 client/EmbeddedTlsAnchor.cpp
create mode 100644 client/EmbeddedTlsAnchor.h
create mode 100644 common/tls_anchor.h
create mode 100644 docs/ClientEmbeddedTLS_Design.md
create mode 100644 server/2015Remote/EmbeddedAnchor.cpp
create mode 100644 server/2015Remote/EmbeddedAnchor.h
diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp
index 4e4da57..113c7ea 100644
--- a/client/ClientDll.cpp
+++ b/client/ClientDll.cpp
@@ -776,6 +776,12 @@ DWORD WINAPI StartClient(LPVOID lParam)
std::string expiredDate;
BOOL isAuthKernel = IsAuthKernel(expiredDate);
if (isAuthKernel) ParseAuthServer(&settings);
+
+ // 内嵌 TLS 信任锚只在「连的正是生成时那台主控」时生效(见设计文档 §3.6)。这个判定
+ // 由 TLS 层在每条连接上自己做(TlsChannel::Handshake / TlsEnabledFor 按目标地址比
+ // IP 集合),不再在这里写全局开关:被转移(assigned)与被分享的实例是**同一进程里
+ // 的线程**,一个进程级布尔量表达不了「主连接用锚、分享连接不用」,谁先连谁说了算。
+
Mprintf("[StartClient] Current client: %s\n", isAuthKernel ? "AUTH" : "NORMAL");
std::string pubIP = cfg.GetStr("settings", "public_ip", "");
// V2 authorization supports offline mode, verify signature and skip timeout check
@@ -815,7 +821,9 @@ DWORD WINAPI StartClient(LPVOID lParam)
SAFE_DELETE(Manager);
//准备第一波数据
- LOGIN_INFOR login = GetLoginInfo(GetTickCount64() - dwTickCount, settings, expiredDate, isAuthKernel);
+ LOGIN_INFOR login = GetLoginInfo(GetTickCount64() - dwTickCount, settings, expiredDate,
+ isAuthKernel, ClientObject->IsTlsActive(),
+ ClientObject->IsTlsAnchorEmbedded());
Manager = isAuthKernel ? new AuthKernelManager(&settings, ClientObject, app.g_hInstance, kb, bExit) :
new CKernelManager(&settings, ClientObject, app.g_hInstance, kb, bExit);
Manager->SetClientApp(&app);
diff --git a/client/ClientDll_vs2015.vcxproj b/client/ClientDll_vs2015.vcxproj
index fa43b98..ede06fd 100644
--- a/client/ClientDll_vs2015.vcxproj
+++ b/client/ClientDll_vs2015.vcxproj
@@ -226,6 +226,7 @@
+
@@ -266,6 +267,7 @@
+
diff --git a/client/ClientDll_vs2015.vcxproj.filters b/client/ClientDll_vs2015.vcxproj.filters
index dfab08c..b13e996 100644
--- a/client/ClientDll_vs2015.vcxproj.filters
+++ b/client/ClientDll_vs2015.vcxproj.filters
@@ -56,6 +56,7 @@
+
@@ -125,6 +126,7 @@
+
diff --git a/client/EmbeddedTlsAnchor.cpp b/client/EmbeddedTlsAnchor.cpp
new file mode 100644
index 0000000..6b9ad48
--- /dev/null
+++ b/client/EmbeddedTlsAnchor.cpp
@@ -0,0 +1,25 @@
+// EmbeddedTlsAnchor.cpp — 内嵌 TLS 信任锚块的定义与读取入口。
+//
+// 本文件必须保持「只有一个全局对象、只被 GetEmbeddedAnchor 读取」,理由见下方定义处注释
+// 与 docs/ClientEmbeddedTLS_Design.md §3.3。请不要把这块合并进 TlsChannel.cpp。
+
+#include "stdafx.h"
+#include "EmbeddedTlsAnchor.h"
+
+// 四条要求缺一不可(设计文档 §3.3 的 LTCG 分析):
+// volatile —— 否则 length 恒为 0,IsAnchorBlockValid 会被内联并折叠成 false,
+// 唯一读取点随之成为死代码,整个对象在 LTO 下被删除;
+// 非 const —— const 更容易被直接常量折叠;
+// 独立 TU —— 让它在任何优化级别下都必须被发射;
+// 非零初值 —— 全零初值会落进 .bss,文件里没有这些字节,生成端扫不到 magic。
+volatile EmbeddedTlsAnchor g_embeddedTlsAnchor = {
+ { 'Y','A','M','A','_','T','L','S','_','A','N','C','H','O','R','\0' },
+ kAnchorVersion, kAnchorCapacity, 0, 0, { 0 }, { 0 }
+};
+
+void GetEmbeddedAnchor(EmbeddedTlsAnchor& out)
+{
+ // 整体拷贝成一份普通对象:调用方不必到处处理 volatile。生成端只改磁盘上的字节,
+ // 运行中的进程里这块数据不会变,因此这里不需要锁。
+ memcpy(&out, (const void*)&g_embeddedTlsAnchor, sizeof(out));
+}
diff --git a/client/EmbeddedTlsAnchor.h b/client/EmbeddedTlsAnchor.h
new file mode 100644
index 0000000..9669612
--- /dev/null
+++ b/client/EmbeddedTlsAnchor.h
@@ -0,0 +1,18 @@
+#pragma once
+//
+// 客户端侧的内嵌 TLS 信任锚块 —— 见 docs/ClientEmbeddedTLS_Design.md §3.3、§3.6。
+//
+#include "common/tls_anchor.h"
+
+// 本 stub 内嵌的锚块。定义在 EmbeddedTlsAnchor.cpp 的**独立编译单元**里:
+// ghost / ClientDll 两个工程都开着 WholeProgramOptimization + MinSpace + COMDAT 折叠,
+// 若把这块定义在调用方 TU 里、且写成 const,唯一读取点会被常量折叠成 false 并随对象
+// 一起被删掉——结果是所有生成都报 NoBlock。定义处的 volatile 是同一道保险。
+extern volatile EmbeddedTlsAnchor g_embeddedTlsAnchor;
+
+// 取一份普通对象的快照(内部负责去掉 volatile)。
+void GetEmbeddedAnchor(EmbeddedTlsAnchor& out);
+
+// 「本连接是否可以使用内嵌锚」不是进程级状态:被转移(assigned)、被分享、并行多主控的
+// 实例都跑在同一个进程里,各自连的主控可能不同。判定在 TlsChannel.cpp 里按每条连接的
+// 目标地址做(EmbeddedAnchorAppliesTo),本文件只负责交出锚块本身。
diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp
index 5f26d93..7f6fcf5 100644
--- a/client/IOCPClient.cpp
+++ b/client/IOCPClient.cpp
@@ -523,13 +523,20 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
bool wsDegraded = false;
#ifdef YAMA_TLS_CHANNEL
- // 单向 TLS:证书存在(TlsEnabled())时,在应用协议首包前完成握手;握手失败即断开,
- // 不回退明文(fail-closed,防降级)。证书存在但加载失败(TlsLoadError())同样拒绝
- // 连接。握手须在 m_bConnected 置位前,确保工作线程不会在握手尚未完成时就开始 recv。
+ // 单向 TLS:本连接有可用信任锚(TlsEnabledFor)时,在应用协议首包前完成握手;握手
+ // 失败即断开,不回退明文(fail-closed,防降级)。信任锚存在但加载失败
+ // (TlsLoadErrorFor)同样拒绝连接。握手须在 m_bConnected 置位前,确保工作线程不会
+ // 在握手尚未完成时就开始 recv。
// m_useTls 在这里一次性锁定:Linux 客户端可能在此后收到服务端下发并热切换传输方式,
// 那条连接已建立,本连接的 Send/Recv 必须继续按握手时的传输方式走。
m_useTls = false;
- if (TlsLoadError()) {
+ // 「这条连接会不会走 TLS」按**本连接的目标**判定:内嵌锚只对生成时那台主控有效,
+ // 分享/并行实例连的是另一台主控,必须跳过内嵌锚改用明文首连 + 对方下发(§3.6)。
+ // 判定在 TLS 层内部完成(地址集合相交),这里只把两个地址传进去:
+ // m_sCurIP 本次实际连到的 IP(DomainPool 解析后选定)
+ // m_Domain.Address() 配置里的原始写法(域名/多地址),域名对域名靠它做快路径
+ const std::string tlsConfigured = m_Domain.GetAddress();
+ if (TlsLoadErrorFor(m_sCurIP, tlsConfigured)) {
Mprintf("[TLS] 证书加载失败,拒绝连接(fail-closed)\n");
closesocket(m_sClientSocket);
m_sClientSocket = INVALID_SOCKET;
@@ -537,11 +544,11 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
}
// PROTO_WS 是显式选择的明文形态(反代的 80 端口、内网直连):即便本客户端配了信任锚
// 也不叠加 TLS,否则该形态永远不可达。WSS 与直连主控端口仍按上面的规则强制 TLS。
- if (TlsEnabled() && protoType != PROTO_WS) {
+ if (TlsEnabledFor(m_sCurIP, tlsConfigured) && protoType != PROTO_WS) {
// SNI:WSS 经反代时要让反代按域名分流到正确的 server 块;直连 IP/主控端口时保持
// 原样(不传名字),与既有行为一致。
const char* sni = (wsTls && wsHostIsDomain) ? wsHost.c_str() : NULL;
- if (!m_tls.Handshake(m_sClientSocket, sni)) {
+ if (!m_tls.Handshake(m_sClientSocket, sni, m_sCurIP, tlsConfigured)) {
Mprintf("[TLS] 握手失败,断开连接(fail-closed)\n");
closesocket(m_sClientSocket);
m_sClientSocket = INVALID_SOCKET;
@@ -552,8 +559,8 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
// 回退(不是降级攻击面):没有信任锚时无法校验反代出示的证书,但此时客户端
// 本来也还没被「颁发」证书——先用明文 WS 把隧道打通,等服务端下发信任锚
// (COMMAND_TLS_ENABLE → TlsReload)后,下一次连接自动走 WSS。
- // 注意:有锚但握手失败、或锚存在却加载失败(上面 TlsLoadError),仍然 fail-closed,
- // 绝不静默降级。
+ // 注意:有锚但握手失败、或锚存在却加载失败(上面 TlsLoadErrorFor),仍然
+ // fail-closed,绝不静默降级。
wsDegraded = true;
Mprintf("[WS] 未配置信任锚,本次按明文 WebSocket 连接;服务端下发证书后重连将升级为 WSS\n");
}
diff --git a/client/IOCPClient.h b/client/IOCPClient.h
index 2595d3d..04c456c 100644
--- a/client/IOCPClient.h
+++ b/client/IOCPClient.h
@@ -304,6 +304,31 @@ public:
void SetLoginAcked(bool v) { m_loginAcked.store(v, std::memory_order_release); }
bool IsLoginAcked() const { return m_loginAcked.load(std::memory_order_acquire); }
+ // 本连接是否真的走 TLS(ConnectServer 握手成功时锁定,见 m_useTls)。登录上报用它,
+ // 而不是再去看全局的 TlsEnabled()——UDP/KCP 客户端各自 override ConnectServer、完全
+ // 不含 TLS,PROTO_WS 是显式明文形态,只有这里才给得出如实答案。
+#if defined(YAMA_TLS_CHANNEL)
+ bool IsTlsActive() const { return m_useTls.load(std::memory_order_acquire); }
+ // 本连接校验服务端证书时用的是不是「生成时内嵌的锚」。登录上报
+ // CLIENT_CAP_EMBEDDED_ANCHOR 用它:分享/并行实例连的是别的主控,本连接并不会用
+ // 内嵌锚,不能因为它进程里有锚块就声称「关服务端 TLS 会失联」。
+ bool IsTlsAnchorEmbedded() const
+ {
+ return m_useTls.load(std::memory_order_acquire) && m_tls.AnchorEmbedded();
+ }
+ // 按**本连接的目标**重算一次传输方式(信任锚热重载后由 KernelManager 调用)。
+ // 不能用进程级 TlsEnabled() 比较前后:分享/并行实例连的不是生成时那台主控,它实际
+ // 在走明文,而进程级开关(「允许内嵌锚」那套状态)仍报 true,会漏掉明文 → TLS 的切换。
+ bool TlsEnabledForCurrentTarget() const
+ {
+ return TlsEnabledFor(m_sCurIP, m_Domain.GetAddress());
+ }
+#else
+ bool IsTlsActive() const { return false; }
+ bool IsTlsAnchorEmbedded() const { return false; }
+ bool TlsEnabledForCurrentTarget() const { return false; }
+#endif
+
// 主动断开当前连接,关闭 socket。提到 public 让外层(如 Linux/macOS main 的心跳
// 循环检测到服务端身份校验超时)能在重连前显式关闭旧 fd,避免泄漏。
virtual VOID Disconnect(); // 函数支持 TCP/UDP
diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp
index 458b349..6ab7550 100644
--- a/client/KernelManager.cpp
+++ b/client/KernelManager.cpp
@@ -24,6 +24,7 @@
#include "common/DateVerify.h"
#include "common/LANChecker.h"
#include "common/scheduler.h"
+#include "common/tls_anchor.h" // 内嵌锚块:COMMAND_UPDATE 判断新镜像有没有带锚
#include "session.h"
extern "C" {
#include "ServiceWrapper.h"
@@ -804,18 +805,60 @@ void ResponseDisable(IOCPClient *client, const char* type, LPBYTE data, int size
extern "C" bool IsSystemInSession0();
+// 「取消 TLS」抑制标记:让客户端压过出厂内嵌锚回到明文(设计文档 §3.5)。
+// 直写注册表而不是 m_cfg->SetStr(""):空 REG_SZ 在 ReadTrustAnchor 里仍算「已配置」,
+// 会被当作 fail-closed 的空锚,与「没有标记」完全不是一回事。
+static void SetTlsDisabledMarker(bool disabled)
+{
+ std::string subKey = GetRegistryName() + "\\settings";
+ HKEY hKey = NULL;
+ if (RegOpenKeyExA(GetCurrentUserRegistryKey(), subKey.c_str(), 0,
+ KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
+ return;
+ if (disabled) {
+ const char* v = "1";
+ RegSetValueExA(hKey, "tls_disabled", 0, REG_SZ, (const BYTE*)v, 2);
+ } else {
+ RegDeleteValueA(hKey, "tls_disabled");
+ }
+ RegCloseKey(hKey);
+}
+
+// 扫一段镜像里有没有「带正文的锚块」。生成端写锚时会连同 .rdata 里的 magic 字面量
+// 一起留在二进制里,所以不能只认第一处命中,必须逐个做结构校验(同 common/tls_anchor.h
+// 的说明)。被 UPX/AES 包过的镜像扫不到——那就按「没带锚」处理,退回原有行为。
+static bool PayloadCarriesAnchor(const BYTE* data, size_t size)
+{
+ if (!data || size < sizeof(EmbeddedTlsAnchor))
+ return false;
+ const size_t last = size - sizeof(EmbeddedTlsAnchor);
+ for (size_t i = 0; i <= last; ++i) {
+ const char* p = (const char*)data + i;
+ if (p[0] != 'Y' || !IsAnchorMagic(p))
+ continue;
+ EmbeddedTlsAnchor block;
+ memcpy(&block, p, sizeof(block)); // 对齐安全:不直接解引用未对齐指针
+ if (IsAnchorBlockValid(block) && block.length > 0)
+ return true;
+ }
+ return false;
+}
+
// 信任锚已写入/删除:热重载 TLS 状态,只在传输方式真正变化(明文 ↔ TLS)时断开重连;
// 仅换证书(仍是 TLS)不打断现有会话,新锚从下一条新连接开始生效。与 Linux 版的
// linux/main.cpp::ApplyTlsAnchorChange 同一语义。
static void ApplyTlsAnchorChange(IOCPClient* client)
{
- const bool before = TlsEnabled();
+ // 前后都用**本连接**的判定:分享/并行实例连的不是生成时那台主控,它实际在走明文,
+ // 而进程级 TlsEnabled() 看的是「允许内嵌锚」那套状态、恒为 true —— 拿它比较会漏掉
+ // 明文 → TLS 的切换,客户端明明收到了锚却继续明文连(设计文档 §3.6)。
+ const bool before = client ? client->IsTlsActive() : TlsEnabled();
std::string err;
if (!TlsReload(err)) {
Mprintf("[TLS] reload failed: %s (keeping current transport)\n", err.c_str());
return;
}
- const bool after = TlsEnabled();
+ const bool after = client ? client->TlsEnabledForCurrentTarget() : TlsEnabled();
if (before == after) {
Mprintf("[TLS] trust anchor updated, transport unchanged (%s)\n",
after ? "TLS" : "plaintext");
@@ -881,9 +924,20 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
// 服务端下发信任锚证书:payload = PEM 正文(szBuffer+1 .. ulLength-1)。
// 写入注册表 settings\tls_cert 后热重载:明文 → TLS 的切换会断开本连接,重连
// 循环随即用 TLS 重新登录(此前按明文回退的 WSS 客户端就此升级为 WSS)。
+ //
+ // 分享实例一律忽略:它和主实例是同一进程里的两个 ClientApp 线程(同一 exe
+ // 路径、同一份配置),注册表键同为 Software\\settings。分享方下发锚
+ // 会改掉主实例连原主控用的 tls_cert,主实例下次重连必然握手失败且无法自愈
+ //(设计文档 §3.8)。与 COMMAND_SET_PROTO 同一策略——被分享的主机无权切换
+ // 自己的传输方式。
+ if (m_ClientApp && m_ClientApp->IsShared()) {
+ Mprintf("[TLS] ignored: shared instance follows the sharing host's anchor\n");
+ break;
+ }
if (ulLength > 1) {
std::string certPem((char*)szBuffer + 1, ulLength - 1);
m_cfg->SetStr("settings", "tls_cert", certPem);
+ SetTlsDisabledMarker(false); // 覆盖掉此前的「取消 TLS」,否则新锚被标记作废
ApplyTlsAnchorChange(m_ClientObject);
} else {
// 没有证书正文就等于「启用但没有锚」——不该悄悄忽略,留一行便于排查。
@@ -893,6 +947,13 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
}
case COMMAND_TLS_DISABLE: {
// 删除注册表信任锚后热重载:TLS → 明文时断开重连(WSS 客户端会回到明文回退)。
+ // 同时写抑制标记:只删 tls_cert 的话,内嵌锚客户端会回落到出厂锚继续发
+ // ClientHello,而服务端已按明文处理 → fail-closed 永久失联(设计文档 §3.5)。
+ if (m_ClientApp && m_ClientApp->IsShared()) {
+ Mprintf("[TLS] ignored: shared instance follows the sharing host's anchor\n");
+ break;
+ }
+ SetTlsDisabledMarker(true);
std::string subKey = GetRegistryName() + "\\settings";
HKEY hKey = NULL;
if (RegOpenKeyExA(GetCurrentUserRegistryKey(), subKey.c_str(), 0,
@@ -946,6 +1007,10 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
std::string certPem((const char*)szBuffer + 1 + sizeof(ProtoSwitchPacket), certLen);
const std::string prevCert = m_cfg->GetStr("settings", "tls_cert");
m_cfg->SetStr("settings", "tls_cert", certPem);
+ // 必须同时清掉抑制标记:留着它 ReadTrustAnchor 会直接判「未配置」,
+ // BuildTlsAnchor 返回 TLS_ANCHOR_NONE,ApplyTlsStateLocked 走成功分支把
+ // g_tlsEnabled 置 false 并返回 true——切换照常进行、WSS 静默降级成明文 WS。
+ SetTlsDisabledMarker(false);
std::string err;
if (!TlsReload(err)) {
// 放弃本次切换(不写 proto_override/entry_*,不断连):PEM 加载不了时客户端
@@ -1589,6 +1654,22 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
case COMMAND_UPDATE: {
auto typ = m_conn->ClientType();
+ // 本次下发的镜像若带着内嵌信任锚,就先清掉本机的「取消 TLS」抑制标记:否则重装/
+ // 更新之后,运维刚内嵌的锚会被上一轮「取消 TLS」留下的标记悄悄作废(§6.5)。
+ // 只在镜像里真有可用锚时才清——反过来一律清除,会让一台已被要求走明文的主机
+ // 重新发 ClientHello,撞上明文服务端就是不可恢复的失联。
+ {
+ ULONGLONG size = 0;
+ if (ulLength > 1 + sizeof(ULONGLONG)) {
+ memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));
+ const size_t avail = (size_t)ulLength - 1 - sizeof(ULONGLONG);
+ if (size > 0 && (ULONGLONG)avail >= size &&
+ PayloadCarriesAnchor(szBuffer + 1 + sizeof(ULONGLONG), (size_t)size)) {
+ SetTlsDisabledMarker(false);
+ Mprintf("[TLS] update payload carries an embedded anchor; cleared tls_disabled\n");
+ }
+ }
+ }
if (typ == CLIENT_TYPE_DLL || typ == CLIENT_TYPE_MODULE) {
ULONGLONG size = 0;
memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));
diff --git a/client/LoginServer.cpp b/client/LoginServer.cpp
index 96794eb..d945a97 100644
--- a/client/LoginServer.cpp
+++ b/client/LoginServer.cpp
@@ -342,7 +342,8 @@ BOOL IsAuthKernel(std::string &str) {
return isAuthKernel;
}
-LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS& conn, const std::string& expiredDate, bool isAuth)
+LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS& conn, const std::string& expiredDate,
+ bool isAuth, bool tlsOnWire, bool tlsAnchorEmbedded)
{
std::string str = expiredDate;
iniFile cfg(CLIENT_PATH);
@@ -473,7 +474,18 @@ LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS& conn, const std::string
double KB = fileInfo.nFileSizeLow > 1024 ? fileInfo.nFileSizeLow / 1024.0 : 0;
sprintf_s(fileSize, "%.1f%s", MB > 0 ? MB : KB, MB > 0 ? "M" : "K");
LoginInfor.AddReserved(fileSize); // 文件大小
- LoginInfor.AddReserved(TlsEnabled() ? 1 : 0); // TLS 启用标志:有信任锚(证书)即 1
+ // TLS 标志:按**本连接实际是否加密**上报,而不是「有没有信任锚」。后者在 UDP/KCP
+ // (各自 override ConnectServer,完全不含 TLS)和 PROTO_WS(显式明文形态)下会误报
+ // (设计文档 §3.7)。
+ LoginInfor.AddReserved(tlsOnWire ? 1 : 0);
+
+ // 本机是不是靠「生成时内嵌的锚」在加密:只有这种情况下,关掉服务端 TLS 才会把它
+ // 锁死(在线可下发取消、离线永久失联,见 §3.5)。服务端据此在「取消 TLS」前提示。
+ // 必须按连接取(IsTlsAnchorEmbedded):分享/并行实例向上线到另一台主控时用的是对方
+ // 下发的锚或明文首连,拿进程级的内嵌锚状态上报会让那台主控误判。
+ if (tlsOnWire && tlsAnchorEmbedded) {
+ LoginInfor.SetCaps(LOGIN_INFOR::DefaultCaps() | CLIENT_CAP_EMBEDDED_ANCHOR);
+ }
return LoginInfor;
}
diff --git a/client/LoginServer.h b/client/LoginServer.h
index 16fb299..14aeb31 100644
--- a/client/LoginServer.h
+++ b/client/LoginServer.h
@@ -9,6 +9,12 @@ std::string GetMachineGuidWindows();
uint64_t CalcalateIDv2(const std::string& machineGuid, const std::string& normalizedPath, bool isAuth = false);
uint64_t RegenerateClientID(uint64_t oldID);
BOOL IsAuthKernel(std::string& str);
-LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS &conn, const std::string& expiredDate, bool isAuth);
+// tlsOnWire:本连接是否真的走了 TLS(由调用方取 IOCPClient::IsTlsActive())。不能在这里
+// 现查 TlsEnabled()——UDP/KCP 根本不走 TLS,PROTO_WS 是显式明文形态,那样会误报加密。
+// tlsAnchorEmbedded:本连接的信任锚是不是「生成时内嵌的」那个(IOCPClient::
+// IsTlsAnchorEmbedded())。同样按连接判定:分享/并行实例连的是别的主控,它们并不用
+// 内嵌锚,不能因为进程里带着锚块就上报「关服务端 TLS 会失联」。
+LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, CONNECT_ADDRESS &conn, const std::string& expiredDate,
+ bool isAuth, bool tlsOnWire, bool tlsAnchorEmbedded);
DWORD CPUClockMHz();
BOOL WebCamIsExist();
diff --git a/client/TlsChannel.cpp b/client/TlsChannel.cpp
index 586bbf2..675a845 100644
--- a/client/TlsChannel.cpp
+++ b/client/TlsChannel.cpp
@@ -3,15 +3,19 @@
#include "stdafx.h"
#include "TlsChannel.h"
+#include "EmbeddedTlsAnchor.h" // 生成客户端时写进镜像的信任锚块(ReadTrustAnchor 第四级)
#include "common/iniFile.h" // config(settings.ini 文件回退) + GetRegistryName/GetCurrentUserRegistryKey(注册表正文)
+#include "domain_pool.h" // GetIPAddress:把内嵌锚的主控地址(可能是域名)解析成 IP
#include // 证书/链引擎 API
#include // RegOpenKeyExA / RegQueryValueExA
+#include // tolower
#include // _access
#include // std::ifstream
#include // std::istreambuf_iterator
#include
+#include