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

+49 -4
View File
@@ -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