Feature: Add on-demand TLS transport with hot-switch to the Android client

Reuse the Linux OpenSSL TlsChannel implementation (platform guard widened
to __linux__); the trust anchor lives in the app-private filesDir, injected
via TlsSetAnchorDir in nativeInit before the connection thread starts.
COMMAND_TLS_ENABLE/DISABLE save/remove the anchor, hot-reload and reconnect
only when the transport actually changes, so the master menu takes effect
within seconds without restarting the app. Login reports RES_TLS, refreshed
before each re-login. The reconnect flag is consumed before the drop check
and cleared before reconnecting so a switch racing a disconnect cannot
cause an extra reconnect/login cycle.

Cross-compile OpenSSL 3.5.0 per ABI with build_openssl_android.sh (NDK
clang + MSYS2 perl/make), integrated into build_android_libs.ps1
(-OpenSslOnly). Static libs and per-ABI headers are tracked like the
existing libzstd/libsign prebuilts; --exclude-libs keeps OpenSSL symbols
private to libyama.so, isolated from the Java-layer Conscrypt.

Co-Authored-By: Qoder <noreply@qoder.com>
This commit is contained in:
yuanyuanxiangandQoder committed 2026-11-11 11:56:32 +01:00
1 parent 36618b0ad0
commit 51747dc0ea
586 files changed
+183905 -35

No files matched your search

