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?
|
2020-12-22 15:40:28 +00:00
|
|
|
|
|
|
|
private let tunnelsBreakdownMenu = NSMenu()
|
|
|
|
private let tunnelsMenuItem = NSMenuItem(title: tr("macTunnelsMenuTitle"), action: nil, keyEquivalent: "")
|
|
|
|
private let tunnelsMenuSeparatorItem = NSMenuItem.separator()
|
|
|
|
|
|
|
|
private var firstTunnelMenuItemIndex = 0
|
|
|
|
private var numberOfTunnelMenuItems = 0
|
|
|
|
private var tunnelsPresentationStyle = StatusMenuTunnelsPresentationStyle.inline
|
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
|
2020-12-22 15:40:28 +00:00
|
|
|
|
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())
|
|
|
|
|
2020-12-22 15:40:28 +00:00
|
|
|
tunnelsMenuItem.submenu = tunnelsBreakdownMenu
|
|
|
|
addItem(tunnelsMenuItem)
|
|
|
|
|
2018-12-29 10:03:41 +00:00
|
|
|
firstTunnelMenuItemIndex = numberOfItems
|
2020-12-22 15:40:28 +00:00
|
|
|
populateInitialTunnelMenuItems()
|
|
|
|
|
|
|
|
addItem(tunnelsMenuSeparatorItem)
|
|
|
|
|
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-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) {
|
2020-12-22 15:40:28 +00:00
|
|
|
let nextNumberOfTunnels = numberOfTunnelMenuItems + 1
|
|
|
|
|
|
|
|
guard !reparentTunnelMenuItems(nextNumberOfTunnels: nextNumberOfTunnels) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let menuItem = makeTunnelItem(tunnel: tunnel)
|
|
|
|
switch tunnelsPresentationStyle {
|
|
|
|
case .submenu:
|
|
|
|
tunnelsBreakdownMenu.insertItem(menuItem, at: tunnelIndex)
|
|
|
|
case .inline:
|
|
|
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + tunnelIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfTunnelMenuItems = nextNumberOfTunnels
|
|
|
|
updateTunnelsMenuItemVisibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeTunnelMenuItem(at tunnelIndex: Int) {
|
|
|
|
let nextNumberOfTunnels = numberOfTunnelMenuItems - 1
|
|
|
|
|
|
|
|
guard !reparentTunnelMenuItems(nextNumberOfTunnels: nextNumberOfTunnels) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tunnelsPresentationStyle {
|
|
|
|
case .submenu:
|
|
|
|
tunnelsBreakdownMenu.removeItem(at: tunnelIndex)
|
|
|
|
case .inline:
|
|
|
|
removeItem(at: firstTunnelMenuItemIndex + tunnelIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfTunnelMenuItems = nextNumberOfTunnels
|
|
|
|
updateTunnelsMenuItemVisibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveTunnelMenuItem(from oldTunnelIndex: Int, to newTunnelIndex: Int) {
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: newTunnelIndex)
|
|
|
|
let menuItem = makeTunnelItem(tunnel: tunnel)
|
|
|
|
|
|
|
|
switch tunnelsPresentationStyle {
|
|
|
|
case .submenu:
|
|
|
|
tunnelsBreakdownMenu.removeItem(at: oldTunnelIndex)
|
|
|
|
tunnelsBreakdownMenu.insertItem(menuItem, at: newTunnelIndex)
|
|
|
|
case .inline:
|
|
|
|
removeItem(at: firstTunnelMenuItemIndex + oldTunnelIndex)
|
|
|
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + newTunnelIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func makeTunnelItem(tunnel: TunnelContainer) -> TunnelMenuItem {
|
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
|
2020-12-22 15:40:28 +00:00
|
|
|
return menuItem
|
|
|
|
}
|
|
|
|
|
|
|
|
private func populateInitialTunnelMenuItems() {
|
|
|
|
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
|
|
|
let initialStyle = tunnelsPresentationStyle.preferredPresentationStyle(numberOfTunnels: numberOfTunnels)
|
|
|
|
|
|
|
|
tunnelsPresentationStyle = initialStyle
|
|
|
|
switch initialStyle {
|
|
|
|
case .inline:
|
|
|
|
numberOfTunnelMenuItems = addTunnelMenuItems(into: self, at: firstTunnelMenuItemIndex)
|
|
|
|
case .submenu:
|
|
|
|
numberOfTunnelMenuItems = addTunnelMenuItems(into: tunnelsBreakdownMenu, at: 0)
|
2018-12-29 13:14:29 +00:00
|
|
|
}
|
2020-12-22 15:40:28 +00:00
|
|
|
|
|
|
|
updateTunnelsMenuItemVisibility()
|
2018-12-29 13:14:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:40:28 +00:00
|
|
|
private func reparentTunnelMenuItems(nextNumberOfTunnels: Int) -> Bool {
|
|
|
|
let nextStyle = tunnelsPresentationStyle.preferredPresentationStyle(numberOfTunnels: nextNumberOfTunnels)
|
|
|
|
|
|
|
|
switch (tunnelsPresentationStyle, nextStyle) {
|
|
|
|
case (.inline, .submenu):
|
|
|
|
tunnelsPresentationStyle = nextStyle
|
|
|
|
for index in (0..<numberOfTunnelMenuItems).reversed() {
|
|
|
|
removeItem(at: firstTunnelMenuItemIndex + index)
|
2018-12-29 13:14:29 +00:00
|
|
|
}
|
2020-12-22 15:40:28 +00:00
|
|
|
numberOfTunnelMenuItems = addTunnelMenuItems(into: tunnelsBreakdownMenu, at: 0)
|
|
|
|
updateTunnelsMenuItemVisibility()
|
|
|
|
return true
|
|
|
|
|
|
|
|
case (.submenu, .inline):
|
|
|
|
tunnelsPresentationStyle = nextStyle
|
|
|
|
tunnelsBreakdownMenu.removeAllItems()
|
|
|
|
numberOfTunnelMenuItems = addTunnelMenuItems(into: self, at: firstTunnelMenuItemIndex)
|
|
|
|
updateTunnelsMenuItemVisibility()
|
|
|
|
return true
|
|
|
|
|
|
|
|
case (.submenu, .submenu), (.inline, .inline):
|
|
|
|
return false
|
2018-12-29 13:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 15:40:28 +00:00
|
|
|
private func addTunnelMenuItems(into menu: NSMenu, at startIndex: Int) -> Int {
|
|
|
|
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
|
|
|
for tunnelIndex in 0..<numberOfTunnels {
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: tunnelIndex)
|
|
|
|
let menuItem = makeTunnelItem(tunnel: tunnel)
|
|
|
|
menu.insertItem(menuItem, at: startIndex + tunnelIndex)
|
|
|
|
}
|
|
|
|
return numberOfTunnels
|
|
|
|
}
|
2018-12-29 13:14:29 +00:00
|
|
|
|
2020-12-22 15:40:28 +00:00
|
|
|
private func updateTunnelsMenuItemVisibility() {
|
|
|
|
switch tunnelsPresentationStyle {
|
|
|
|
case .inline:
|
|
|
|
tunnelsMenuItem.isHidden = true
|
|
|
|
case .submenu:
|
|
|
|
tunnelsMenuItem.isHidden = false
|
|
|
|
}
|
|
|
|
tunnelsMenuSeparatorItem.isHidden = numberOfTunnelMenuItems == 0
|
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
|
|
|
}
|
|
|
|
}
|
2020-12-22 15:40:28 +00:00
|
|
|
|
|
|
|
private enum StatusMenuTunnelsPresentationStyle {
|
|
|
|
case inline
|
|
|
|
case submenu
|
|
|
|
|
|
|
|
func preferredPresentationStyle(numberOfTunnels: Int) -> StatusMenuTunnelsPresentationStyle {
|
|
|
|
let maxInlineTunnels = 10
|
|
|
|
|
|
|
|
if case .inline = self, numberOfTunnels > maxInlineTunnels {
|
|
|
|
return .submenu
|
|
|
|
} else if case .submenu = self, numberOfTunnels <= maxInlineTunnels {
|
|
|
|
return .inline
|
|
|
|
} else {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|