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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-01 20:40:02 +01:00
1 parent c4021e1fc9
commit 951b4072e5
31 files changed
+1911 -122

No files matched your search

+15 -8
View File
@@ -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");
}