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)
}
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 {

View File

@ -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()

View File

@ -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() {

View File

@ -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)
}
}

View File

@ -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 {