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
+49
-4
@@ -4,11 +4,16 @@ project(ghost_macos)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# macOS deployment target
|
||||
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Minimum macOS version")
|
||||
# macOS deployment target。arm64 的底线就是 11.0(Apple Silicon 首发系统),
|
||||
# 与 Homebrew openssl@3 自身的最低版本一致,避免链接时出现 "built for newer" 告警。
|
||||
set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0" CACHE STRING "Minimum macOS version")
|
||||
|
||||
# Universal Binary (Intel + Apple Silicon)
|
||||
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Build architectures")
|
||||
# 只构建 Apple Silicon。仓库里发布过的 macos/ghost 一直是 arm64-only(universal 设置从未
|
||||
# 真正落地),且 Homebrew 的 OpenSSL 是单架构——保留 x86_64 slice 会让链接直接失败。
|
||||
# 将来真要支持 Intel Mac,应单独用 x86_64 的 OpenSSL 构建一份,而不是把这里改回 universal。
|
||||
# 注意 CMAKE_OSX_ARCHITECTURES 是 cache 变量:旧 build 目录里存的值不会因这里改字面量而
|
||||
# 更新,build.sh 因此显式传 -DCMAKE_OSX_ARCHITECTURES=arm64。
|
||||
set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures (arm64 only)")
|
||||
|
||||
include_directories(../)
|
||||
include_directories(../client)
|
||||
@@ -19,6 +24,9 @@ set(SOURCES
|
||||
main.mm
|
||||
../client/Buffer.cpp
|
||||
../client/IOCPClient.cpp
|
||||
# TLS 传输(OpenSSL 实现,与 Linux/Android 共用)。文件名叫 Linux 是历史原因,
|
||||
# 平台守卫为 __linux__ || __APPLE__;macOS 的差异只有 ExeDir() 用 _NSGetExecutablePath。
|
||||
../client/TlsChannelLinux.cpp
|
||||
../client/sign_shim_unix.cpp
|
||||
ScreenHandler.mm
|
||||
InputHandler.mm
|
||||
@@ -49,6 +57,41 @@ find_library(COREVIDEO_FRAMEWORK CoreVideo REQUIRED)
|
||||
find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED)
|
||||
find_library(ICONV_LIBRARY iconv REQUIRED)
|
||||
|
||||
# ---- OpenSSL(TLS 传输,见 client/TlsChannelLinux.cpp)----
|
||||
# macOS 不自带 OpenSSL。优先用 Homebrew 的 openssl@3;也可显式指定
|
||||
# cmake .. -DOPENSSL_ROOT_DIR=/path/to/openssl
|
||||
# Apple Silicon 的 Homebrew 前缀是 /opt/homebrew,Intel 是 /usr/local。
|
||||
if(NOT OPENSSL_ROOT_DIR)
|
||||
foreach(_openssl_candidate
|
||||
/opt/homebrew/opt/openssl@3
|
||||
/usr/local/opt/openssl@3
|
||||
/opt/homebrew/opt/openssl
|
||||
/usr/local/opt/openssl)
|
||||
if(EXISTS "${_openssl_candidate}/include/openssl/ssl.h")
|
||||
set(OPENSSL_ROOT_DIR "${_openssl_candidate}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
if(NOT OPENSSL_ROOT_DIR)
|
||||
message(FATAL_ERROR
|
||||
"找不到 OpenSSL。先执行: brew install openssl@3\n"
|
||||
"或显式指定: cmake .. -DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3")
|
||||
endif()
|
||||
|
||||
# 有静态库就静态链接:产物保持单文件,install.sh 不需要打包 dylib 或修 @rpath。
|
||||
# Homebrew 的 openssl@3 同时装 .a 和 .dylib;只有动态库时这里退化为动态链接并告警,
|
||||
# 那种情况下发布前必须把 libssl/libcrypto dylib 一并放进 .app 的 Contents/Frameworks。
|
||||
if(EXISTS "${OPENSSL_ROOT_DIR}/lib/libssl.a" AND EXISTS "${OPENSSL_ROOT_DIR}/lib/libcrypto.a")
|
||||
set(OPENSSL_USE_STATIC_LIBS TRUE)
|
||||
else()
|
||||
message(WARNING
|
||||
"OpenSSL 静态库不存在(${OPENSSL_ROOT_DIR}/lib/libssl.a),将动态链接;"
|
||||
"发布前请确认目标 Mac 装有同一份 OpenSSL,或把 dylib 放进 Contents/Frameworks。")
|
||||
endif()
|
||||
find_package(OpenSSL REQUIRED)
|
||||
message(STATUS "OpenSSL: ${OPENSSL_VERSION} (${OPENSSL_INCLUDE_DIR})")
|
||||
|
||||
target_link_libraries(ghost PRIVATE
|
||||
${COCOA_FRAMEWORK}
|
||||
${COREGRAPHICS_FRAMEWORK}
|
||||
@@ -68,6 +111,8 @@ target_link_libraries(ghost PRIVATE
|
||||
# libsign.a 内部使用 macOS CommonCrypto(HMAC-SHA256),CCHmac 在 libSystem
|
||||
# 中已被 Cocoa/CoreFoundation 等链接自动引入,故此处无需额外 framework
|
||||
"${CMAKE_SOURCE_DIR}/lib/libsign.a"
|
||||
OpenSSL::SSL
|
||||
OpenSSL::Crypto
|
||||
)
|
||||
|
||||
# Compiler flags
|
||||
|
||||
+27
-1
@@ -4,6 +4,12 @@ macOS Remote Desktop Client
|
||||
Prerequisites:
|
||||
1. Xcode Command Line Tools: xcode-select --install
|
||||
2. CMake: brew install cmake
|
||||
3. OpenSSL (TLS transport): brew install openssl@3
|
||||
CMake 自动探测 /opt/homebrew/opt/openssl@3,也可用
|
||||
cmake .. -DOPENSSL_ROOT_DIR=/path/to/openssl 指定。
|
||||
|
||||
构建目标是 Apple Silicon (arm64) 单架构:Homebrew 的 OpenSSL 只有本机架构,
|
||||
保留 x86_64 slice 会链接失败。Intel Mac 需另建 x86_64 的 OpenSSL 单独构建。
|
||||
|
||||
Build:
|
||||
chmod +x build.sh
|
||||
@@ -11,7 +17,7 @@ Build:
|
||||
|
||||
Or manually:
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
cmake .. -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
|
||||
make
|
||||
|
||||
Run:
|
||||
@@ -21,6 +27,26 @@ Configuration:
|
||||
Server address is configured in main.mm (g_SETTINGS variable).
|
||||
Modify before building if needed.
|
||||
|
||||
TLS Transport:
|
||||
Default is plaintext on first connect. The controller's host right-click menu
|
||||
("TLS 传输") sends the trust anchor; the client stores it at
|
||||
~/.config/ghost/yama-ca.pem, hot-reloads and reconnects over TLS 1.2
|
||||
(fail-closed, pinned to that anchor). "取消TLS" deletes the file and
|
||||
reconnects in plaintext. Existing sessions survive only if the transport does
|
||||
not change (replacing the certificate keeps TLS).
|
||||
|
||||
Verify after building:
|
||||
otool -L build/bin/ghost | grep -i ssl # 期望无输出:OpenSSL 已静态链接
|
||||
tail -f /tmp/ghost.log
|
||||
Startup prints one of:
|
||||
[TLS] transport on first connect: plaintext (no trust anchor)
|
||||
[TLS] transport on first connect: TLS (trust anchor present)
|
||||
Clicking "TLS 传输" should log "TLS transport switched to TLS, reconnecting";
|
||||
the host's TLS column in the online list then shows Y. "取消TLS" logs
|
||||
"switched to plaintext" and the column flips back to N.
|
||||
If the log says "trust anchor exists but failed to load", the client refuses
|
||||
to connect (fail-closed) until the anchor is fixed or removed.
|
||||
|
||||
Permissions Required:
|
||||
1. Screen Recording - System Settings > Privacy & Security > Screen Recording
|
||||
2. Accessibility - System Settings > Privacy & Security > Accessibility
|
||||
|
||||
+6
-1
@@ -27,8 +27,13 @@ mkdir -p build
|
||||
cd build
|
||||
|
||||
# Configure
|
||||
# 架构与最低系统版本显式传:两者都是 CMake 的 cache 变量,旧 build 目录里可能还存着
|
||||
# universal / 10.13 的旧值,只改 CMakeLists.txt 不会生效(arm64 + Homebrew OpenSSL
|
||||
# 是单架构组合,留着 x86_64 slice 会链接失败)。
|
||||
echo "Configuring..."
|
||||
cmake .. -DCMAKE_BUILD_TYPE=Release
|
||||
cmake .. -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_OSX_ARCHITECTURES=arm64 \
|
||||
-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
|
||||
|
||||
# Build
|
||||
echo ""
|
||||
|
||||
+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