From 4ed646973ee4e1871cda792083bf4fe70afa8c3f Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 22 Dec 2018 00:28:18 +0100 Subject: Move name from interface to tunnel Signed-off-by: Jason A. Donenfeld --- WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 10 +++++----- WireGuard/WireGuard/UI/TunnelViewModel.swift | 19 ++++++++++++------- .../UI/iOS/ViewController/QRScanViewController.swift | 4 ++-- .../TunnelEditTableViewController.swift | 2 +- .../TunnelsListTableViewController.swift | 2 +- WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 2 +- WireGuard/WireGuard/ZipArchive/ZipImporter.swift | 2 +- 7 files changed, 23 insertions(+), 18 deletions(-) (limited to 'WireGuard/WireGuard') diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index 8c7bbb3..bf5ab52 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -54,7 +54,7 @@ class TunnelsManager { } func add(tunnelConfiguration: TunnelConfiguration, activateOnDemandSetting: ActivateOnDemandSetting = ActivateOnDemandSetting.defaultSetting, completionHandler: @escaping (WireGuardResult) -> Void) { - let tunnelName = tunnelConfiguration.interface.name ?? "" + let tunnelName = tunnelConfiguration.name ?? "" if tunnelName.isEmpty { completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty)) return @@ -67,7 +67,7 @@ class TunnelsManager { let tunnelProviderManager = NETunnelProviderManager() tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) - tunnelProviderManager.localizedDescription = tunnelConfiguration.interface.name + tunnelProviderManager.localizedDescription = tunnelConfiguration.name tunnelProviderManager.isEnabled = true activateOnDemandSetting.apply(on: tunnelProviderManager) @@ -107,7 +107,7 @@ class TunnelsManager { } func modify(tunnel: TunnelContainer, tunnelConfiguration: TunnelConfiguration, activateOnDemandSetting: ActivateOnDemandSetting, completionHandler: @escaping (TunnelsManagerError?) -> Void) { - let tunnelName = tunnelConfiguration.interface.name ?? "" + let tunnelName = tunnelConfiguration.name ?? "" if tunnelName.isEmpty { completionHandler(TunnelsManagerError.tunnelNameEmpty) return @@ -124,7 +124,7 @@ class TunnelsManager { } tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) - tunnelProviderManager.localizedDescription = tunnelConfiguration.interface.name + tunnelProviderManager.localizedDescription = tunnelConfiguration.name tunnelProviderManager.isEnabled = true let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && activateOnDemandSetting.isActivateOnDemandEnabled @@ -349,7 +349,7 @@ class TunnelContainer: NSObject { private var lastTunnelConnectionStatus: NEVPNStatus? var tunnelConfiguration: TunnelConfiguration? { - return (tunnelProvider.protocolConfiguration as? NETunnelProviderProtocol)?.tunnelConfiguration(name: tunnelProvider.localizedDescription) + return (tunnelProvider.protocolConfiguration as? NETunnelProviderProtocol)?.asTunnelConfiguration(called: tunnelProvider.localizedDescription) } var activateOnDemandSetting: ActivateOnDemandSetting { diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index 0b5b8c0..0be3cb8 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -66,6 +66,7 @@ class TunnelViewModel { var scratchpad = [InterfaceField: String]() var fieldsWithError = Set() var validatedConfiguration: InterfaceConfiguration? + var validatedName: String? subscript(field: InterfaceField) -> String { get { @@ -83,6 +84,7 @@ class TunnelViewModel { populateScratchpad() } validatedConfiguration = nil + validatedName = nil if stringValue.isEmpty { scratchpad.removeValue(forKey: field) } else { @@ -102,7 +104,8 @@ class TunnelViewModel { func populateScratchpad() { // Populate the scratchpad from the configuration object guard let config = validatedConfiguration else { return } - scratchpad[.name] = config.name + guard let name = validatedName else { return } + scratchpad[.name] = name scratchpad[.privateKey] = config.privateKey.base64EncodedString() scratchpad[.publicKey] = config.publicKey.base64EncodedString() if !config.addresses.isEmpty { @@ -120,10 +123,10 @@ class TunnelViewModel { } //swiftlint:disable:next cyclomatic_complexity function_body_length - func save() -> SaveResult { - if let validatedConfiguration = validatedConfiguration { + func save() -> SaveResult<(String, InterfaceConfiguration)> { + if let config = validatedConfiguration, let name = validatedName { // It's already validated and saved - return .saved(validatedConfiguration) + return .saved((name, config)) } fieldsWithError.removeAll() guard let name = scratchpad[.name]?.trimmingCharacters(in: .whitespacesAndNewlines), (!name.isEmpty) else { @@ -138,7 +141,7 @@ class TunnelViewModel { fieldsWithError.insert(.privateKey) return .error(tr("alertInvalidInterfaceMessagePrivateKeyInvalid")) } - var config = InterfaceConfiguration(name: name, privateKey: privateKey) + var config = InterfaceConfiguration(privateKey: privateKey) var errorMessages = [String]() if let addressesString = scratchpad[.addresses] { var addresses = [IPAddressRange]() @@ -184,7 +187,8 @@ class TunnelViewModel { guard errorMessages.isEmpty else { return .error(errorMessages.first!) } validatedConfiguration = config - return .saved(config) + validatedName = name + return .saved((name, config)) } func filterFieldsWithValueOrControl(interfaceFields: [InterfaceField]) -> [InterfaceField] { @@ -390,6 +394,7 @@ class TunnelViewModel { var peersData = [PeerData]() if let tunnelConfiguration = tunnelConfiguration { interfaceData.validatedConfiguration = tunnelConfiguration.interface + interfaceData.validatedName = tunnelConfiguration.name for (index, peerConfiguration) in tunnelConfiguration.peers.enumerated() { let peerData = PeerData(index: index) peerData.validatedConfiguration = peerConfiguration @@ -453,7 +458,7 @@ class TunnelViewModel { return .error(tr("alertInvalidPeerMessagePublicKeyDuplicated")) } - let tunnelConfiguration = TunnelConfiguration(interface: interfaceConfiguration, peers: peerConfigurations) + let tunnelConfiguration = TunnelConfiguration(name: interfaceConfiguration.0, interface: interfaceConfiguration.1, peers: peerConfigurations) return .saved(tunnelConfiguration) } } diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/QRScanViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/QRScanViewController.swift index a4f7130..2f8d41f 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/QRScanViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/QRScanViewController.swift @@ -101,7 +101,7 @@ class QRScanViewController: UIViewController { } func scanDidComplete(withCode code: String) { - let scannedTunnelConfiguration = try? TunnelConfiguration(code, name: "Scanned") + let scannedTunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: code, called: "Scanned") guard let tunnelConfiguration = scannedTunnelConfiguration else { scanDidEncounterError(title: tr("alertScanQRCodeInvalidQRCodeTitle"), message: tr("alertScanQRCodeInvalidQRCodeMessage")) return @@ -114,7 +114,7 @@ class QRScanViewController: UIViewController { }) alert.addAction(UIAlertAction(title: tr("actionSave"), style: .default) { [weak self] _ in guard let title = alert.textFields?[0].text?.trimmingCharacters(in: .whitespacesAndNewlines), !title.isEmpty else { return } - tunnelConfiguration.interface.name = title + tunnelConfiguration.name = title if let self = self { self.delegate?.addScannedQRCode(tunnelConfiguration: tunnelConfiguration, qrScanViewController: self) { self.dismiss(animated: true, completion: nil) diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift index 79dc7b6..17e5130 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift @@ -98,7 +98,7 @@ class TunnelEditTableViewController: UITableViewController { let tunnelSaveResult = tunnelViewModel.save() switch tunnelSaveResult { case .error(let errorMessage): - let alertTitle = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? + let alertTitle = (tunnelViewModel.interfaceData.validatedConfiguration == nil || tunnelViewModel.interfaceData.validatedName == nil) ? tr("alertInvalidInterfaceTitle") : tr("alertInvalidPeerTitle") ErrorPresenter.showErrorAlert(title: alertTitle, message: errorMessage, from: self) tableView.reloadData() // Highlight erroring fields diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift index 77952c8..5e4583e 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift @@ -180,7 +180,7 @@ class TunnelsListTableViewController: UIViewController { } else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ { let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) if let fileContents = try? String(contentsOf: url), - let tunnelConfiguration = try? TunnelConfiguration(fileContents, name: fileBaseName) { + let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) { tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] result in if let error = result.error { ErrorPresenter.showErrorAlert(error: error, from: self, onPresented: completionHandler) diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift index 052242a..1f7c2da 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -23,7 +23,7 @@ class ZipExporter { var lastTunnelName: String = "" for tunnelConfiguration in tunnelConfigurations { if let contents = tunnelConfiguration.asWgQuickConfig().data(using: .utf8) { - let name = tunnelConfiguration.interface.name ?? "" + let name = tunnelConfiguration.name ?? "untitled" if name.isEmpty || name == lastTunnelName { continue } inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) lastTunnelName = name diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift index a8819e2..18a00e8 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift @@ -44,7 +44,7 @@ class ZipImporter { continue } guard let fileContents = String(data: file.contents, encoding: .utf8) else { continue } - guard let tunnelConfig = try? TunnelConfiguration(fileContents, name: file.fileBaseName) else { continue } + guard let tunnelConfig = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: file.fileBaseName) else { continue } configs[index] = tunnelConfig } DispatchQueue.main.async { completion(.success(configs)) } -- cgit v1.2.3-59-g8ed1b