diff options
Diffstat (limited to 'Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m')
-rw-r--r-- | Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m b/Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m index 8d074d9..1c37daf 100644 --- a/Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m +++ b/Sources/WireGuardApp/UI/macOS/LoginItemHelper/main.m @@ -5,13 +5,32 @@ int main(int argc, char *argv[]) { - NSString *appIdInfoDictionaryKey = @"com.wireguard.macos.app_id"; - NSString *appId = [NSBundle.mainBundle objectForInfoDictionaryKey:appIdInfoDictionaryKey]; - - NSString *launchCode = @"LaunchedByWireGuardLoginItemHelper"; - NSAppleEventDescriptor *paramDescriptor = [NSAppleEventDescriptor descriptorWithString:launchCode]; - - [NSWorkspace.sharedWorkspace launchAppWithBundleIdentifier:appId options:NSWorkspaceLaunchWithoutActivation - additionalEventParamDescriptor:paramDescriptor launchIdentifier:NULL]; + NSString *appId = [NSBundle.mainBundle objectForInfoDictionaryKey:@"com.wireguard.macos.app_id"]; + NSString *appGroupId = [NSBundle.mainBundle objectForInfoDictionaryKey:@"com.wireguard.macos.app_group_id"]; + if (!appId || !appGroupId) + return 1; + NSURL *containerUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupId]; + if (!containerUrl) + return 2; + uint64_t now = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); + if (![[NSData dataWithBytes:&now length:sizeof(now)] writeToURL:[containerUrl URLByAppendingPathComponent:@"login-helper-timestamp.bin"] atomically:YES]) + return 3; + if (@available(macOS 10.15, *)) { + NSCondition *condition = [[NSCondition alloc] init]; + NSURL *appURL = [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:appId]; + if (!appURL) + return 4; + NSWorkspaceOpenConfiguration *openConfiguration = [NSWorkspaceOpenConfiguration configuration]; + openConfiguration.activates = NO; + openConfiguration.addsToRecentItems = NO; + openConfiguration.hides = YES; + [NSWorkspace.sharedWorkspace openApplicationAtURL:appURL configuration:openConfiguration completionHandler:^(NSRunningApplication * _Nullable app, NSError * _Nullable error) { + [condition signal]; + }]; + [condition wait]; + } else { + [NSWorkspace.sharedWorkspace launchAppWithBundleIdentifier:appId options:NSWorkspaceLaunchWithoutActivation + additionalEventParamDescriptor:NULL launchIdentifier:NULL]; + } return 0; } |