Files
SimpleRemoter/macos/Permissions.mm
T
yuanyuanxianganddeepseek-flash c2b59f5d4a Feat(client): restart the macOS client once permissions are granted
macOS applies TCC grants per process, so ticking Screen Recording and
Accessibility in System Settings does nothing for a running client.
install.sh resets both with tccutil and re-signs the bundle ad-hoc, and a
rebuilt binary carries a new cdhash, so every update costs two rounds of
"grant one, kill the app, reopen it".

When a permission is missing at startup the client now starts a watcher
that polls CGPreflightScreenCaptureAccess and AXIsProcessTrusted -- only
for the permission that is actually missing, so a screen grant the startup
capture already proved is never re-judged by preflight, whose false
negatives after re-signing would otherwise block the restart forever.

Once everything reads granted the main loop disconnects and hands over to
a fresh process instead of exec'ing in place: TCC re-evaluates by pid, and
the child closes every fd above 2 before exec, so the old main and
sub-connection sockets cannot keep the server holding a dead host. The
parent exits only after the child starts; a child that dies on the spot
(exec failure, AMFI rejecting an un-re-signed binary, dyld abort) keeps
the current process alive instead, and a stop request during the hand-off
terminates it again. GHOST_AUTO_RELAUNCHED bounds this to one auto-restart
per launch, and the new process logs that it was started this way, which
is what the 运行日志 dialog shows.

Not built or run on a Mac yet.

Co-Authored-By: deepseek-flash
2026-11-14 12:52:48 +01:00

134 lines
4.5 KiB
Plaintext

#import "Permissions.h"
#import <Cocoa/Cocoa.h>
#import <CoreGraphics/CoreGraphics.h>
#import <ApplicationServices/ApplicationServices.h>
bool Permissions::checkScreenCapture() {
// macOS 10.15+ requires screen recording permission
if (@available(macOS 10.15, *)) {
// CGPreflightScreenCaptureAccess() is unreliable - it can return false
// even when permission is granted (especially after code re-signing).
// Instead, actually try to capture the screen to verify permission.
CGDirectDisplayID displayID = CGMainDisplayID();
CGImageRef image = CGDisplayCreateImage(displayID);
if (image != NULL) {
// Got an image - permission is granted
// Additional check: verify image has actual content (not blank)
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
CGImageRelease(image);
if (width > 0 && height > 0) {
return true;
}
}
// Failed to capture - permission not granted or display issue
// Fall back to preflight check for triggering dialog
return CGPreflightScreenCaptureAccess();
}
// Before 10.15, no permission needed
return true;
}
bool Permissions::checkScreenCapturePreflight() {
if (@available(macOS 10.15, *)) {
return CGPreflightScreenCaptureAccess();
}
return true;
}
void Permissions::requestScreenCapture() {
if (@available(macOS 10.15, *)) {
// Trigger system permission dialog
CGRequestScreenCaptureAccess();
}
}
bool Permissions::checkAccessibility() {
return AXIsProcessTrusted();
}
void Permissions::requestAccessibility() {
NSDictionary *options = @{
(__bridge id)kAXTrustedCheckOptionPrompt: @YES
};
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
}
void Permissions::openScreenCaptureSettings() {
if (@available(macOS 10.15, *)) {
// Open System Preferences -> Security & Privacy -> Privacy -> Screen Recording
NSURL *url = [NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
}
void Permissions::openAccessibilitySettings() {
// Open System Preferences -> Security & Privacy -> Privacy -> Accessibility
NSURL *url = [NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
bool Permissions::checkFullDiskAccess() {
// There's no official API to check Full Disk Access.
// Try to actually read a protected file that requires FDA.
NSString* testPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/Bookmarks.plist"];
NSFileManager* fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:testPath]) {
// Try to actually read the file (more reliable than isReadableFileAtPath)
NSData* data = [NSData dataWithContentsOfFile:testPath];
if (data != nil) {
NSLog(@"FDA check: OK (can read Safari bookmarks)");
return true;
} else {
NSLog(@"FDA check: FAILED (Safari bookmarks exists but unreadable)");
return false;
}
}
// Safari bookmarks doesn't exist, try TCC database
testPath = @"/Library/Application Support/com.apple.TCC/TCC.db";
if ([fm fileExistsAtPath:testPath]) {
NSData* data = [NSData dataWithContentsOfFile:testPath];
if (data != nil) {
NSLog(@"FDA check: OK (can read TCC.db)");
return true;
} else {
NSLog(@"FDA check: FAILED (TCC.db exists but unreadable)");
return false;
}
}
// No test files exist, assume OK
NSLog(@"FDA check: SKIPPED (no test files found)");
return true;
}
void Permissions::openFullDiskAccessSettings() {
// Open System Preferences -> Security & Privacy -> Privacy -> Full Disk Access
NSURL *url = [NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
bool Permissions::checkAllPermissions() {
return checkScreenCapture() && checkAccessibility() && checkFullDiskAccess();
}
bool Permissions::waitForPermissions(int timeoutSeconds) {
int elapsed = 0;
while (elapsed < timeoutSeconds) {
if (checkAllPermissions()) {
return true;
}
[NSThread sleepForTimeInterval:1.0];
elapsed++;
}
return false;
}