2018-12-28 13:59:09 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2018-12-28 13:59:09 +00:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
protocol StatusMenuWindowDelegate: class {
|
2019-05-15 10:46:00 +00:00
|
|
|
func showManageTunnelsWindow(completion: ((NSWindow?) -> Void)?)
|
2019-01-17 19:57:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 13:59:09 +00:00
|
|
|
class StatusMenu: NSMenu {
|
|
|
|
|
|
|
|
let tunnelsManager: TunnelsManager
|
2018-12-29 13:14:29 +00:00
|
|
|
|
2018-12-29 14:09:14 +00:00
|
|
|
var statusMenuItem: NSMenuItem?
|
|
|
|
var networksMenuItem: NSMenuItem?
|
2019-03-19 10:05:15 +00:00
|
|
|
var deactivateMenuItem: NSMenuItem?
|
2019-01-10 09:21:20 +00:00
|
|
|
var firstTunnelMenuItemIndex = 0
|
|
|
|
var numberOfTunnelMenuItems = 0
|
2018-12-28 13:59:09 +00:00
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
var currentTunnel: TunnelContainer? {
|
|
|
|
didSet {
|
|
|
|
updateStatusMenuItems(with: currentTunnel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
weak var windowDelegate: StatusMenuWindowDelegate?
|
2019-01-03 14:13:52 +00:00
|
|
|
|
2018-12-28 13:59:09 +00:00
|
|
|
init(tunnelsManager: TunnelsManager) {
|
|
|
|
self.tunnelsManager = tunnelsManager
|
2019-01-22 10:25:16 +00:00
|
|
|
super.init(title: tr("macMenuTitle"))
|
2018-12-29 14:09:14 +00:00
|
|
|
|
|
|
|
addStatusMenuItems()
|
|
|
|
addItem(NSMenuItem.separator())
|
|
|
|
|
2018-12-29 10:03:41 +00:00
|
|
|
firstTunnelMenuItemIndex = numberOfItems
|
|
|
|
let isAdded = addTunnelMenuItems()
|
|
|
|
if isAdded {
|
|
|
|
addItem(NSMenuItem.separator())
|
|
|
|
}
|
2018-12-28 19:12:02 +00:00
|
|
|
addTunnelManagementItems()
|
2019-01-08 21:44:59 +00:00
|
|
|
addItem(NSMenuItem.separator())
|
|
|
|
addApplicationItems()
|
2018-12-28 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init(coder decoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2018-12-29 14:09:14 +00:00
|
|
|
func addStatusMenuItems() {
|
2019-01-03 12:19:32 +00:00
|
|
|
let statusTitle = tr(format: "macStatus (%@)", tr("tunnelStatusInactive"))
|
2019-01-21 22:41:24 +00:00
|
|
|
let statusMenuItem = NSMenuItem(title: statusTitle, action: nil, keyEquivalent: "")
|
2018-12-29 14:09:14 +00:00
|
|
|
statusMenuItem.isEnabled = false
|
|
|
|
addItem(statusMenuItem)
|
2019-01-21 22:41:24 +00:00
|
|
|
let networksMenuItem = NSMenuItem(title: "", action: nil, keyEquivalent: "")
|
2018-12-29 14:09:14 +00:00
|
|
|
networksMenuItem.isEnabled = false
|
2019-01-15 21:35:30 +00:00
|
|
|
networksMenuItem.isHidden = true
|
2018-12-29 14:09:14 +00:00
|
|
|
addItem(networksMenuItem)
|
2019-03-19 10:05:15 +00:00
|
|
|
let deactivateMenuItem = NSMenuItem(title: tr("macToggleStatusButtonDeactivate"), action: #selector(deactivateClicked), keyEquivalent: "")
|
|
|
|
deactivateMenuItem.target = self
|
|
|
|
deactivateMenuItem.isHidden = true
|
|
|
|
addItem(deactivateMenuItem)
|
2018-12-29 14:09:14 +00:00
|
|
|
self.statusMenuItem = statusMenuItem
|
|
|
|
self.networksMenuItem = networksMenuItem
|
2019-03-19 10:05:15 +00:00
|
|
|
self.deactivateMenuItem = deactivateMenuItem
|
2018-12-29 14:09:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
func updateStatusMenuItems(with tunnel: TunnelContainer?) {
|
2019-03-19 10:05:15 +00:00
|
|
|
guard let statusMenuItem = statusMenuItem, let networksMenuItem = networksMenuItem, let deactivateMenuItem = deactivateMenuItem else { return }
|
2019-01-17 19:57:17 +00:00
|
|
|
guard let tunnel = tunnel else {
|
|
|
|
statusMenuItem.title = tr(format: "macStatus (%@)", tr("tunnelStatusInactive"))
|
|
|
|
networksMenuItem.title = ""
|
|
|
|
networksMenuItem.isHidden = true
|
2019-03-19 10:05:15 +00:00
|
|
|
deactivateMenuItem.isHidden = true
|
2019-01-17 19:57:17 +00:00
|
|
|
return
|
|
|
|
}
|
2018-12-29 14:09:14 +00:00
|
|
|
var statusText: String
|
|
|
|
|
|
|
|
switch tunnel.status {
|
|
|
|
case .waiting:
|
2019-01-17 19:57:17 +00:00
|
|
|
statusText = tr("tunnelStatusWaiting")
|
2018-12-29 14:09:14 +00:00
|
|
|
case .inactive:
|
|
|
|
statusText = tr("tunnelStatusInactive")
|
|
|
|
case .activating:
|
|
|
|
statusText = tr("tunnelStatusActivating")
|
|
|
|
case .active:
|
|
|
|
statusText = tr("tunnelStatusActive")
|
|
|
|
case .deactivating:
|
|
|
|
statusText = tr("tunnelStatusDeactivating")
|
|
|
|
case .reasserting:
|
|
|
|
statusText = tr("tunnelStatusReasserting")
|
|
|
|
case .restarting:
|
|
|
|
statusText = tr("tunnelStatusRestarting")
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:19:32 +00:00
|
|
|
statusMenuItem.title = tr(format: "macStatus (%@)", statusText)
|
2018-12-29 14:09:14 +00:00
|
|
|
|
2019-01-08 21:19:46 +00:00
|
|
|
if tunnel.status == .inactive {
|
2019-01-17 19:57:17 +00:00
|
|
|
networksMenuItem.title = ""
|
2019-01-15 21:35:30 +00:00
|
|
|
networksMenuItem.isHidden = true
|
2018-12-29 14:09:14 +00:00
|
|
|
} else {
|
2019-01-15 21:35:30 +00:00
|
|
|
let allowedIPs = tunnel.tunnelConfiguration?.peers.flatMap { $0.allowedIPs }.map { $0.stringRepresentation }.joined(separator: ", ") ?? ""
|
|
|
|
if !allowedIPs.isEmpty {
|
|
|
|
networksMenuItem.title = tr(format: "macMenuNetworks (%@)", allowedIPs)
|
2019-01-08 21:19:46 +00:00
|
|
|
} else {
|
2019-01-15 21:35:30 +00:00
|
|
|
networksMenuItem.title = tr("macMenuNetworksNone")
|
2019-01-08 21:19:46 +00:00
|
|
|
}
|
2019-01-15 21:35:30 +00:00
|
|
|
networksMenuItem.isHidden = false
|
2018-12-29 14:09:14 +00:00
|
|
|
}
|
2019-03-19 10:05:15 +00:00
|
|
|
deactivateMenuItem.isHidden = tunnel.status != .active
|
2018-12-29 14:09:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 10:03:41 +00:00
|
|
|
func addTunnelMenuItems() -> Bool {
|
|
|
|
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
2018-12-28 13:59:09 +00:00
|
|
|
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: index)
|
2018-12-29 13:14:29 +00:00
|
|
|
insertTunnelMenuItem(for: tunnel, at: numberOfTunnelMenuItems)
|
2018-12-28 13:59:09 +00:00
|
|
|
}
|
2018-12-29 10:03:41 +00:00
|
|
|
return numberOfTunnels > 0
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:12:02 +00:00
|
|
|
func addTunnelManagementItems() {
|
|
|
|
let manageItem = NSMenuItem(title: tr("macMenuManageTunnels"), action: #selector(manageTunnelsClicked), keyEquivalent: "")
|
|
|
|
manageItem.target = self
|
|
|
|
addItem(manageItem)
|
|
|
|
let importItem = NSMenuItem(title: tr("macMenuImportTunnels"), action: #selector(importTunnelsClicked), keyEquivalent: "")
|
|
|
|
importItem.target = self
|
|
|
|
addItem(importItem)
|
|
|
|
}
|
|
|
|
|
2019-01-08 21:44:59 +00:00
|
|
|
func addApplicationItems() {
|
2019-05-06 13:38:57 +00:00
|
|
|
let aboutItem = NSMenuItem(title: tr("macMenuAbout"), action: #selector(AppDelegate.aboutClicked), keyEquivalent: "")
|
|
|
|
aboutItem.target = NSApp.delegate
|
2019-01-11 18:50:02 +00:00
|
|
|
addItem(aboutItem)
|
2019-02-21 14:11:01 +00:00
|
|
|
let quitItem = NSMenuItem(title: tr("macMenuQuit"), action: #selector(AppDelegate.quit), keyEquivalent: "")
|
|
|
|
quitItem.target = NSApp.delegate
|
2019-01-08 21:44:59 +00:00
|
|
|
addItem(quitItem)
|
|
|
|
}
|
|
|
|
|
2019-03-19 10:05:15 +00:00
|
|
|
@objc func deactivateClicked() {
|
|
|
|
if let currentTunnel = currentTunnel {
|
|
|
|
tunnelsManager.startDeactivation(of: currentTunnel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 13:14:29 +00:00
|
|
|
@objc func tunnelClicked(sender: AnyObject) {
|
2019-01-17 19:57:17 +00:00
|
|
|
guard let tunnelMenuItem = sender as? TunnelMenuItem else { return }
|
2018-12-29 13:14:29 +00:00
|
|
|
if tunnelMenuItem.state == .off {
|
2019-01-17 19:57:17 +00:00
|
|
|
tunnelsManager.startActivation(of: tunnelMenuItem.tunnel)
|
2018-12-29 13:14:29 +00:00
|
|
|
} else {
|
2019-01-17 19:57:17 +00:00
|
|
|
tunnelsManager.startDeactivation(of: tunnelMenuItem.tunnel)
|
2018-12-29 13:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:12:02 +00:00
|
|
|
@objc func manageTunnelsClicked() {
|
2019-05-15 10:46:00 +00:00
|
|
|
windowDelegate?.showManageTunnelsWindow(completion: nil)
|
2018-12-28 19:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func importTunnelsClicked() {
|
2019-05-15 10:46:00 +00:00
|
|
|
windowDelegate?.showManageTunnelsWindow { [weak self] manageTunnelsWindow in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let manageTunnelsWindow = manageTunnelsWindow else { return }
|
|
|
|
ImportPanelPresenter.presentImportPanel(tunnelsManager: self.tunnelsManager,
|
|
|
|
sourceVC: manageTunnelsWindow.contentViewController)
|
|
|
|
}
|
2018-12-29 10:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 13:14:29 +00:00
|
|
|
extension StatusMenu {
|
|
|
|
func insertTunnelMenuItem(for tunnel: TunnelContainer, at tunnelIndex: Int) {
|
2019-01-17 19:57:17 +00:00
|
|
|
let menuItem = TunnelMenuItem(tunnel: tunnel, action: #selector(tunnelClicked(sender:)))
|
2018-12-29 13:14:29 +00:00
|
|
|
menuItem.target = self
|
2019-06-11 00:03:11 +00:00
|
|
|
menuItem.isHidden = !tunnel.isTunnelAvailableToUser
|
2018-12-29 13:14:29 +00:00
|
|
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + tunnelIndex)
|
|
|
|
if numberOfTunnelMenuItems == 0 {
|
|
|
|
insertItem(NSMenuItem.separator(), at: firstTunnelMenuItemIndex + tunnelIndex + 1)
|
|
|
|
}
|
|
|
|
numberOfTunnelMenuItems += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeTunnelMenuItem(at tunnelIndex: Int) {
|
|
|
|
removeItem(at: firstTunnelMenuItemIndex + tunnelIndex)
|
|
|
|
numberOfTunnelMenuItems -= 1
|
|
|
|
if numberOfTunnelMenuItems == 0 {
|
|
|
|
if let firstItem = item(at: firstTunnelMenuItemIndex), firstItem.isSeparatorItem {
|
|
|
|
removeItem(at: firstTunnelMenuItemIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveTunnelMenuItem(from oldTunnelIndex: Int, to newTunnelIndex: Int) {
|
2019-01-17 19:57:17 +00:00
|
|
|
guard let oldMenuItem = item(at: firstTunnelMenuItemIndex + oldTunnelIndex) as? TunnelMenuItem else { return }
|
|
|
|
let oldMenuItemTunnel = oldMenuItem.tunnel
|
2018-12-29 13:14:29 +00:00
|
|
|
removeItem(at: firstTunnelMenuItemIndex + oldTunnelIndex)
|
2019-01-17 19:57:17 +00:00
|
|
|
let menuItem = TunnelMenuItem(tunnel: oldMenuItemTunnel, action: #selector(tunnelClicked(sender:)))
|
2018-12-29 13:14:29 +00:00
|
|
|
menuItem.target = self
|
|
|
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + newTunnelIndex)
|
|
|
|
|
2018-12-29 10:03:41 +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
|
|
|
class TunnelMenuItem: NSMenuItem {
|
2018-12-29 10:03:41 +00:00
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
var tunnel: TunnelContainer
|
2018-12-29 10:03:41 +00:00
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
private var statusObservationToken: AnyObject?
|
|
|
|
private var nameObservationToken: AnyObject?
|
2019-01-05 08:56:20 +00:00
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
init(tunnel: TunnelContainer, action selector: Selector?) {
|
|
|
|
self.tunnel = tunnel
|
|
|
|
super.init(title: tunnel.name, action: selector, keyEquivalent: "")
|
|
|
|
updateStatus()
|
|
|
|
let statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in
|
|
|
|
self?.updateStatus()
|
|
|
|
}
|
|
|
|
updateTitle()
|
|
|
|
let nameObservationToken = tunnel.observe(\TunnelContainer.name) { [weak self] _, _ in
|
|
|
|
self?.updateTitle()
|
2019-01-05 08:56:20 +00:00
|
|
|
}
|
2019-01-17 19:57:17 +00:00
|
|
|
self.statusObservationToken = statusObservationToken
|
|
|
|
self.nameObservationToken = nameObservationToken
|
2019-01-05 08:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
required init(coder decoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
2019-01-05 08:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
func updateTitle() {
|
|
|
|
title = tunnel.name
|
2019-01-05 08:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:57:17 +00:00
|
|
|
func updateStatus() {
|
|
|
|
let shouldShowCheckmark = (tunnel.status != .inactive && tunnel.status != .deactivating)
|
|
|
|
state = shouldShowCheckmark ? .on : .off
|
2019-01-05 08:56:20 +00:00
|
|
|
}
|
|
|
|
}
|