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
+120
-2
@@ -15,9 +15,12 @@
|
||||
#import <memory>
|
||||
#import <string>
|
||||
#import <map>
|
||||
#import <vector>
|
||||
#import <cstring> // strncpy(RefreshLoginTls 改写 szReserved)
|
||||
#import <system_error>
|
||||
|
||||
#import "../client/IOCPClient.h"
|
||||
#include "../client/TlsChannel.h" // TLS 传输(信任锚保存/热重载),随 IOCPClient 一起定义 YAMA_TLS_CHANNEL
|
||||
#define XXH_INLINE_ALL
|
||||
#include "../common/xxhash.h"
|
||||
#include "../common/rtt_estimator.h"
|
||||
@@ -38,6 +41,10 @@
|
||||
// Global state
|
||||
static std::atomic<bool> g_running(true);
|
||||
static std::atomic<bool> g_needResendLogin(false); // 分组变更后需要重发登录信息
|
||||
// 传输方式热切换(服务端 COMMAND_TLS_ENABLE/DISABLE 改了信任锚)后需要重建连接。
|
||||
// DataProcess 跑在接收线程上,直接关 socket 会和主线程的发送竞争,所以只置标志,
|
||||
// 由主心跳循环择机 Disconnect 并重连(与 linux/main.cpp 的 g_needReconnect 同语义)。
|
||||
static std::atomic<bool> g_needReconnect(false);
|
||||
|
||||
// Client ID (calculated from system info, used by ScreenHandler)
|
||||
uint64_t g_myClientID = 0;
|
||||
@@ -496,6 +503,11 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
// Token is set in constructor
|
||||
info.bToken = TOKEN_LOGIN;
|
||||
|
||||
// 分组变更后会带着同一个 logInfo 再调一次本函数。AddReserved 是「追加」,不清空
|
||||
// 就会把整套保留字段再拼一遍,服务端按 LOGIN_RES 下标取值,看到的是重复/错位的
|
||||
// 字段。这里先把保留区清空,让本函数可重入。
|
||||
info.szReserved[0] = '\0';
|
||||
|
||||
// OS Version
|
||||
std::string osVer = getMacOSVersion();
|
||||
strncpy(info.OsVerInfoEx, osVer.c_str(), sizeof(info.OsVerInfoEx) - 1);
|
||||
@@ -612,11 +624,13 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
}
|
||||
info.AddReserved(std::to_string(g_myClientID).c_str());
|
||||
|
||||
// PID / 文件大小 / TLS:macOS 暂不上报,占位保持位置(服务端按 LOGIN_RES 下标取值,
|
||||
// PID / 文件大小:macOS 暂不上报,占位保持位置(服务端按 LOGIN_RES 下标取值,
|
||||
// 少发一项会让后面所有字段前移)。
|
||||
info.AddReserved("");
|
||||
info.AddReserved("");
|
||||
info.AddReserved("");
|
||||
// [19] RES_TLS:与 Linux 一致,有信任锚即 1。服务端在线列表的 TLS 列取这个值;
|
||||
// 热切换后重连重发登录,务必用 RefreshLoginTls 刷新成当前状态。
|
||||
info.AddReserved(TlsEnabled() ? 1 : 0);
|
||||
// [20] RES_UPTIME:登录时刻的进程已运行秒数,服务端据此算「启动信息」里的时长
|
||||
info.AddReserved((int)getProcessUptimeSeconds());
|
||||
|
||||
@@ -627,6 +641,39 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
osVer.c_str(), hostname.c_str(), info.dwCPUMHz, pubIP.c_str(), g_myClientID);
|
||||
}
|
||||
|
||||
// [[19] RES_TLS] 是「当前」传输状态的快照,服务端在线列表的 TLS 列就取这个值。
|
||||
// logInfo 只在启动时填一次、之后每条连接复用,因此热切换后重连时必须刷新它,
|
||||
// 否则列里会一直显示切换前的状态。szReserved 是按 '|' 位置解析的,这里逐字段
|
||||
// 拆开、只改第 [19] 项再拼回去(不能用 AddReserved,它只会往后追加)。
|
||||
// 只在主线程调用(与 SendLoginInfo 同一线程),避免和接收线程抢同一个结构体。
|
||||
static void RefreshLoginTls(LOGIN_INFOR& info)
|
||||
{
|
||||
std::vector<std::string> fields;
|
||||
std::string s(info.szReserved);
|
||||
size_t pos = 0;
|
||||
for (;;) {
|
||||
size_t bar = s.find('|', pos);
|
||||
if (bar == std::string::npos) {
|
||||
fields.push_back(s.substr(pos));
|
||||
break;
|
||||
}
|
||||
fields.push_back(s.substr(pos, bar - pos));
|
||||
pos = bar + 1;
|
||||
}
|
||||
while (fields.size() <= (size_t)RES_TLS)
|
||||
fields.push_back("");
|
||||
fields[RES_TLS] = TlsEnabled() ? "1" : "0";
|
||||
|
||||
std::string joined;
|
||||
for (size_t i = 0; i < fields.size(); ++i) {
|
||||
if (i)
|
||||
joined += '|';
|
||||
joined += fields[i];
|
||||
}
|
||||
strncpy(info.szReserved, joined.c_str(), sizeof(info.szReserved) - 1);
|
||||
info.szReserved[sizeof(info.szReserved) - 1] = '\0';
|
||||
}
|
||||
|
||||
// ============== Signal Handling ==============
|
||||
|
||||
// 注意:signal handler 内不能调 NSLog/Mprintf(NSLog 走 Foundation 锁,
|
||||
@@ -781,6 +828,28 @@ static void SpawnSubConnWorker(const char* name, void* (*entry)(void*))
|
||||
}
|
||||
}
|
||||
|
||||
// 信任锚已落盘/已删除:热重载 TLS 状态,必要时请求主循环重建连接。
|
||||
// 只在传输方式真正变化(明文↔TLS)时重连;仅换证书(仍是 TLS)不打断现有会话,
|
||||
// 新锚从下一条新连接开始生效。与 linux/main.cpp 的 ApplyTlsAnchorChange 同语义。
|
||||
static void ApplyTlsAnchorChange(void* user)
|
||||
{
|
||||
bool before = TlsEnabled();
|
||||
std::string err;
|
||||
if (!TlsReload(err)) {
|
||||
Mprintf("** [%p] TLS reload failed: %s (keeping current transport) ***\n", user, err.c_str());
|
||||
return;
|
||||
}
|
||||
bool after = TlsEnabled();
|
||||
if (before == after) {
|
||||
Mprintf("** [%p] TLS trust anchor updated, transport unchanged (%s) ***\n",
|
||||
user, after ? "TLS" : "plaintext");
|
||||
return;
|
||||
}
|
||||
g_needReconnect.store(true);
|
||||
Mprintf("** [%p] TLS transport switched to %s, reconnecting ***\n",
|
||||
user, after ? "TLS" : "plaintext");
|
||||
}
|
||||
|
||||
int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
{
|
||||
if (szBuffer == nullptr || ulLength == 0)
|
||||
@@ -888,6 +957,31 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
// 标记需要重发登录信息(让服务端更新分组显示)
|
||||
g_needResendLogin.store(true);
|
||||
Mprintf("** [%p] Group changed to: %s ***\n", user, groupName.c_str());
|
||||
} else if (szBuffer[0] == COMMAND_TLS_ENABLE) {
|
||||
// 服务端下发信任锚证书(载荷与 Windows/Linux 一致:szBuffer+1 起为 PEM 正文)。
|
||||
// 落盘到 ~/.config/ghost/yama-ca.pem 后热重载,传输方式真正变化时由主循环重连。
|
||||
if (ulLength > 1) {
|
||||
std::string pem((char*)szBuffer + 1, ulLength - 1);
|
||||
std::string err;
|
||||
if (TlsTrustAnchorSave(pem, err)) {
|
||||
Mprintf("** [%p] TLS trust anchor received and saved ***\n", user);
|
||||
ApplyTlsAnchorChange(user);
|
||||
} else {
|
||||
Mprintf("** [%p] TLS trust anchor save failed: %s ***\n", user, err.c_str());
|
||||
}
|
||||
} else {
|
||||
// 没有证书正文就等于「启用但没有锚」——不该悄悄忽略,留一行便于排查。
|
||||
Mprintf("** [%p] COMMAND_TLS_ENABLE without a certificate payload, ignored\n", user);
|
||||
}
|
||||
} else if (szBuffer[0] == COMMAND_TLS_DISABLE) {
|
||||
// 撤销信任锚:删除配置文件并热重载,当前连接若是 TLS 就重连回明文。
|
||||
std::string err;
|
||||
if (TlsTrustAnchorRemove(err)) {
|
||||
Mprintf("** [%p] TLS trust anchor removed ***\n", user);
|
||||
ApplyTlsAnchorChange(user);
|
||||
} else {
|
||||
Mprintf("** [%p] TLS trust anchor removal: %s ***\n", user, err.c_str());
|
||||
}
|
||||
} else {
|
||||
Mprintf("** [%p] Received unimplemented command: %d ***\n", user, int(szBuffer[0]));
|
||||
}
|
||||
@@ -953,6 +1047,15 @@ int main(int argc, const char* argv[])
|
||||
loadConfig();
|
||||
NSLog(@"Config loaded from %s", g_configPath.c_str());
|
||||
|
||||
// 传输方式由信任锚文件决定(服务端右键「TLS 传输」才会写入 ~/.config/ghost/yama-ca.pem)。
|
||||
// 启动时打一行,排查「首次连接为什么/为什么不加密」不用去猜。
|
||||
if (TlsLoadError()) {
|
||||
Mprintf("[TLS] trust anchor exists but failed to load; connections will be refused (fail-closed)\n");
|
||||
} else {
|
||||
Mprintf("[TLS] transport on first connect: %s\n",
|
||||
TlsEnabled() ? "TLS (trust anchor present)" : "plaintext (no trust anchor)");
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
NSLog(@"Checking permissions...");
|
||||
|
||||
@@ -1011,9 +1114,15 @@ int main(int argc, const char* argv[])
|
||||
Sleep(5000);
|
||||
continue;
|
||||
}
|
||||
// 上一条连接断掉时若还挂着「切换传输」的请求,这条连接已经是按新信任锚
|
||||
// 建立的了(ConnectServer 现读 TlsEnabled),标志已过时——不清掉会让心跳
|
||||
// 循环刚进去就再断一次,白白抖动一轮。
|
||||
g_needReconnect.store(false);
|
||||
|
||||
// 进入新连接,重置服务端身份校验状态
|
||||
ClientAuth::OnNewConnection();
|
||||
// 本条连接可能是热切换信任锚后重建的:TLS 标志按当前状态重算,再发登录。
|
||||
RefreshLoginTls(logInfo);
|
||||
ClientObject->SendLoginInfo(logInfo.Speed(clock() - c));
|
||||
|
||||
// 心跳保活循环:定时发送心跳包,服务端回复后动态更新 RTT
|
||||
@@ -1028,10 +1137,19 @@ int main(int argc, const char* argv[])
|
||||
// 等待心跳间隔(每秒检查一次退出条件,保证及时响应)
|
||||
int interval = g_heartbeatInterval > 0 ? g_heartbeatInterval : 30;
|
||||
for (int i = 0; i < interval; ++i) {
|
||||
if (g_needReconnect.load(std::memory_order_relaxed))
|
||||
break;
|
||||
if (!ClientObject->IsRunning() || !ClientObject->IsConnected() || g_bExit != S_CLIENT_NORMAL)
|
||||
break;
|
||||
Sleep(1000);
|
||||
}
|
||||
// 信任锚热切换(COMMAND_TLS_ENABLE/DISABLE)会置这个标志:
|
||||
// 断开后由外层循环按新的传输方式重连。
|
||||
if (g_needReconnect.exchange(false)) {
|
||||
Mprintf(">>> Reconnecting to apply the transport change\n");
|
||||
ClientObject->Disconnect();
|
||||
break;
|
||||
}
|
||||
if (!ClientObject->IsRunning() || !ClientObject->IsConnected() || g_bExit != S_CLIENT_NORMAL)
|
||||
break;
|
||||
|
||||
|
||||
Reference in new issue
Block a user