From 203d6b459610de0111eb57280f92f4d29eee32ab Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 14 Nov 2018 18:52:10 +0530 Subject: Importing: Refactor out zip importing into a separate class Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift | 8 ++++ .../UI/iOS/TunnelsListTableViewController.swift | 52 ++++------------------ WireGuard/WireGuard/ZipArchive/ZipImporter.swift | 48 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 WireGuard/WireGuard/ZipArchive/ZipImporter.swift (limited to 'WireGuard/WireGuard') diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift index fd56588..abc0083 100644 --- a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift +++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift @@ -24,6 +24,14 @@ class ErrorPresenter { case TunnelActivationError.tunnelActivationFailed: return ("Activation failure", "The tunnel could not be activated due to an internal error") + // Importing a zip file + case ZipArchiveError.cantOpenInputZipFile: + return ("Unable to read zip archive", "The zip archive could not be read.") + case ZipArchiveError.badArchive: + return ("Unable to read zip archive", "Bad or corrupt zip archive.") + case ZipImporterError.noTunnelsInZipArchive: + return ("No tunnels in zip archive", "No .conf tunnel files were found inside the zip archive.") + default: os_log("ErrorPresenter: Error not presented: %{public}@", log: OSLog.default, type: .error, "\(error)") return nil diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index af06eb6..796c030 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -158,62 +158,28 @@ class TunnelsListTableViewController: UIViewController { } func importFromFile(url: URL) { + guard let tunnelsManager = tunnelsManager else { return } if (url.pathExtension == "zip") { - var unarchivedFiles: [(fileName: String, contents: Data)] = [] + let zipImporter = ZipImporter(url: url) + let configs: [TunnelConfiguration?] do { - unarchivedFiles = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"]) - } catch ZipArchiveError.cantOpenInputZipFile { - showErrorAlert(title: "Unable to read zip archive", message: "The zip archive could not be read.") - return - } catch ZipArchiveError.badArchive { - showErrorAlert(title: "Unable to read zip archive", message: "Bad or corrupt zip archive.") - return + configs = try zipImporter.importConfigFiles() } catch (let error) { - showErrorAlert(title: "Unable to read zip archive", message: "Unexpected error: \(String(describing: error))") - return - } - - for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() { - let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent - if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty { - unarchivedFiles[i].fileName = trimmedName - } else { - unarchivedFiles.remove(at: i) - } - } - if (unarchivedFiles.isEmpty) { - showErrorAlert(title: "No tunnels in zip archive", message: "No .conf tunnel files were found inside the zip archive.") + ErrorPresenter.showErrorAlert(error: error, from: self) return } - guard let tunnelsManager = tunnelsManager else { return } - unarchivedFiles.sort { $0.fileName < $1.fileName } - var lastFileName: String? - var configs: [TunnelConfiguration] = [] - for file in unarchivedFiles { - if file.fileName == lastFileName { - continue - } - lastFileName = file.fileName - guard let fileContents = String(data: file.contents, encoding: .utf8) else { - continue - } - guard let tunnelConfig = try? WgQuickConfigFileParser.parse(fileContents, name: file.fileName) else { - continue - } - configs.append(tunnelConfig) - } - tunnelsManager.addMultiple(tunnelConfigurations: configs) { [weak self] (numberSuccessful) in - if numberSuccessful == unarchivedFiles.count { + tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { [weak self] (numberSuccessful) in + if numberSuccessful == configs.count { return } self?.showErrorAlert(title: "Created \(numberSuccessful) tunnels", - message: "Created \(numberSuccessful) of \(unarchivedFiles.count) tunnels from zip archive") + message: "Created \(numberSuccessful) of \(configs.count) tunnels from zip archive") } } 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? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) { - tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in + tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in if let error = error { ErrorPresenter.showErrorAlert(error: error, from: self) } diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift new file mode 100644 index 0000000..7ee5c32 --- /dev/null +++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import UIKit + +enum ZipImporterError: Error { + case noTunnelsInZipArchive +} + +class ZipImporter { + let url: URL + init(url: URL) { + self.url = url + } + + func importConfigFiles() throws -> [TunnelConfiguration?] { + var unarchivedFiles: [(fileName: String, contents: Data)] = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"]) + + for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() { + let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent + if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty { + unarchivedFiles[i].fileName = trimmedName + } else { + unarchivedFiles.remove(at: i) + } + } + + if (unarchivedFiles.isEmpty) { + throw ZipImporterError.noTunnelsInZipArchive + } + + unarchivedFiles.sort { $0.fileName < $1.fileName } + var configs = Array(repeating: nil, count: unarchivedFiles.count) + for (i, file) in unarchivedFiles.enumerated() { + if (i > 0 && file == unarchivedFiles[i - 1]) { + continue + } + guard let fileContents = String(data: file.contents, encoding: .utf8) else { + continue + } + guard let tunnelConfig = try? WgQuickConfigFileParser.parse(fileContents, name: file.fileName) else { + continue + } + configs[i] = tunnelConfig + } + return configs + } +} -- cgit v1.2.3-59-g8ed1b