Feat(client): report macOS runtime logs to the controller
macOS already handled COMMAND_QUERY_LOG and opened the sub-connection that sends TOKEN_REPORT_LOG, but the snapshot was always empty: macOS Mprintf went to NSLog/fprintf(stderr) and never through Logger, so the ring it dumps stayed empty and both the 运行日志 dialog and MCP get_client_log got nothing. macOS now writes Logger like Linux does, with enable=false so no second log file appears under ~/.config/ghost, and Logger mirrors each line to stderr, which main.mm already redirects to /tmp/ghost.log in daemon and App bundle mode. Prefix and timestamp match on both sides, so dialog and file can be compared line for line. main.mm logged its own lifecycle through NSLog, which never reaches the ring, so the lines people open the dialog for were missing; those calls now use Mprintf. The ObjC helpers (ScreenHandler/H264Encoder/InputHandler/Permissions) keep NSLog and stay file-only. POSIX reports one snapshot per connection, so reopening 运行日志 takes a fresh one; Windows keeps pushing every 3s. Verified with a WSL probe built with -D__APPLE__ (ring, incremental dump, stderr mirror), a MinGW probe for the _WIN32 branch, and a green Linux client rebuild. Not built or run on a real Mac. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
b743de6747
commit
a25e89b220
4 files changed
+102
-38
No files matched your search
+36
-27
@@ -38,6 +38,11 @@
|
||||
#import "../common/logger.h"
|
||||
#import "ClipboardHandler.h"
|
||||
|
||||
// 本文件的运行期日志一律用 Mprintf:它既进内存 ring —— 服务端「运行日志」菜单与
|
||||
// MCP 的 get_client_log 就取这一份 —— 又由 Logger 镜像到 stderr(/tmp/ghost.log)。
|
||||
// NSLog 只会落在文件里、不进 ring,所以启动/配置/权限这些现场最常翻的行不要用
|
||||
// NSLog。(ScreenHandler/H264/InputHandler 等 ObjC 模块里的 NSLog 保持原样。)
|
||||
|
||||
// Global state
|
||||
static std::atomic<bool> g_running(true);
|
||||
static std::atomic<bool> g_needResendLogin(false); // 分组变更后需要重发登录信息
|
||||
@@ -116,14 +121,14 @@ static void saveConfig()
|
||||
|
||||
std::ofstream file(g_configPath, std::ios::trunc);
|
||||
if (!file.is_open()) {
|
||||
NSLog(@"Failed to save config to %s", g_configPath.c_str());
|
||||
Mprintf("Failed to save config to %s\n", g_configPath.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& kv : g_configData) {
|
||||
file << kv.first << "=" << kv.second << "\n";
|
||||
}
|
||||
NSLog(@"Config saved to %s", g_configPath.c_str());
|
||||
Mprintf("Config saved to %s\n", g_configPath.c_str());
|
||||
}
|
||||
|
||||
// Get config string value
|
||||
@@ -144,7 +149,7 @@ static void setConfigStr(const std::string& key, const std::string& value)
|
||||
static void saveGroupName(const std::string& groupName)
|
||||
{
|
||||
setConfigStr("group_name", groupName);
|
||||
NSLog(@"Group name saved: %s", groupName.c_str());
|
||||
Mprintf("Group name saved: %s\n", groupName.c_str());
|
||||
}
|
||||
|
||||
// Load group name from config file
|
||||
@@ -489,7 +494,7 @@ static std::string getInstallTime()
|
||||
installTime = [NSString stringWithUTF8String:currentTime.c_str()];
|
||||
[defaults setObject:installTime forKey:@"ghost_install_time"];
|
||||
[defaults synchronize];
|
||||
NSLog(@"First run - recorded install time: %@", installTime);
|
||||
Mprintf("First run - recorded install time: %s\n", [installTime UTF8String]);
|
||||
}
|
||||
|
||||
return std::string([installTime UTF8String]);
|
||||
@@ -608,8 +613,8 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
std::string normPath = normalizeExePathLower(exePath);
|
||||
std::string idInput = machineId + "|" + normPath;
|
||||
g_myClientID = XXH64(idInput.c_str(), idInput.length(), 0);
|
||||
NSLog(@"ClientID(v2): %llu (machineId=%s, path=%s)",
|
||||
g_myClientID, machineId.c_str(), normPath.c_str());
|
||||
Mprintf("ClientID(v2): %llu (machineId=%s, path=%s)\n",
|
||||
g_myClientID, machineId.c_str(), normPath.c_str());
|
||||
} else {
|
||||
// 老算法兜底
|
||||
char cpuStr[32];
|
||||
@@ -620,7 +625,7 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
cpuStr + "|" +
|
||||
exePath;
|
||||
g_myClientID = XXH64(idInput.c_str(), idInput.length(), 0);
|
||||
NSLog(@"ClientID(v1 fallback): %llu (IOPlatformUUID 读取失败)", g_myClientID);
|
||||
Mprintf("ClientID(v1 fallback): %llu (IOPlatformUUID 读取失败)\n", g_myClientID);
|
||||
}
|
||||
info.AddReserved(std::to_string(g_myClientID).c_str());
|
||||
|
||||
@@ -637,8 +642,8 @@ static void fillLoginInfo(LOGIN_INFOR& info)
|
||||
// 服务端签名输入:与服务端 AddList 处签名格式一致(startTime + "|" + clientID)
|
||||
ClientAuth::g_loginMsg = std::string(info.szStartTime) + "|" + std::to_string(g_myClientID);
|
||||
|
||||
NSLog(@"LOGIN_INFOR filled: OS=%s, Host=%s, CPU=%dMHz, PubIP=%s, ClientID=%llu",
|
||||
osVer.c_str(), hostname.c_str(), info.dwCPUMHz, pubIP.c_str(), g_myClientID);
|
||||
Mprintf("LOGIN_INFOR filled: OS=%s, Host=%s, CPU=%dMHz, PubIP=%s, ClientID=%llu\n",
|
||||
osVer.c_str(), hostname.c_str(), info.dwCPUMHz, pubIP.c_str(), g_myClientID);
|
||||
}
|
||||
|
||||
// [[19] RES_TLS] 是「当前」传输状态的快照,服务端在线列表的 TLS 列就取这个值。
|
||||
@@ -696,6 +701,10 @@ static void setupSignals()
|
||||
}
|
||||
|
||||
// 经典 Unix 双 fork 守护进程
|
||||
//
|
||||
// 必须早于任何 Mprintf:Logger 单例在首次 Mprintf 时创建并启动刷盘线程,fork 之后
|
||||
// 子进程里那条线程并不存在(这里 enable=false 不刷盘,ring 与 stderr 镜像照常,
|
||||
// 但别把第一条 Mprintf 挪到本函数之前)。
|
||||
static void daemonize()
|
||||
{
|
||||
// macOS 10.12+ NSLog 默认只写 os_log(Unified Logging),非 TTY 时不写 stderr。
|
||||
@@ -1014,14 +1023,14 @@ int main(int argc, const char* argv[])
|
||||
}
|
||||
|
||||
@autoreleasepool {
|
||||
NSLog(@"=== macOS Ghost Client%s ===", daemon_mode ? " (daemon)" : "");
|
||||
Mprintf("=== macOS Ghost Client%s ===\n", daemon_mode ? " (daemon)" : "");
|
||||
|
||||
// ============== Power Management: Keep System Awake ==============
|
||||
// 1. Disable App Nap - prevent macOS from suspending this process
|
||||
id<NSObject> powerActivity = [[NSProcessInfo processInfo]
|
||||
beginActivityWithOptions:(NSActivityUserInitiated | NSActivityIdleSystemSleepDisabled)
|
||||
reason:@"Remote control client must maintain persistent connection"];
|
||||
NSLog(@"App Nap disabled, activity token acquired");
|
||||
Mprintf("App Nap disabled, activity token acquired\n");
|
||||
|
||||
// 2. Prevent system idle sleep using IOKit power assertion
|
||||
IOPMAssertionID sleepAssertionID = 0;
|
||||
@@ -1032,9 +1041,9 @@ int main(int argc, const char* argv[])
|
||||
&sleepAssertionID
|
||||
);
|
||||
if (result == kIOReturnSuccess) {
|
||||
NSLog(@"Power assertion created: system idle sleep disabled (ID: %u)", sleepAssertionID);
|
||||
Mprintf("Power assertion created: system idle sleep disabled (ID: %u)\n", sleepAssertionID);
|
||||
} else {
|
||||
NSLog(@"Warning: Failed to create power assertion (error: 0x%x)", result);
|
||||
Mprintf("Warning: Failed to create power assertion (error: 0x%x)\n", result);
|
||||
}
|
||||
|
||||
// 3. Display sleep: managed by ScreenHandler - only prevents display sleep
|
||||
@@ -1045,7 +1054,7 @@ int main(int argc, const char* argv[])
|
||||
|
||||
// Load configuration file (~/.config/ghost/config.conf)
|
||||
loadConfig();
|
||||
NSLog(@"Config loaded from %s", g_configPath.c_str());
|
||||
Mprintf("Config loaded from %s\n", g_configPath.c_str());
|
||||
|
||||
// 传输方式由信任锚文件决定(服务端右键「TLS 传输」才会写入 ~/.config/ghost/yama-ca.pem)。
|
||||
// 启动时打一行,排查「首次连接为什么/为什么不加密」不用去猜。
|
||||
@@ -1057,14 +1066,14 @@ int main(int argc, const char* argv[])
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
NSLog(@"Checking permissions...");
|
||||
Mprintf("Checking permissions...\n");
|
||||
|
||||
bool hasScreenCapture = Permissions::checkScreenCapture();
|
||||
if (hasScreenCapture) {
|
||||
NSLog(@"Screen capture permission: OK");
|
||||
Mprintf("Screen capture permission: OK\n");
|
||||
} else {
|
||||
NSLog(@"Screen capture permission not granted.");
|
||||
NSLog(@"Please grant permission in System Preferences > Privacy & Security > Screen Recording");
|
||||
Mprintf("Screen capture permission not granted.\n");
|
||||
Mprintf("Please grant permission in System Preferences > Privacy & Security > Screen Recording\n");
|
||||
// Request permission (triggers system dialog on first run)
|
||||
Permissions::requestScreenCapture();
|
||||
// Only open settings if this appears to be a re-run without permission
|
||||
@@ -1076,20 +1085,20 @@ int main(int argc, const char* argv[])
|
||||
|
||||
bool hasAccessibility = Permissions::checkAccessibility();
|
||||
if (hasAccessibility) {
|
||||
NSLog(@"Accessibility permission: OK");
|
||||
Mprintf("Accessibility permission: OK\n");
|
||||
} else {
|
||||
NSLog(@"Accessibility permission not granted.");
|
||||
NSLog(@"Please grant permission in System Preferences > Privacy & Security > Accessibility");
|
||||
Mprintf("Accessibility permission not granted.\n");
|
||||
Mprintf("Please grant permission in System Preferences > Privacy & Security > Accessibility\n");
|
||||
Permissions::requestAccessibility();
|
||||
}
|
||||
|
||||
// FDA check is unreliable (no official API), just log a warning
|
||||
if (!Permissions::checkFullDiskAccess()) {
|
||||
NSLog(@"Full Disk Access: not detected (may be false negative).");
|
||||
NSLog(@"If file access issues occur, grant FDA in System Preferences > Privacy & Security > Full Disk Access");
|
||||
Mprintf("Full Disk Access: not detected (may be false negative).\n");
|
||||
Mprintf("If file access issues occur, grant FDA in System Preferences > Privacy & Security > Full Disk Access\n");
|
||||
// Don't auto-open settings since detection is unreliable
|
||||
} else {
|
||||
NSLog(@"Full Disk Access: OK");
|
||||
Mprintf("Full Disk Access: OK\n");
|
||||
}
|
||||
|
||||
// Create client
|
||||
@@ -1104,7 +1113,7 @@ int main(int argc, const char* argv[])
|
||||
}
|
||||
|
||||
// Main event loop
|
||||
NSLog(@"Starting main loop...");
|
||||
Mprintf("Starting main loop...\n");
|
||||
LOGIN_INFOR logInfo;
|
||||
fillLoginInfo(logInfo);
|
||||
|
||||
@@ -1183,12 +1192,12 @@ int main(int argc, const char* argv[])
|
||||
Mprintf(">>> Exit normally (g_bExit=%d)\n", (int)g_bExit);
|
||||
}
|
||||
|
||||
NSLog(@"Shutting down...");
|
||||
Mprintf("Shutting down...\n");
|
||||
|
||||
// Release power assertions
|
||||
if (sleepAssertionID) {
|
||||
IOPMAssertionRelease(sleepAssertionID);
|
||||
NSLog(@"Released sleep assertion");
|
||||
Mprintf("Released sleep assertion\n");
|
||||
}
|
||||
// Display assertion is managed by ScreenHandler (released in stop())
|
||||
// powerActivity is automatically released when exiting @autoreleasepool
|
||||
|
||||
Reference in new issue
Block a user