aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-11-03 02:51:32 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-11-03 03:43:09 +0100
commit95101dce5cf230e398eb04b50d441723f6101869 (patch)
tree9689cb2d399c8ffb8c4ed48be335827dd322eabd
parentTunnel detail: In the key-value cell, make the value label scrollable horizontally (diff)
downloadwireguard-apple-95101dce5cf230e398eb04b50d441723f6101869.tar.xz
wireguard-apple-95101dce5cf230e398eb04b50d441723f6101869.zip
Import: rework addMultiple logic
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift56
-rw-r--r--WireGuard/WireGuard/VPN/TunnelsManager.swift23
2 files changed, 43 insertions, 36 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
index 5804973..9515885 100644
--- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
@@ -145,40 +145,50 @@ class TunnelsListTableViewController: UITableViewController {
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")
+ 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")
+ showErrorAlert(title: "Unable to read zip archive", message: "Bad or corrupt zip archive.")
+ return
} catch (let error) {
showErrorAlert(title: "Unable to read zip archive", message: "Unexpected error: \(String(describing: error))")
+ return
}
- var numberOfConfigFilesWithErrors = 0
- var tunnelConfigurationsToAdd: [TunnelConfiguration] = []
- for unarchivedFile in unarchivedFiles {
- guard let tunnelsManager = tunnelsManager else { return }
- if let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent,
- (!tunnelsManager.containsTunnel(named: fileBaseName)),
- let fileContents = String(data: unarchivedFile.contents, encoding: .utf8),
- let tunnelConfiguration = try? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) {
- tunnelConfigurationsToAdd.append(tunnelConfiguration)
+
+ for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() {
+ if let trimmedName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent, !trimmedName.isEmpty {
+ unarchivedFiles[i].fileName = trimmedName
} else {
- numberOfConfigFilesWithErrors = numberOfConfigFilesWithErrors + 1
+ unarchivedFiles.remove(at: i)
}
}
- guard (tunnelConfigurationsToAdd.count > 0) else {
- showErrorAlert(title: "No configurations found", message: "Zip archive does not contain any valid .conf files")
+ if (unarchivedFiles.isEmpty) {
+ showErrorAlert(title: "No tunnels in zip archive", message: "No .conf tunnel files were found inside the zip archive.")
return
}
- var numberOfTunnelsRemainingAfterError = 0
- tunnelsManager?.addMultiple(tunnelConfigurations: tunnelConfigurationsToAdd) { (numberOfTunnelsRemaining, error) in
- if (error != nil) {
- numberOfTunnelsRemainingAfterError = numberOfTunnelsRemaining
- } else {
- assert(numberOfTunnelsRemaining == 0)
+ 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)
}
- if (numberOfConfigFilesWithErrors > 0) {
- showErrorAlert(title: "Created \(unarchivedFiles.count) tunnels",
- message: "Created \(numberOfTunnelsRemainingAfterError) of \(unarchivedFiles.count) tunnels from files in zip archive")
+ tunnelsManager.addMultiple(tunnelConfigurations: configs) { [weak self] (numberSuccessful) in
+ if numberSuccessful == unarchivedFiles.count {
+ return
+ }
+ self?.showErrorAlert(title: "Created \(numberSuccessful) tunnels",
+ message: "Created \(numberSuccessful) of \(unarchivedFiles.count) tunnels from zip archive")
}
}
}
diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift
index 7d5dd50..5015be0 100644
--- a/WireGuard/WireGuard/VPN/TunnelsManager.swift
+++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift
@@ -119,23 +119,20 @@ class TunnelsManager {
}
}
- func addMultiple(tunnelConfigurations: [TunnelConfiguration], completionHandler: @escaping (Int, TunnelManagementError?) -> Void) {
- addMultiple(tunnelConfigurations: tunnelConfigurations[0...], completionHandler: completionHandler)
+ func addMultiple(tunnelConfigurations: [TunnelConfiguration], completionHandler: @escaping (UInt) -> Void) {
+ addMultiple(tunnelConfigurations: tunnelConfigurations[0...], numberSuccessful: 0, completionHandler: completionHandler)
}
- private func addMultiple(tunnelConfigurations: ArraySlice<TunnelConfiguration>, completionHandler: @escaping (Int, TunnelManagementError?) -> Void) {
- assert(!tunnelConfigurations.isEmpty)
+ private func addMultiple(tunnelConfigurations: ArraySlice<TunnelConfiguration>, numberSuccessful: UInt, completionHandler: @escaping (UInt) -> Void) {
+ if tunnelConfigurations.isEmpty {
+ completionHandler(numberSuccessful)
+ return
+ }
let head = tunnelConfigurations.first!
- let tail = tunnelConfigurations[1 ..< tunnelConfigurations.count]
+ let tail = tunnelConfigurations[1...]
self.add(tunnelConfiguration: head) { [weak self] (tunnel, error) in
- if (error != nil) {
- completionHandler(tail.count, error)
- } else if (tail.isEmpty) {
- completionHandler(0, nil)
- } else {
- DispatchQueue.main.async {
- self?.addMultiple(tunnelConfigurations: tail, completionHandler: completionHandler)
- }
+ DispatchQueue.main.async {
+ self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessful + (error == nil ? 1 : 0), completionHandler: completionHandler)
}
}
}