From 422c4da09cbef996be4cd19c53e123c162181e8a Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 27 Oct 2018 11:36:41 +0200 Subject: [PATCH] Move ParsedFile out of WizardHostVC --- Passepartout-iOS/Global/IssueReporter.swift | 5 ++--- .../Scenes/ConfigurationViewController.swift | 8 ++++---- .../Organizer/WizardHostViewController.swift | 13 +------------ .../TunnelKitProvider+FileConfiguration.swift | 17 ++++++++++++++--- .../FileConfigurationTests.swift | 9 ++++----- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Passepartout-iOS/Global/IssueReporter.swift b/Passepartout-iOS/Global/IssueReporter.swift index 473e8a5b..80ccd093 100644 --- a/Passepartout-iOS/Global/IssueReporter.swift +++ b/Passepartout-iOS/Global/IssueReporter.swift @@ -90,10 +90,9 @@ class IssueReporter: NSObject { vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.debugLog, fileName: AppConstants.IssueReporter.Filenames.debugLog) } if let url = configurationURL { - var lines: [String] = [] do { - _ = try TunnelKitProvider.Configuration.parsed(from: url, stripped: &lines) - if let attachment = lines.joined(separator: "\n").data(using: .utf8) { + let parsedFile = try TunnelKitProvider.Configuration.parsed(from: url, returnsStripped: true) + if let attachment = parsedFile.strippedLines?.joined(separator: "\n").data(using: .utf8) { vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.configuration, fileName: AppConstants.IssueReporter.Filenames.configuration) } } catch { diff --git a/Passepartout-iOS/Scenes/ConfigurationViewController.swift b/Passepartout-iOS/Scenes/ConfigurationViewController.swift index 34208abb..b72253f3 100644 --- a/Passepartout-iOS/Scenes/ConfigurationViewController.swift +++ b/Passepartout-iOS/Scenes/ConfigurationViewController.swift @@ -117,18 +117,18 @@ class ConfigurationViewController: UIViewController, TableModelHost { // MARK: Actions private func resetOriginalConfiguration() { - guard let url = originalConfigurationURL else { + guard let originalURL = originalConfigurationURL else { log.warning("Resetting with no original configuration set? Bad table model?") return } - let originalConfiguration: TunnelKitProvider.Configuration + let parsedFile: ParsedFile do { - (_, originalConfiguration) = try TunnelKitProvider.Configuration.parsed(from: url) + parsedFile = try TunnelKitProvider.Configuration.parsed(from: originalURL) } catch let e { log.error("Could not parse original configuration: \(e)") return } - initialConfiguration = originalConfiguration.sessionConfiguration + initialConfiguration = parsedFile.configuration.sessionConfiguration configuration = initialConfiguration.builder() itemRefresh.isEnabled = true // allow for manual reconnection tableView.reloadData() diff --git a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift index 04a5be1e..c94168de 100644 --- a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift @@ -30,14 +30,6 @@ import SwiftyBeaver private let log = SwiftyBeaver.self class WizardHostViewController: UITableViewController, TableModelHost, Wizard { - private struct ParsedFile { - let url: URL - - let hostname: String - - let configuration: TunnelKitProvider.Configuration - } - @IBOutlet private weak var itemNext: UIBarButtonItem! private let existingHosts: [String] = { @@ -97,15 +89,12 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard { func setConfigurationURL(_ url: URL) throws { log.debug("Parsing configuration URL: \(url)") - let hostname: String - let configuration: TunnelKitProvider.Configuration do { - (hostname, configuration) = try TunnelKitProvider.Configuration.parsed(from: url) + parsedFile = try TunnelKitProvider.Configuration.parsed(from: url) } catch let e { log.error("Could not parse .ovpn configuration file: \(e)") throw e } - parsedFile = ParsedFile(url: url, hostname: hostname, configuration: configuration) } private func useSuggestedTitle() { diff --git a/Passepartout/Sources/VPN/TunnelKitProvider+FileConfiguration.swift b/Passepartout/Sources/VPN/TunnelKitProvider+FileConfiguration.swift index 7c70d081..8c34aace 100644 --- a/Passepartout/Sources/VPN/TunnelKitProvider+FileConfiguration.swift +++ b/Passepartout/Sources/VPN/TunnelKitProvider+FileConfiguration.swift @@ -29,6 +29,16 @@ import SwiftyBeaver private let log = SwiftyBeaver.self +struct ParsedFile { + let url: URL + + let hostname: String + + let configuration: TunnelKitProvider.Configuration + + let strippedLines: [String]? +} + extension TunnelKitProvider.Configuration { private struct Regex { static let proto = Utils.regex("^proto +(udp6?|tcp6?)") @@ -62,8 +72,9 @@ extension TunnelKitProvider.Configuration { static let blockEnd = Utils.regex("^<\\/[\\w\\-]+>") } - static func parsed(from url: URL, stripped: UnsafeMutablePointer<[String]>? = nil) throws -> (String, TunnelKitProvider.Configuration) { + static func parsed(from url: URL, returnsStripped: Bool = false) throws -> ParsedFile { let lines = try String(contentsOf: url).trimmedLines() + var strippedLines: [String]? = returnsStripped ? [] : nil var defaultProto: TunnelKitProvider.SocketType? var defaultPort: UInt16? @@ -94,7 +105,7 @@ extension TunnelKitProvider.Configuration { var strippedLine = line defer { if isHandled { - stripped?.pointee.append(strippedLine) + strippedLines?.append(strippedLine) } } @@ -311,7 +322,7 @@ extension TunnelKitProvider.Configuration { var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build()) builder.endpointProtocols = endpointProtocols - return (hostname, builder.build()) + return ParsedFile(url: url, hostname: hostname, configuration: builder.build(), strippedLines: strippedLines) } } diff --git a/PassepartoutTests-iOS/FileConfigurationTests.swift b/PassepartoutTests-iOS/FileConfigurationTests.swift index ead8d5b5..acd967dc 100644 --- a/PassepartoutTests-iOS/FileConfigurationTests.swift +++ b/PassepartoutTests-iOS/FileConfigurationTests.swift @@ -39,16 +39,15 @@ class FileConfigurationTests: XCTestCase { } func testPIA() throws { - let cfg = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary")).1 + let cfg = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary")).configuration XCTAssertEqual(cfg.sessionConfiguration.cipher, .aes128cbc) XCTAssertEqual(cfg.sessionConfiguration.digest, .sha1) } func testStripped() throws { - var lines: [String] = [] - _ = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"), stripped: &lines) - let cfg = lines.joined(separator: "\n") - print(cfg) + let lines = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"), returnsStripped: true).strippedLines! + let stripped = lines.joined(separator: "\n") + print(stripped) } private func url(withName name: String) -> URL {