diff options
author | Roopesh Chander <roop@roopc.net> | 2019-01-09 01:17:46 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2019-01-14 14:52:35 +0530 |
commit | 922b6f76b21d0c2b270fbe5a4743d28977cfe02e (patch) | |
tree | af1ab2cb29ae82f69534c305a0b23f3ebaaac7d9 /WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift | |
parent | macOS: Update detail view after editing (diff) | |
download | wireguard-apple-922b6f76b21d0c2b270fbe5a4743d28977cfe02e.tar.xz wireguard-apple-922b6f76b21d0c2b270fbe5a4743d28977cfe02e.zip |
macOS: Manage tunnels: Add empty tunnel pulldown menu implementation
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r-- | WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift index 62eba17..ae95fba 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift @@ -81,8 +81,10 @@ class TunnelEditViewController: NSViewController { fatalError("init(coder:) has not been implemented") } - override func loadView() { - if let tunnel = tunnel, let tunnelConfiguration = tunnel.tunnelConfiguration { + func populateTextFields() { + if let tunnel = tunnel { + // Editing an existing tunnel + let tunnelConfiguration = tunnel.tunnelConfiguration! nameRow.value = tunnel.name textView.string = tunnelConfiguration.asWgQuickConfig() publicKeyRow.value = tunnelConfiguration.interface.publicKey.base64EncodedString() @@ -97,7 +99,21 @@ class TunnelEditViewController: NSViewController { publicKeyRow?.value = "" } } + } else { + // Creating a new tunnel + let privateKey = Curve25519.generatePrivateKey() + let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey) + let bootstrappingText = """ + [Interface] + PrivateKey = \(privateKey.base64EncodedString()) + """ + publicKeyRow.value = publicKey.base64EncodedString() + textView.string = bootstrappingText } + } + + override func loadView() { + populateTextFields() scrollView.documentView = textView @@ -163,6 +179,29 @@ class TunnelEditViewController: NSViewController { } catch { fatalError() } + } else { + // We're creating a new tunnel + if tunnelsManager.tunnel(named: name) != nil { + ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self) + return + } + do { + let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value) + let onDemandSetting = ActivateOnDemandSetting.defaultSetting + tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] result in + if let error = result.error { + ErrorPresenter.showErrorAlert(error: error, from: self) + } else { + let tunnel: TunnelContainer = result.value! + self?.dismiss(self) + self?.delegate?.tunnelSaved(tunnel: tunnel) + } + } + } catch let error as WireGuardAppError { + ErrorPresenter.showErrorAlert(error: error, from: self) + } catch { + fatalError() + } } } |