2018-12-25 13:03:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
2018-12-25 13:03:28 +00:00
|
|
|
|
|
|
|
import Cocoa
|
2019-02-19 15:13:52 +00:00
|
|
|
import ServiceManagement
|
2018-12-25 13:03:28 +00:00
|
|
|
|
|
|
|
@NSApplicationMain
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
var tunnelsManager: TunnelsManager?
|
|
|
|
var tunnelsTracker: TunnelsTracker?
|
2019-01-15 19:30:42 +00:00
|
|
|
var statusItemController: StatusItemController?
|
2019-01-17 19:57:17 +00:00
|
|
|
|
|
|
|
var manageTunnelsRootVC: ManageTunnelsRootViewController?
|
|
|
|
var manageTunnelsWindowObject: NSWindow?
|
2019-05-22 09:56:38 +00:00
|
|
|
var onAppDeactivation: (() -> Void)?
|
2018-12-28 13:59:09 +00:00
|
|
|
|
2019-05-22 14:08:15 +00:00
|
|
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
|
|
|
// To workaround a possible AppKit bug that causes the main menu to become unresponsive sometimes
|
|
|
|
// (especially when launched through Xcode) if we call setActivationPolicy(.regular) in
|
|
|
|
// in applicationDidFinishLaunching, we set it to .prohibited here.
|
|
|
|
// Setting it to .regular would fix that problem too, but at this point, we don't know
|
|
|
|
// whether the app was launched at login or not, so we're not sure whether we should
|
|
|
|
// show the app icon in the dock or not.
|
|
|
|
NSApp.setActivationPolicy(.prohibited)
|
|
|
|
}
|
|
|
|
|
2018-12-25 13:03:28 +00:00
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
2019-03-17 06:41:10 +00:00
|
|
|
Logger.configureGlobal(tagged: "APP", withFilePath: FileManager.logFileURL?.path)
|
2019-02-19 15:13:52 +00:00
|
|
|
registerLoginItem(shouldLaunchAtLogin: true)
|
2019-05-19 19:54:43 +00:00
|
|
|
|
2019-05-20 08:34:47 +00:00
|
|
|
var isLaunchedAtLogin = false
|
|
|
|
if let appleEvent = NSAppleEventManager.shared().currentAppleEvent {
|
|
|
|
isLaunchedAtLogin = LaunchedAtLoginDetector.isLaunchedAtLogin(openAppleEvent: appleEvent)
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:38:57 +00:00
|
|
|
NSApp.mainMenu = MainMenu()
|
2019-05-22 14:08:15 +00:00
|
|
|
setDockIconAndMainMenuVisibility(isVisible: !isLaunchedAtLogin)
|
2019-01-04 12:41:49 +00:00
|
|
|
|
2018-12-28 13:59:09 +00:00
|
|
|
TunnelsManager.create { [weak self] result in
|
|
|
|
guard let self = self else { return }
|
2019-01-15 19:30:42 +00:00
|
|
|
|
2019-04-08 07:52:06 +00:00
|
|
|
switch result {
|
|
|
|
case .failure(let error):
|
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: nil)
|
|
|
|
case .success(let tunnelsManager):
|
|
|
|
let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
|
|
|
|
statusMenu.windowDelegate = self
|
2019-01-15 19:30:42 +00:00
|
|
|
|
2019-04-08 07:52:06 +00:00
|
|
|
let statusItemController = StatusItemController()
|
|
|
|
statusItemController.statusItem.menu = statusMenu
|
2019-01-17 19:57:17 +00:00
|
|
|
|
2019-04-08 07:52:06 +00:00
|
|
|
let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager)
|
|
|
|
tunnelsTracker.statusMenu = statusMenu
|
|
|
|
tunnelsTracker.statusItemController = statusItemController
|
2019-01-17 19:57:17 +00:00
|
|
|
|
2019-04-08 07:52:06 +00:00
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
self.tunnelsTracker = tunnelsTracker
|
|
|
|
self.statusItemController = statusItemController
|
2019-05-19 19:54:43 +00:00
|
|
|
|
2019-05-20 08:34:47 +00:00
|
|
|
if !isLaunchedAtLogin {
|
|
|
|
self.showManageTunnelsWindow(completion: nil)
|
|
|
|
}
|
2019-04-08 07:52:06 +00:00
|
|
|
}
|
2019-01-17 19:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 11:01:26 +00:00
|
|
|
|
2019-05-22 09:48:50 +00:00
|
|
|
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool {
|
2019-05-22 12:01:05 +00:00
|
|
|
if let appleEvent = NSAppleEventManager.shared().currentAppleEvent {
|
|
|
|
if LaunchedAtLoginDetector.isReopenedByLoginItemHelper(reopenAppleEvent: appleEvent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 09:48:50 +00:00
|
|
|
if hasVisibleWindows {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
showManageTunnelsWindow(completion: nil)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-04 20:47:49 +00:00
|
|
|
@objc func confirmAndQuit() {
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = tr("macConfirmAndQuitAlertMessage")
|
|
|
|
if let currentTunnel = tunnelsTracker?.currentTunnel, currentTunnel.status == .active || currentTunnel.status == .activating {
|
|
|
|
alert.informativeText = tr(format: "macConfirmAndQuitInfoWithActiveTunnel (%@)", currentTunnel.name)
|
|
|
|
} else {
|
|
|
|
alert.informativeText = tr("macConfirmAndQuitAlertInfo")
|
|
|
|
}
|
|
|
|
alert.addButton(withTitle: tr("macConfirmAndQuitAlertCloseWindow"))
|
|
|
|
alert.addButton(withTitle: tr("macConfirmAndQuitAlertQuitWireGuard"))
|
|
|
|
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
if let manageWindow = manageTunnelsWindowObject {
|
|
|
|
manageWindow.orderFront(self)
|
|
|
|
alert.beginSheetModal(for: manageWindow) { response in
|
|
|
|
switch response {
|
|
|
|
case .alertFirstButtonReturn:
|
|
|
|
manageWindow.close()
|
|
|
|
case .alertSecondButtonReturn:
|
|
|
|
NSApp.terminate(nil)
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:11:01 +00:00
|
|
|
@objc func quit() {
|
2019-03-28 18:32:54 +00:00
|
|
|
if let manageWindow = manageTunnelsWindowObject, manageWindow.attachedSheet != nil {
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
manageWindow.orderFront(self)
|
|
|
|
return
|
|
|
|
}
|
2019-02-19 15:13:52 +00:00
|
|
|
registerLoginItem(shouldLaunchAtLogin: false)
|
2019-02-07 21:31:17 +00:00
|
|
|
guard let currentTunnel = tunnelsTracker?.currentTunnel, currentTunnel.status == .active || currentTunnel.status == .activating else {
|
2019-02-21 14:11:01 +00:00
|
|
|
NSApp.terminate(nil)
|
|
|
|
return
|
2019-02-07 21:31:17 +00:00
|
|
|
}
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = tr("macAppExitingWithActiveTunnelMessage")
|
|
|
|
alert.informativeText = tr("macAppExitingWithActiveTunnelInfo")
|
2019-03-28 18:40:05 +00:00
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
if let manageWindow = manageTunnelsWindowObject {
|
|
|
|
manageWindow.orderFront(self)
|
|
|
|
alert.beginSheetModal(for: manageWindow) { _ in
|
2019-02-07 21:31:17 +00:00
|
|
|
NSApp.terminate(nil)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert.runModal()
|
2019-02-21 14:11:01 +00:00
|
|
|
NSApp.terminate(nil)
|
2019-01-23 11:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 08:05:43 +00:00
|
|
|
|
|
|
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
|
|
|
guard let currentTunnel = tunnelsTracker?.currentTunnel, currentTunnel.status == .active || currentTunnel.status == .activating else {
|
|
|
|
return .terminateNow
|
|
|
|
}
|
|
|
|
guard let appleEvent = NSAppleEventManager.shared().currentAppleEvent else {
|
|
|
|
return .terminateNow
|
|
|
|
}
|
|
|
|
guard MacAppStoreUpdateDetector.isUpdatingFromMacAppStore(quitAppleEvent: appleEvent) else {
|
|
|
|
return .terminateNow
|
|
|
|
}
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = tr("macAppStoreUpdatingAlertMessage")
|
|
|
|
if currentTunnel.isActivateOnDemandEnabled {
|
|
|
|
alert.informativeText = tr(format: "macAppStoreUpdatingAlertInfoWithOnDemand (%@)", currentTunnel.name)
|
|
|
|
} else {
|
|
|
|
alert.informativeText = tr(format: "macAppStoreUpdatingAlertInfoWithoutOnDemand (%@)", currentTunnel.name)
|
|
|
|
}
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
if let manageWindow = manageTunnelsWindowObject {
|
|
|
|
alert.beginSheetModal(for: manageWindow) { _ in }
|
|
|
|
} else {
|
|
|
|
alert.runModal()
|
|
|
|
}
|
|
|
|
return .terminateCancel
|
|
|
|
}
|
2019-04-24 09:49:20 +00:00
|
|
|
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ application: NSApplication) -> Bool {
|
2019-05-25 18:38:13 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak self] in
|
|
|
|
self?.setDockIconAndMainMenuVisibility(isVisible: false)
|
|
|
|
}
|
2019-04-24 09:49:20 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-05-22 09:56:38 +00:00
|
|
|
|
|
|
|
private func setDockIconAndMainMenuVisibility(isVisible: Bool, completion: (() -> Void)? = nil) {
|
|
|
|
let currentActivationPolicy = NSApp.activationPolicy()
|
|
|
|
let newActivationPolicy: NSApplication.ActivationPolicy = isVisible ? .regular : .accessory
|
|
|
|
guard currentActivationPolicy != newActivationPolicy else {
|
|
|
|
if newActivationPolicy == .regular {
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
}
|
|
|
|
completion?()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if newActivationPolicy == .regular && NSApp.isActive {
|
|
|
|
// To workaround a possible AppKit bug that causes the main menu to become unresponsive,
|
|
|
|
// we should deactivate the app first and then set the activation policy.
|
|
|
|
// NSApp.deactivate() doesn't always deactivate the app, so we instead use
|
|
|
|
// setActivationPolicy(.prohibited).
|
|
|
|
onAppDeactivation = {
|
|
|
|
NSApp.setActivationPolicy(.regular)
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
completion?()
|
|
|
|
}
|
|
|
|
NSApp.setActivationPolicy(.prohibited)
|
|
|
|
} else {
|
|
|
|
NSApp.setActivationPolicy(newActivationPolicy)
|
|
|
|
if newActivationPolicy == .regular {
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
}
|
|
|
|
completion?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationDidResignActive(_ notification: Notification) {
|
|
|
|
onAppDeactivation?()
|
|
|
|
onAppDeactivation = nil
|
|
|
|
}
|
2019-05-06 13:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension AppDelegate {
|
|
|
|
@objc func aboutClicked() {
|
|
|
|
var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
|
|
|
|
if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
|
|
|
|
appVersion += " (\(appBuild))"
|
|
|
|
}
|
|
|
|
let appVersionString = [
|
|
|
|
tr(format: "macAppVersion (%@)", appVersion),
|
2020-12-02 17:03:16 +00:00
|
|
|
tr(format: "macGoBackendVersion (%@)", WIREGUARD_GO_VERSION)
|
2019-05-06 13:38:57 +00:00
|
|
|
].joined(separator: "\n")
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
NSApp.orderFrontStandardAboutPanel(options: [
|
|
|
|
.applicationVersion: appVersionString,
|
2019-10-11 19:31:20 +00:00
|
|
|
.version: "",
|
2020-12-15 23:03:34 +00:00
|
|
|
.credits: ""
|
2019-05-06 13:38:57 +00:00
|
|
|
])
|
|
|
|
}
|
2019-01-17 19:57:17 +00:00
|
|
|
}
|
2018-12-29 10:03:41 +00:00
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
extension AppDelegate: StatusMenuWindowDelegate {
|
2019-05-15 10:46:00 +00:00
|
|
|
func showManageTunnelsWindow(completion: ((NSWindow?) -> Void)?) {
|
|
|
|
guard let tunnelsManager = tunnelsManager else {
|
|
|
|
completion?(nil)
|
|
|
|
return
|
|
|
|
}
|
2019-01-17 19:57:17 +00:00
|
|
|
if manageTunnelsWindowObject == nil {
|
2019-05-15 10:46:00 +00:00
|
|
|
manageTunnelsRootVC = ManageTunnelsRootViewController(tunnelsManager: tunnelsManager)
|
2019-01-17 19:57:17 +00:00
|
|
|
let window = NSWindow(contentViewController: manageTunnelsRootVC!)
|
|
|
|
window.title = tr("macWindowTitleManageTunnels")
|
|
|
|
window.setContentSize(NSSize(width: 800, height: 480))
|
|
|
|
window.setFrameAutosaveName(NSWindow.FrameAutosaveName("ManageTunnelsWindow")) // Auto-save window position and size
|
|
|
|
manageTunnelsWindowObject = window
|
|
|
|
tunnelsTracker?.manageTunnelsRootVC = manageTunnelsRootVC
|
2018-12-28 13:59:09 +00:00
|
|
|
}
|
2019-05-22 09:56:38 +00:00
|
|
|
setDockIconAndMainMenuVisibility(isVisible: true) { [weak manageTunnelsWindowObject] in
|
|
|
|
manageTunnelsWindowObject?.makeKeyAndOrderFront(self)
|
|
|
|
completion?(manageTunnelsWindowObject)
|
|
|
|
}
|
2018-12-25 13:03:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-19 15:13:52 +00:00
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func registerLoginItem(shouldLaunchAtLogin: Bool) -> Bool {
|
|
|
|
let appId = Bundle.main.bundleIdentifier!
|
|
|
|
let helperBundleId = "\(appId).login-item-helper"
|
|
|
|
return SMLoginItemSetEnabled(helperBundleId as CFString, shouldLaunchAtLogin)
|
|
|
|
}
|