aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift')
-rw-r--r--Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift19
1 files changed, 19 insertions, 0 deletions
diff --git a/Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift b/Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift
new file mode 100644
index 0000000..158e464
--- /dev/null
+++ b/Sources/WireGuardApp/UI/macOS/LaunchedAtLoginDetector.swift
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018-2023 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class LaunchedAtLoginDetector {
+ static func isLaunchedAtLogin(openAppleEvent: NSAppleEventDescriptor) -> Bool {
+ let now = clock_gettime_nsec_np(CLOCK_UPTIME_RAW)
+ guard openAppleEvent.eventClass == kCoreEventClass && openAppleEvent.eventID == kAEOpenApplication else { return false }
+ guard let url = FileManager.loginHelperTimestampURL else { return false }
+ guard let data = try? Data(contentsOf: url) else { return false }
+ _ = FileManager.deleteFile(at: url)
+ guard data.count == 8 else { return false }
+ let then = data.withUnsafeBytes { ptr in
+ ptr.load(as: UInt64.self)
+ }
+ return now - then <= 20000000000
+ }
+}