Move ParsedFile out of WizardHostVC

This commit is contained in:
Davide De Rosa 2018-10-27 11:36:41 +02:00
parent 0d14349bca
commit 422c4da09c
5 changed files with 25 additions and 27 deletions

View File

@ -90,10 +90,9 @@ class IssueReporter: NSObject {
vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.debugLog, fileName: AppConstants.IssueReporter.Filenames.debugLog) vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.debugLog, fileName: AppConstants.IssueReporter.Filenames.debugLog)
} }
if let url = configurationURL { if let url = configurationURL {
var lines: [String] = []
do { do {
_ = try TunnelKitProvider.Configuration.parsed(from: url, stripped: &lines) let parsedFile = try TunnelKitProvider.Configuration.parsed(from: url, returnsStripped: true)
if let attachment = lines.joined(separator: "\n").data(using: .utf8) { if let attachment = parsedFile.strippedLines?.joined(separator: "\n").data(using: .utf8) {
vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.configuration, fileName: AppConstants.IssueReporter.Filenames.configuration) vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.configuration, fileName: AppConstants.IssueReporter.Filenames.configuration)
} }
} catch { } catch {

View File

@ -117,18 +117,18 @@ class ConfigurationViewController: UIViewController, TableModelHost {
// MARK: Actions // MARK: Actions
private func resetOriginalConfiguration() { private func resetOriginalConfiguration() {
guard let url = originalConfigurationURL else { guard let originalURL = originalConfigurationURL else {
log.warning("Resetting with no original configuration set? Bad table model?") log.warning("Resetting with no original configuration set? Bad table model?")
return return
} }
let originalConfiguration: TunnelKitProvider.Configuration let parsedFile: ParsedFile
do { do {
(_, originalConfiguration) = try TunnelKitProvider.Configuration.parsed(from: url) parsedFile = try TunnelKitProvider.Configuration.parsed(from: originalURL)
} catch let e { } catch let e {
log.error("Could not parse original configuration: \(e)") log.error("Could not parse original configuration: \(e)")
return return
} }
initialConfiguration = originalConfiguration.sessionConfiguration initialConfiguration = parsedFile.configuration.sessionConfiguration
configuration = initialConfiguration.builder() configuration = initialConfiguration.builder()
itemRefresh.isEnabled = true // allow for manual reconnection itemRefresh.isEnabled = true // allow for manual reconnection
tableView.reloadData() tableView.reloadData()

View File

@ -30,14 +30,6 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
class WizardHostViewController: UITableViewController, TableModelHost, Wizard { class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
private struct ParsedFile {
let url: URL
let hostname: String
let configuration: TunnelKitProvider.Configuration
}
@IBOutlet private weak var itemNext: UIBarButtonItem! @IBOutlet private weak var itemNext: UIBarButtonItem!
private let existingHosts: [String] = { private let existingHosts: [String] = {
@ -97,15 +89,12 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
func setConfigurationURL(_ url: URL) throws { func setConfigurationURL(_ url: URL) throws {
log.debug("Parsing configuration URL: \(url)") log.debug("Parsing configuration URL: \(url)")
let hostname: String
let configuration: TunnelKitProvider.Configuration
do { do {
(hostname, configuration) = try TunnelKitProvider.Configuration.parsed(from: url) parsedFile = try TunnelKitProvider.Configuration.parsed(from: url)
} catch let e { } catch let e {
log.error("Could not parse .ovpn configuration file: \(e)") log.error("Could not parse .ovpn configuration file: \(e)")
throw e throw e
} }
parsedFile = ParsedFile(url: url, hostname: hostname, configuration: configuration)
} }
private func useSuggestedTitle() { private func useSuggestedTitle() {

View File

@ -29,6 +29,16 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
struct ParsedFile {
let url: URL
let hostname: String
let configuration: TunnelKitProvider.Configuration
let strippedLines: [String]?
}
extension TunnelKitProvider.Configuration { extension TunnelKitProvider.Configuration {
private struct Regex { private struct Regex {
static let proto = Utils.regex("^proto +(udp6?|tcp6?)") static let proto = Utils.regex("^proto +(udp6?|tcp6?)")
@ -62,8 +72,9 @@ extension TunnelKitProvider.Configuration {
static let blockEnd = Utils.regex("^<\\/[\\w\\-]+>") 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() let lines = try String(contentsOf: url).trimmedLines()
var strippedLines: [String]? = returnsStripped ? [] : nil
var defaultProto: TunnelKitProvider.SocketType? var defaultProto: TunnelKitProvider.SocketType?
var defaultPort: UInt16? var defaultPort: UInt16?
@ -94,7 +105,7 @@ extension TunnelKitProvider.Configuration {
var strippedLine = line var strippedLine = line
defer { defer {
if isHandled { if isHandled {
stripped?.pointee.append(strippedLine) strippedLines?.append(strippedLine)
} }
} }
@ -311,7 +322,7 @@ extension TunnelKitProvider.Configuration {
var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build()) var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
builder.endpointProtocols = endpointProtocols builder.endpointProtocols = endpointProtocols
return (hostname, builder.build()) return ParsedFile(url: url, hostname: hostname, configuration: builder.build(), strippedLines: strippedLines)
} }
} }

View File

@ -39,16 +39,15 @@ class FileConfigurationTests: XCTestCase {
} }
func testPIA() throws { 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.cipher, .aes128cbc)
XCTAssertEqual(cfg.sessionConfiguration.digest, .sha1) XCTAssertEqual(cfg.sessionConfiguration.digest, .sha1)
} }
func testStripped() throws { func testStripped() throws {
var lines: [String] = [] let lines = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"), returnsStripped: true).strippedLines!
_ = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"), stripped: &lines) let stripped = lines.joined(separator: "\n")
let cfg = lines.joined(separator: "\n") print(stripped)
print(cfg)
} }
private func url(withName name: String) -> URL { private func url(withName name: String) -> URL {