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:
1 parent
3d87ec6720
commit
b743de6747
12 files changed
+298
-38
No files matched your search
@@ -733,7 +733,7 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
return FALSE;
|
||||
}
|
||||
m_useWs = true;
|
||||
// 注意:这里不能读 m_useTls——无 TLS 的平台上(macOS/Android)它不存在;
|
||||
// 注意:这里不能读 m_useTls——它只在定义了 YAMA_TLS_CHANNEL 的平台上存在;
|
||||
// 本连接实际走的形态由 wsDegraded 区分:WSS 有锚走 TLS,没锚则明文回退。
|
||||
Mprintf("[WS] WebSocket 已就绪: %s:%d%s%s\n", wsHost.c_str(), (int)port,
|
||||
WS_UPGRADE_PATH, wsDegraded ? " (明文,等待信任锚)"
|
||||
@@ -956,7 +956,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 没有 TlsChannel(TLS 已在 Windows/Linux/Android 客户端实现)
|
||||
// 该平台没有 TlsChannel(YAMA_TLS_CHANNEL 未定义):没有传输层状态可打
|
||||
Mprintf("[ERROR] Unknown header! this=%p bufLen=%lu, first 16 bytes: %s\n",
|
||||
this, bufLen, buf.c_str());
|
||||
#endif
|
||||
|
||||
+2
-2
@@ -11,8 +11,8 @@
|
||||
#pragma comment(lib,"ws2_32.lib")
|
||||
#endif
|
||||
|
||||
// TLS 通道:Windows 走 Schannel,Linux 走 OpenSSL;macOS/Android 下该头为空,
|
||||
// 且不定义 YAMA_TLS_CHANNEL,下方代码自动退回明文。
|
||||
// TLS 通道:Windows 走 Schannel,Linux / Android / macOS 走 OpenSSL(TlsChannelLinux.cpp)。
|
||||
// 其它平台该头为空,且不定义 YAMA_TLS_CHANNEL,下方代码自动退回明文。
|
||||
#include "TlsChannel.h"
|
||||
|
||||
// WebSocket 传输(经反代 443 等受限端口接入)。头文件实现,各平台编译目标无需改动。
|
||||
|
||||
+14
-9
@@ -6,7 +6,8 @@
|
||||
//
|
||||
// 选型:
|
||||
// - Windows:Schannel(原生 SSPI),零第三方依赖;secur32.lib / crypt32.lib 为系统库。
|
||||
// - Linux:OpenSSL(构建链已因 libsign 链接 libcrypto,另加 libssl 即可)。
|
||||
// - Linux / macOS:OpenSSL(Linux 构建链已因 libsign 链接 libcrypto,另加 libssl 即可;
|
||||
// macOS 需 Homebrew openssl@3,见 macos/CMakeLists.txt)。
|
||||
// 两者都是单向 TLS:客户端只校验服务端证书链到固定的信任锚,不提供客户端证书,
|
||||
// 客户端身份仍由隧道内登录(V1 PwdHmac / V2 ECDSA)承担;均不校主机名(客户端按 IP
|
||||
// 连接),不做吊销检查——与 Windows 版既有语义保持一致。
|
||||
@@ -16,11 +17,12 @@
|
||||
// Send/Recv 对上下文的使用由 m_ctxMtx 互斥,避免重连线程释放上下文时与使用线程产生
|
||||
// use-after-free。本对象作为 IOCPClient 的值成员存在,析构时释放上下文。
|
||||
//
|
||||
// 平台范围:_WIN32(Schannel)、Linux 与 Android(OpenSSL)有实现;macOS 不含任何内容,
|
||||
// 客户端维持明文。这些平台都通过 YAMA_TLS_CHANNEL 宏告知调用方「本平台有 TLS 通道」。
|
||||
// 平台范围:_WIN32(Schannel)、Linux / Android / macOS(OpenSSL)有实现。这些平台都通过
|
||||
// YAMA_TLS_CHANNEL 宏告知调用方「本平台有 TLS 通道」。
|
||||
// Android 复用 Linux 的 OpenSSL 实现(TlsChannelLinux.cpp),唯一差异是信任锚目录改为
|
||||
// App 私有 filesDir(由 TlsSetAnchorDir 注入),语义(单向 TLS1.2 钉扎、fail-closed、
|
||||
// 热重载)与 Linux 完全一致。
|
||||
// 热重载)与 Linux 完全一致。macOS 同样复用这份 OpenSSL 实现且不改目录规则:
|
||||
// ~/.config/ghost/yama-ca.pem 与 macOS 客户端现有的 config.conf 目录一致。
|
||||
#pragma once
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -135,7 +137,7 @@ private:
|
||||
std::vector<BYTE> m_pending; // 已解密、未交付给应用层的明文
|
||||
};
|
||||
|
||||
#elif defined(__linux__) // 含 Android:__ANDROID__ 同时定义 __linux__
|
||||
#elif defined(__linux__) || defined(__APPLE__) // Android 同时定义 __linux__;macOS 走同一份 OpenSSL 实现
|
||||
|
||||
#define YAMA_TLS_CHANNEL 1
|
||||
|
||||
@@ -152,15 +154,18 @@ bool TlsEnabled();
|
||||
// (fail-closed),不得静默回退明文。线程安全、幂等。
|
||||
bool TlsLoadError();
|
||||
|
||||
// Windows 版按目标地址区分「用不用内嵌锚」。Linux / Android 客户端没有内嵌锚概念(生成器
|
||||
// 只往 Windows 镜像里写锚块),信任锚一律是配置文件,故这里忽略目标地址,与进程级开关等价。
|
||||
// Windows 版按目标地址区分「用不用内嵌锚」。Linux / Android / macOS 客户端没有内嵌锚概念
|
||||
// (生成器只往 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 正文写入信任锚文件,原子替换、权限 0600。
|
||||
// 写入前先解析验证,不是合法证书就整包拒绝。目录按平台:
|
||||
// Linux:$XDG_CONFIG_HOME/ghost/yama-ca.pem(默认 ~/.config/ghost/yama-ca.pem,无 HOME
|
||||
// 时退回程序目录);
|
||||
// Linux:$XDG_CONFIG_HOME/ghost/yama-ca.pem(默认 ~/.config/ghost/yama-ca.pem);
|
||||
// macOS:~/.config/ghost/yama-ca.pem(与 macos/main.mm 的 config.conf 同目录,不读
|
||||
// XDG_CONFIG_HOME;HOME 兜底也逐字对齐 initConfigPaths:HOME → getpwuid → /tmp);
|
||||
// Linux 无 HOME 时退回程序目录;
|
||||
// Android:App 私有 filesDir/yama-ca.pem(目录由 TlsSetAnchorDir 注入)。
|
||||
// 落盘后用 TlsReload() 立即生效(Windows 客户端在 COMMAND_TLS_ENABLE 里调用同一个
|
||||
// TlsReload(),各平台行为一致)。
|
||||
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user