Feat(client): add TLS transport to the macOS client

macOS shares the Linux transport: TlsChannel.h now dispatches __APPLE__ to
the OpenSSL implementation, so handshake, pinned trust anchor, reload and
removal all come from client/TlsChannelLinux.cpp. Only the platform glue
is new: ConfigDir() follows initConfigPaths (HOME, then getpwuid(), then
/tmp; no XDG) so the anchor sits next to config.conf, and ExeDir() uses
_NSGetExecutablePath.

First connect stays plaintext. The operator turns TLS on from the online
list: COMMAND_TLS_ENABLE carries the anchor PEM, COMMAND_TLS_DISABLE
removes it. macos/main.mm saves the anchor, calls TlsReload() and only
reconnects when the transport actually changed, so re-saving the same
anchor does not drop a live session. RES_TLS is filled from TlsEnabled()
and refreshed on every reconnect; fillLoginInfo clears szReserved first
now, because AddReserved appends and it runs twice on a group change.

CMake builds arm64 only (deployment target 11.0) and links Homebrew
openssl@3 statically when libcrypto.a is present, so the binary carries
no Homebrew path dependency. Not built or run on a Mac yet: the Apple
branch was syntax-checked under WSL and the login refresh probe-tested;
macos/README.txt has the build and verify steps.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-14 11:43:30 +01:00
1 parent 3d87ec6720
commit b743de6747
12 files changed
+298 -38

No files matched your search

+45 -7
View File
@@ -1,4 +1,4 @@
// TlsChannelLinux.cpp — 见 TlsChannel.h 头注释。Linux 与 Android 编译(共用 OpenSSL 实现)。
// TlsChannelLinux.cpp — 见 TlsChannel.h 头注释。Linux / Android / macOS 编译(共用 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__) // 含 Android:__ANDROID__ 同时定义 __linux__
#if defined(__linux__) || defined(__APPLE__) // Android 同时定义 __linux__;macOS 走同一份实现
#include "TlsChannel.h"
@@ -24,6 +24,7 @@
#include <cerrno>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
@@ -37,6 +38,11 @@
#include <sys/types.h>
#include <unistd.h>
#if defined(__APPLE__)
#include <mach-o/dyld.h> // _NSGetExecutablePath
#include <pwd.h> // getpwuid:与 macos/main.mm 的 HOME 兜底保持一致
#endif
namespace {
const char* const kAnchorName = "yama-ca.pem";
@@ -55,27 +61,58 @@ std::string g_anchorDir; // 信任锚目录,由 TlsSetAnchorDir
// 配置目录:
// Linux:与 LinuxConfig 相同规则($XDG_CONFIG_HOME/ghost 或 ~/.config/ghost);
// macOS:固定 ~/.config/ghost(跟随 macos/main.mm 的 config.conf 位置,不吃 XDG_CONFIG_HOME);
// Android:App 私有 filesDir(TlsSetAnchorDir 注入),无「程序目录」回退。
std::string ConfigDir()
{
#ifdef __ANDROID__
return g_anchorDir;
#else
const char* home = getenv("HOME");
#if defined(__APPLE__)
// macOS 不看 XDG_CONFIG_HOME:macos/main.mm 的 initConfigPaths 把 config.conf 固定
// 放在 ~/.config/ghost,信任锚必须跟着它走,否则同一台机器会出现两份配置目录。
// HOME 兜底顺序也与 initConfigPaths 逐字对齐(HOME → getpwuid()->pw_dir → /tmp):
// 手工做成 launchd daemon 时 launchd 不导出 HOME,只查 HOME 会让锚落到「程序目录」,
// 而 config.conf 在 pw_dir 下——升级/重装 app bundle 时锚跟着被删,客户端静默退回明文,
// 而服务端仍以为这台是 TLS。
if (!home || !home[0]) {
struct passwd* pw = getpwuid(getuid());
if (pw && pw->pw_dir && pw->pw_dir[0])
home = pw->pw_dir;
}
if (!home || !home[0])
home = "/tmp";
return std::string(home) + "/.config/ghost";
#else
const char* xdg = getenv("XDG_CONFIG_HOME");
if (xdg && xdg[0])
return std::string(xdg) + "/ghost";
const char* home = getenv("HOME");
if (!home || !home[0])
return std::string();
return std::string(home) + "/.config/ghost";
#endif
#endif
}
// 可执行文件所在目录(readlink /proc/self/exe,失败返回空)。Android 无此概念,返回空。
// 可执行文件所在目录(失败返回空)。仅用作「无 HOME」时的回退位置。Android 无此概念。
std::string ExeDir()
{
#ifdef __ANDROID__
#if defined(__ANDROID__)
return std::string();
#elif defined(__APPLE__)
// macOS 没有 /proc/self/exe。_NSGetExecutablePath 返回的路径可能含符号链接/相对成分,
// 这里只取所在目录、不做规范化——用途只是找同目录的 yama-ca.pem,与 Linux 分支一致。
// 注意 size 是 in/out:缓冲区不足时返回 -1,不能读返回后的内容。
char buf[4096];
uint32_t size = sizeof(buf);
if (_NSGetExecutablePath(buf, &size) != 0)
return std::string();
char* slash = strrchr(buf, '/');
if (!slash)
return std::string();
*slash = '\0';
return std::string(buf);
#else
char buf[4096];
ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
@@ -91,7 +128,8 @@ std::string ExeDir()
}
// 信任锚候选路径(按优先级),只返回已经存在的那个:
// 1) $XDG_CONFIG_HOME/ghost/yama-ca.pem(默认 ~/.config/ghost/yama-ca.pem)
// 1) ConfigDir()/yama-ca.pem(Linux 默认 ~/.config/ghost,macOS 固定 ~/.config/ghost,
// Android 为 App filesDir)
// 2) 可执行文件同目录 yama-ca.pem(兼容手工投递,与 Windows 的 exe 同名文件回退一致)
bool FindAnchorPath(std::string& out)
{
@@ -600,4 +638,4 @@ bool TlsChannel::HasPending() const
return SSL_pending(m_ssl) > 0 || SSL_has_pending(m_ssl) == 1;
}
#endif // __linux__(含 Android)
#endif // __linux__ / __APPLE__(Linux 含 Android;macOS)