+1 -1
View File
@@ -841,7 +841,7 @@ VOID IOCPClient::OnServerReceiving(CBuffer* m_CompressedBuffer, char* szBuffer,
Mprintf("[ERROR] Unknown header! this=%p bufLen=%lu, first 16 bytes: %s (useTls=%d tlsReady=%d)\n",
this, bufLen, buf.c_str(), (int)m_useTls, (int)m_tls.IsReady());
#else
// macOS/Android 没有 TlsChannel(TLS 只在这两个平台之外的客户端实现)
// macOS 没有 TlsChannel(TLS 已在 Windows/Linux/Android 客户端实现)
Mprintf("[ERROR] Unknown header! this=%p bufLen=%lu, first 16 bytes: %s\n",
this, bufLen, buf.c_str());
#endif
+23 -12
View File
@@ -16,9 +16,11 @@
// Send/Recv 对上下文的使用由 m_ctxMtx 互斥,避免重连线程释放上下文时与使用线程产生
// use-after-free。本对象作为 IOCPClient 的值成员存在,析构时释放上下文。
//
// 平台范围:_WIN32 与 Linux(非 Android)有实现;macOS / Android 不含任何内容,客户端
// 维持明文(TLS 接入按设计文档延后)。两平台都通过 YAMA_TLS_CHANNEL 宏告知调用方
// 「本平台有 TLS 通道」。
// 平台范围:_WIN32(Schannel)、Linux 与 Android(OpenSSL)有实现;macOS 不含任何内容,
// 客户端维持明文。这些平台都通过 YAMA_TLS_CHANNEL 宏告知调用方「本平台有 TLS 通道」。
// Android 复用 Linux 的 OpenSSL 实现(TlsChannelLinux.cpp),唯一差异是信任锚目录改为
// App 私有 filesDir(由 TlsSetAnchorDir 注入),语义(单向 TLS1.2 钉扎、fail-closed、
// 热重载)与 Linux 完全一致。
#pragma once
#if defined(_WIN32)
@@ -133,7 +135,7 @@ private:
std::vector<BYTE> m_pending; // 已解密、未交付给应用层的明文
};
#elif defined(__linux__) && !defined(__ANDROID__)
#elif defined(__linux__) // 含 Android:__ANDROID__ 同时定义 __linux__
#define YAMA_TLS_CHANNEL 1
@@ -150,22 +152,31 @@ bool TlsEnabled();
// (fail-closed),不得静默回退明文。线程安全、幂等。
bool TlsLoadError();
// Windows 版按目标地址区分「用不用内嵌锚」。Linux 客户端没有内嵌锚概念(生成器只往
// Windows 镜像里写锚块),信任锚一律是配置文件,故这里忽略目标地址,与进程级开关等价。
// Windows 版按目标地址区分「用不用内嵌锚」。Linux / Android 客户端没有内嵌锚概念(生成器
// 只往 Windows 镜像里写锚块),信任锚一律是配置文件,故这里忽略目标地址,与进程级开关等价。
inline bool TlsEnabledFor(const std::string&, const std::string&) { return TlsEnabled(); }
inline bool TlsLoadErrorFor(const std::string&, const std::string&) { return TlsLoadError(); }
// 服务端下发信任锚(COMMAND_TLS_ENABLE):把 PEM 正文写入
// $XDG_CONFIG_HOME/ghost/yama-ca.pem(默认 ~/.config/ghost/yama-ca.pem,无 HOME 时退回
// 程序目录),原子替换、权限 0600。写入前先解析验证,不是合法证书就整包拒绝。
// 服务端下发信任锚(COMMAND_TLS_ENABLE):把 PEM 正文写入信任锚文件,原子替换、权限 0600。
// 写入前先解析验证,不是合法证书就整包拒绝。目录按平台:
// Linux:$XDG_CONFIG_HOME/ghost/yama-ca.pem(默认 ~/.config/ghost/yama-ca.pem,无 HOME
// 时退回程序目录);
// Android:App 私有 filesDir/yama-ca.pem(目录由 TlsSetAnchorDir 注入)。
// 落盘后用 TlsReload() 立即生效(Windows 客户端在 COMMAND_TLS_ENABLE 里调用同一个
// TlsReload(),两平台行为一致)。
// TlsReload(),各平台行为一致)。
bool TlsTrustAnchorSave(const std::string& pem, std::string& err);
// 服务端撤销信任锚(COMMAND_TLS_DISABLE):删除上述配置文件(含程序目录同名文件)。
// 返回是否删掉了至少一个文件;之后同样用 TlsReload() 立即生效。
bool TlsTrustAnchorRemove(std::string& err);
#ifdef __ANDROID__
// Android:注入信任锚目录(App 私有 context.filesDir,无需存储权限、跨重启持久)。
// 必须在首次 TlsEnabled()/握手前调用一次(nativeInit 里)。Linux 用 XDG_CONFIG_HOME/HOME
// 定位目录,不需要此函数。设一次后不再变更,故与 TLS 状态锁无竞争。
void TlsSetAnchorDir(const std::string& dir);
#endif
// 按当前信任锚文件重建 TLS 上下文并更新开关,让随后建立的新连接立刻走新传输方式
// (已建立的连接不受影响:SSL 对象自持 SSL_CTX 引用)。信任锚无效时不做任何改动、
// 返回 false 并给出原因——一次坏下发不该把正在工作的客户端打死(下次启动仍 fail-closed)。
@@ -204,7 +215,7 @@ public:
// 握手是否已完成(Send/Recv 可用)。诊断用:区分「本连接已走 TLS」与「裸连接」。
bool IsReady() const { return m_ready; }
// Linux 客户端没有内嵌锚(见上),恒为 false,与 Windows 版签名保持一致。
// Linux / Android 客户端没有内嵌锚(见上),恒为 false,与 Windows 版签名保持一致。
bool AnchorEmbedded() const { return false; }
private:
@@ -221,4 +232,4 @@ private:
mutable std::recursive_mutex m_ctxMtx;
};
#endif // _WIN32 / __linux__ && !__ANDROID__
#endif // _WIN32 / __linux__(含 Android)
+28 -5
View File
@@ -1,4 +1,4 @@
// TlsChannelLinux.cpp — 见 TlsChannel.h 头注释。仅 Linux(非 Android)编译。
// TlsChannelLinux.cpp — 见 TlsChannel.h 头注释。Linux 与 Android 编译(共用 OpenSSL 实现)。
//
// 与 Windows 的 Schannel 实现语义对齐:
// - 单向 TLS 1.2(min=max=1.2,与 SP_PROT_TLS1_2_CLIENT 及两套服务端一致);
@@ -9,7 +9,7 @@
// 与 Windows 实现的差异:OpenSSL 自己管理 TLS 记录缓冲,不需要 m_raw/m_pending;
// 一次性交付不出去的明文留在 SSL 内部,由 SSL_has_pending()/SSL_pending() 暴露给
// 接收线程在 select 前优先消费。
#if defined(__linux__) && !defined(__ANDROID__)
#if defined(__linux__) // 含 Android:__ANDROID__ 同时定义 __linux__
#include "TlsChannel.h"
@@ -49,10 +49,18 @@ bool g_tlsEnabled = false; // TLS 会话可用:send/recv 走加
bool g_tlsError = false; // 信任锚存在但加载失败:拒绝连接(fail-closed)
SSL_CTX* g_tlsCtx = nullptr; // 进程级上下文(线程安全,可被多条连接共享)
std::string g_anchorPath; // 仅用于日志
#ifdef __ANDROID__
std::string g_anchorDir; // 信任锚目录,由 TlsSetAnchorDir 注入(App filesDir)
#endif
// 配置目录:与 LinuxConfig 相同规则($XDG_CONFIG_HOME/ghost 或 ~/.config/ghost)。
// 配置目录:
// Linux:与 LinuxConfig 相同规则($XDG_CONFIG_HOME/ghost 或 ~/.config/ghost);
// Android:App 私有 filesDir(TlsSetAnchorDir 注入),无「程序目录」回退。
std::string ConfigDir()
{
#ifdef __ANDROID__
return g_anchorDir;
#else
const char* xdg = getenv("XDG_CONFIG_HOME");
if (xdg && xdg[0])
return std::string(xdg) + "/ghost";
@@ -60,11 +68,15 @@ std::string ConfigDir()
if (!home || !home[0])
return std::string();
return std::string(home) + "/.config/ghost";
#endif
}
// 可执行文件所在目录(readlink /proc/self/exe,失败返回空)。
// 可执行文件所在目录(readlink /proc/self/exe,失败返回空)。Android 无此概念,返回空。
std::string ExeDir()
{
#ifdef __ANDROID__
return std::string();
#else
char buf[4096];
ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
if (n <= 0)
@@ -75,6 +87,7 @@ std::string ExeDir()
return std::string();
*slash = '\0';
return std::string(buf);
#endif
}
// 信任锚候选路径(按优先级),只返回已经存在的那个:
@@ -266,6 +279,16 @@ void EnsureInitLocked()
} // namespace
#ifdef __ANDROID__
// 注入信任锚目录(App filesDir)。nativeInit 在启动连接线程前调用一次;此后不再变更,
// 故 ConfigDir() 的无锁读取与这里的写入不存在实际竞争(见 TlsChannel.h 契约)。
void TlsSetAnchorDir(const std::string& dir)
{
std::lock_guard<std::mutex> lk(g_stateMtx);
g_anchorDir = dir;
}
#endif
bool TlsEnabled()
{
std::lock_guard<std::mutex> lk(g_stateMtx);
@@ -577,4 +600,4 @@ bool TlsChannel::HasPending() const
return SSL_pending(m_ssl) > 0 || SSL_has_pending(m_ssl) == 1;
}
#endif // __linux__ && !__ANDROID__
#endif // __linux__(含 Android)