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

+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)