2019-01-06 13:21:06 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2023-02-14 15:10:32 +00:00
|
|
|
// Copyright © 2018-2023 WireGuard LLC. All Rights Reserved.
|
2019-01-06 13:21:06 +00:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
2021-08-02 08:16:06 +00:00
|
|
|
protocol TunnelEditViewControllerDelegate: AnyObject {
|
2019-01-08 19:22:11 +00:00
|
|
|
func tunnelSaved(tunnel: TunnelContainer)
|
|
|
|
func tunnelEditingCancelled()
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:21:06 +00:00
|
|
|
class TunnelEditViewController: NSViewController {
|
|
|
|
|
|
|
|
let nameRow: EditableKeyValueRow = {
|
|
|
|
let nameRow = EditableKeyValueRow()
|
|
|
|
nameRow.key = tr(format: "macFieldKey (%@)", TunnelViewModel.InterfaceField.name.localizedUIString)
|
|
|
|
return nameRow
|
|
|
|
}()
|
|
|
|
|
|
|
|
let publicKeyRow: KeyValueRow = {
|
|
|
|
let publicKeyRow = KeyValueRow()
|
|
|
|
publicKeyRow.key = tr(format: "macFieldKey (%@)", TunnelViewModel.InterfaceField.publicKey.localizedUIString)
|
|
|
|
return publicKeyRow
|
|
|
|
}()
|
|
|
|
|
2019-01-08 19:11:36 +00:00
|
|
|
let textView: ConfTextView = {
|
2019-01-07 12:47:27 +00:00
|
|
|
let textView = ConfTextView()
|
2019-01-11 11:21:40 +00:00
|
|
|
let minWidth: CGFloat = 120
|
2019-01-17 08:50:09 +00:00
|
|
|
let minHeight: CGFloat = 0
|
2019-01-06 13:21:06 +00:00
|
|
|
textView.minSize = NSSize(width: 0, height: minHeight)
|
|
|
|
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
2019-01-11 11:17:20 +00:00
|
|
|
textView.autoresizingMask = [.width] // Width should be based on superview width
|
|
|
|
textView.isHorizontallyResizable = false // Width shouldn't be based on content
|
|
|
|
textView.isVerticallyResizable = true // Height should be based on content
|
2019-01-06 13:21:06 +00:00
|
|
|
if let textContainer = textView.textContainer {
|
|
|
|
textContainer.size = NSSize(width: minWidth, height: CGFloat.greatestFiniteMagnitude)
|
|
|
|
textContainer.widthTracksTextView = true
|
|
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
textView.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth),
|
|
|
|
textView.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
|
|
|
|
])
|
|
|
|
return textView
|
|
|
|
}()
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
let onDemandControlsRow = OnDemandControlsRow()
|
2019-03-06 10:00:42 +00:00
|
|
|
|
2019-01-06 13:21:06 +00:00
|
|
|
let scrollView: NSScrollView = {
|
|
|
|
let scrollView = NSScrollView()
|
|
|
|
scrollView.hasVerticalScroller = true
|
2019-01-11 11:26:20 +00:00
|
|
|
scrollView.autohidesScrollers = true
|
2019-01-06 13:21:06 +00:00
|
|
|
scrollView.borderType = .bezelBorder
|
|
|
|
return scrollView
|
|
|
|
}()
|
|
|
|
|
2019-02-16 12:55:17 +00:00
|
|
|
let excludePrivateIPsCheckbox: NSButton = {
|
|
|
|
let checkbox = NSButton()
|
|
|
|
checkbox.title = tr("tunnelPeerExcludePrivateIPs")
|
|
|
|
checkbox.setButtonType(.switch)
|
|
|
|
checkbox.state = .off
|
|
|
|
return checkbox
|
|
|
|
}()
|
|
|
|
|
2019-01-06 13:21:06 +00:00
|
|
|
let discardButton: NSButton = {
|
|
|
|
let button = NSButton()
|
|
|
|
button.title = tr("macEditDiscard")
|
|
|
|
button.setButtonType(.momentaryPushIn)
|
|
|
|
button.bezelStyle = .rounded
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
|
|
|
let saveButton: NSButton = {
|
|
|
|
let button = NSButton()
|
|
|
|
button.title = tr("macEditSave")
|
|
|
|
button.setButtonType(.momentaryPushIn)
|
|
|
|
button.bezelStyle = .rounded
|
2019-05-12 10:15:26 +00:00
|
|
|
button.keyEquivalent = "s"
|
|
|
|
button.keyEquivalentModifierMask = [.command]
|
2019-01-06 13:21:06 +00:00
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
|
|
|
let tunnelsManager: TunnelsManager
|
|
|
|
let tunnel: TunnelContainer?
|
2019-03-06 10:00:42 +00:00
|
|
|
var onDemandViewModel: ActivateOnDemandViewModel
|
2019-01-06 13:21:06 +00:00
|
|
|
|
2019-01-08 19:22:11 +00:00
|
|
|
weak var delegate: TunnelEditViewControllerDelegate?
|
|
|
|
|
2019-01-22 07:27:27 +00:00
|
|
|
var privateKeyObservationToken: AnyObject?
|
|
|
|
var hasErrorObservationToken: AnyObject?
|
2019-02-16 12:55:17 +00:00
|
|
|
var singlePeerAllowedIPsObservationToken: AnyObject?
|
2019-01-08 19:11:36 +00:00
|
|
|
|
2019-02-21 10:04:59 +00:00
|
|
|
var dnsServersAddedToAllowedIPs: String?
|
|
|
|
|
2019-01-06 13:21:06 +00:00
|
|
|
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) {
|
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
self.tunnel = tunnel
|
2019-03-11 12:39:48 +00:00
|
|
|
self.onDemandViewModel = tunnel != nil ? ActivateOnDemandViewModel(tunnel: tunnel!) : ActivateOnDemandViewModel()
|
2019-01-06 13:21:06 +00:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2019-02-24 09:44:35 +00:00
|
|
|
func populateFields() {
|
2019-01-08 19:47:46 +00:00
|
|
|
if let tunnel = tunnel {
|
|
|
|
// Editing an existing tunnel
|
|
|
|
let tunnelConfiguration = tunnel.tunnelConfiguration!
|
2019-01-06 13:21:06 +00:00
|
|
|
nameRow.value = tunnel.name
|
|
|
|
textView.string = tunnelConfiguration.asWgQuickConfig()
|
2020-11-26 16:23:50 +00:00
|
|
|
publicKeyRow.value = tunnelConfiguration.interface.privateKey.publicKey.base64Key
|
|
|
|
textView.privateKeyString = tunnelConfiguration.interface.privateKey.base64Key
|
2019-02-16 12:55:17 +00:00
|
|
|
let singlePeer = tunnelConfiguration.peers.count == 1 ? tunnelConfiguration.peers.first : nil
|
|
|
|
updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: singlePeer?.allowedIPs.map { $0.stringRepresentation })
|
2019-02-21 10:04:59 +00:00
|
|
|
dnsServersAddedToAllowedIPs = excludePrivateIPsCheckbox.state == .on ? tunnelConfiguration.interface.dns.map { $0.stringRepresentation }.joined(separator: ", ") : nil
|
2019-01-08 19:47:46 +00:00
|
|
|
} else {
|
|
|
|
// Creating a new tunnel
|
2020-11-26 16:23:50 +00:00
|
|
|
let privateKey = PrivateKey()
|
|
|
|
let bootstrappingText = "[Interface]\nPrivateKey = \(privateKey.base64Key)\n"
|
|
|
|
publicKeyRow.value = privateKey.publicKey.base64Key
|
2019-01-08 19:47:46 +00:00
|
|
|
textView.string = bootstrappingText
|
2019-03-22 09:45:22 +00:00
|
|
|
updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: nil)
|
|
|
|
dnsServersAddedToAllowedIPs = nil
|
2019-01-06 13:21:06 +00:00
|
|
|
}
|
2019-01-22 07:27:27 +00:00
|
|
|
privateKeyObservationToken = textView.observe(\.privateKeyString) { [weak publicKeyRow] textView, _ in
|
2019-01-21 21:40:43 +00:00
|
|
|
if let privateKeyString = textView.privateKeyString,
|
2020-11-26 16:23:50 +00:00
|
|
|
let privateKey = PrivateKey(base64Key: privateKeyString) {
|
|
|
|
publicKeyRow?.value = privateKey.publicKey.base64Key
|
2019-01-21 21:40:43 +00:00
|
|
|
} else {
|
|
|
|
publicKeyRow?.value = ""
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 07:27:27 +00:00
|
|
|
hasErrorObservationToken = textView.observe(\.hasError) { [weak saveButton] textView, _ in
|
|
|
|
saveButton?.isEnabled = !textView.hasError
|
|
|
|
}
|
2019-02-16 12:55:17 +00:00
|
|
|
singlePeerAllowedIPsObservationToken = textView.observe(\.singlePeerAllowedIPs) { [weak self] textView, _ in
|
|
|
|
self?.updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: textView.singlePeerAllowedIPs)
|
|
|
|
}
|
2019-01-08 19:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func loadView() {
|
2019-02-24 09:44:35 +00:00
|
|
|
populateFields()
|
2019-01-06 13:21:06 +00:00
|
|
|
|
|
|
|
scrollView.documentView = textView
|
|
|
|
|
|
|
|
saveButton.target = self
|
2019-01-16 19:58:57 +00:00
|
|
|
saveButton.action = #selector(handleSaveAction)
|
2019-01-06 13:21:06 +00:00
|
|
|
|
|
|
|
discardButton.target = self
|
2019-01-16 19:58:57 +00:00
|
|
|
discardButton.action = #selector(handleDiscardAction)
|
2019-01-06 13:21:06 +00:00
|
|
|
|
2019-02-16 12:55:17 +00:00
|
|
|
excludePrivateIPsCheckbox.target = self
|
|
|
|
excludePrivateIPsCheckbox.action = #selector(excludePrivateIPsCheckboxToggled(sender:))
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandControlsRow.onDemandViewModel = onDemandViewModel
|
|
|
|
|
2019-01-06 13:21:06 +00:00
|
|
|
let margin: CGFloat = 20
|
|
|
|
let internalSpacing: CGFloat = 10
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
let editorStackView = NSStackView(views: [nameRow, publicKeyRow, onDemandControlsRow, scrollView])
|
2019-01-06 13:21:06 +00:00
|
|
|
editorStackView.orientation = .vertical
|
|
|
|
editorStackView.setHuggingPriority(.defaultHigh, for: .horizontal)
|
|
|
|
editorStackView.spacing = internalSpacing
|
|
|
|
|
|
|
|
let buttonRowStackView = NSStackView()
|
|
|
|
buttonRowStackView.setViews([discardButton, saveButton], in: .trailing)
|
2019-02-16 12:55:17 +00:00
|
|
|
buttonRowStackView.addView(excludePrivateIPsCheckbox, in: .leading)
|
2019-01-06 13:21:06 +00:00
|
|
|
buttonRowStackView.orientation = .horizontal
|
|
|
|
buttonRowStackView.spacing = internalSpacing
|
|
|
|
|
|
|
|
let containerView = NSStackView(views: [editorStackView, buttonRowStackView])
|
|
|
|
containerView.orientation = .vertical
|
|
|
|
containerView.edgeInsets = NSEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
|
|
|
|
containerView.setHuggingPriority(.defaultHigh, for: .horizontal)
|
|
|
|
containerView.spacing = internalSpacing
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 180),
|
|
|
|
containerView.heightAnchor.constraint(greaterThanOrEqualToConstant: 240)
|
|
|
|
])
|
2019-01-11 11:21:40 +00:00
|
|
|
containerView.frame = NSRect(x: 0, y: 0, width: 600, height: 480)
|
2019-01-06 13:21:06 +00:00
|
|
|
|
|
|
|
self.view = containerView
|
|
|
|
}
|
|
|
|
|
2019-02-24 10:35:13 +00:00
|
|
|
func setUserInteractionEnabled(_ enabled: Bool) {
|
|
|
|
view.window?.ignoresMouseEvents = !enabled
|
|
|
|
nameRow.valueLabel.isEditable = enabled
|
|
|
|
textView.isEditable = enabled
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandControlsRow.onDemandSSIDsField.isEnabled = enabled
|
2019-02-24 10:35:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 19:58:57 +00:00
|
|
|
@objc func handleSaveAction() {
|
2019-01-08 13:28:40 +00:00
|
|
|
let name = nameRow.value
|
|
|
|
guard !name.isEmpty else {
|
|
|
|
ErrorPresenter.showErrorAlert(title: tr("macAlertNameIsEmpty"), message: "", from: self)
|
|
|
|
return
|
|
|
|
}
|
2019-03-06 10:00:42 +00:00
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandControlsRow.saveToViewModel()
|
2019-03-08 10:42:54 +00:00
|
|
|
let onDemandOption = onDemandViewModel.toOnDemandOption()
|
2019-01-21 21:52:01 +00:00
|
|
|
|
|
|
|
let isTunnelModifiedWithoutChangingName = (tunnel != nil && tunnel!.name == name)
|
|
|
|
guard isTunnelModifiedWithoutChangingName || tunnelsManager.tunnel(named: name) == nil else {
|
|
|
|
ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:04:59 +00:00
|
|
|
var tunnelConfiguration: TunnelConfiguration
|
2019-01-21 21:52:01 +00:00
|
|
|
do {
|
|
|
|
tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value)
|
|
|
|
} catch let error as WireGuardAppError {
|
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
|
|
|
return
|
|
|
|
} catch {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:04:59 +00:00
|
|
|
if excludePrivateIPsCheckbox.state == .on, tunnelConfiguration.peers.count == 1, let dnsServersAddedToAllowedIPs = dnsServersAddedToAllowedIPs {
|
|
|
|
// Update the DNS servers in the AllowedIPs
|
|
|
|
let tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration)
|
|
|
|
let originalAllowedIPs = tunnelViewModel.peersData[0][.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines)
|
|
|
|
let dnsServersInAllowedIPs = TunnelViewModel.PeerData.normalizedIPAddressRangeStrings(dnsServersAddedToAllowedIPs.splitToArray(trimmingCharacters: .whitespacesAndNewlines))
|
|
|
|
let dnsServersCurrent = TunnelViewModel.PeerData.normalizedIPAddressRangeStrings(tunnelViewModel.interfaceData[.dns].splitToArray(trimmingCharacters: .whitespacesAndNewlines))
|
|
|
|
let modifiedAllowedIPs = originalAllowedIPs.filter { !dnsServersInAllowedIPs.contains($0) } + dnsServersCurrent
|
|
|
|
tunnelViewModel.peersData[0][.allowedIPs] = modifiedAllowedIPs.joined(separator: ", ")
|
|
|
|
let saveResult = tunnelViewModel.save()
|
|
|
|
if case .saved(let modifiedTunnelConfiguration) = saveResult {
|
|
|
|
tunnelConfiguration = modifiedTunnelConfiguration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 10:35:13 +00:00
|
|
|
setUserInteractionEnabled(false)
|
|
|
|
|
2019-01-08 13:28:40 +00:00
|
|
|
if let tunnel = tunnel {
|
|
|
|
// We're modifying an existing tunnel
|
2019-03-08 10:42:54 +00:00
|
|
|
tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] error in
|
2019-05-29 14:15:22 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
self.setUserInteractionEnabled(true)
|
2019-01-21 21:52:01 +00:00
|
|
|
if let error = error {
|
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
|
|
|
return
|
2019-01-08 13:28:40 +00:00
|
|
|
}
|
2019-05-29 14:15:22 +00:00
|
|
|
self.delegate?.tunnelSaved(tunnel: tunnel)
|
|
|
|
self.presentingViewController?.dismiss(self)
|
2019-01-08 13:28:40 +00:00
|
|
|
}
|
2019-01-08 19:47:46 +00:00
|
|
|
} else {
|
|
|
|
// We're creating a new tunnel
|
2019-03-20 03:24:23 +00:00
|
|
|
self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in
|
2019-05-29 14:15:22 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
self.setUserInteractionEnabled(true)
|
2019-04-08 07:52:06 +00:00
|
|
|
switch result {
|
|
|
|
case .failure(let error):
|
2019-03-20 03:24:23 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
2019-04-08 07:52:06 +00:00
|
|
|
case .success(let tunnel):
|
2019-05-29 14:15:22 +00:00
|
|
|
self.delegate?.tunnelSaved(tunnel: tunnel)
|
|
|
|
self.presentingViewController?.dismiss(self)
|
2019-01-08 19:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 13:28:40 +00:00
|
|
|
}
|
2019-01-06 13:21:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 19:58:57 +00:00
|
|
|
@objc func handleDiscardAction() {
|
2019-01-08 19:22:11 +00:00
|
|
|
delegate?.tunnelEditingCancelled()
|
2019-05-29 14:15:22 +00:00
|
|
|
presentingViewController?.dismiss(self)
|
2019-01-06 13:21:06 +00:00
|
|
|
}
|
2019-02-16 12:55:17 +00:00
|
|
|
|
|
|
|
func updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: [String]?) {
|
|
|
|
let shouldAllowExcludePrivateIPsControl: Bool
|
|
|
|
let excludePrivateIPsValue: Bool
|
|
|
|
if let singlePeerAllowedIPs = singlePeerAllowedIPs {
|
|
|
|
(shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: true, allowedIPs: Set<String>(singlePeerAllowedIPs))
|
|
|
|
} else {
|
|
|
|
(shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: false, allowedIPs: Set<String>())
|
|
|
|
}
|
|
|
|
excludePrivateIPsCheckbox.isHidden = !shouldAllowExcludePrivateIPsControl
|
|
|
|
excludePrivateIPsCheckbox.state = excludePrivateIPsValue ? .on : .off
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func excludePrivateIPsCheckboxToggled(sender: AnyObject?) {
|
|
|
|
guard let excludePrivateIPsCheckbox = sender as? NSButton else { return }
|
|
|
|
guard let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value) else { return }
|
|
|
|
let isOn = excludePrivateIPsCheckbox.state == .on
|
|
|
|
let tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration)
|
2019-02-21 12:47:23 +00:00
|
|
|
tunnelViewModel.peersData.first?.excludePrivateIPsValueChanged(isOn: isOn, dnsServers: tunnelViewModel.interfaceData[.dns], oldDNSServers: dnsServersAddedToAllowedIPs)
|
2019-02-16 12:55:17 +00:00
|
|
|
if let modifiedConfig = tunnelViewModel.asWgQuickConfig() {
|
|
|
|
textView.setConfText(modifiedConfig)
|
2019-02-21 10:04:59 +00:00
|
|
|
dnsServersAddedToAllowedIPs = isOn ? tunnelViewModel.interfaceData[.dns] : nil
|
2019-02-16 12:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 13:21:06 +00:00
|
|
|
}
|
2019-05-12 07:53:52 +00:00
|
|
|
|
|
|
|
extension TunnelEditViewController {
|
|
|
|
override func cancelOperation(_ sender: Any?) {
|
|
|
|
handleDiscardAction()
|
|
|
|
}
|
|
|
|
}
|