aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-14 18:52:10 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-14 18:52:10 +0530
commit203d6b459610de0111eb57280f92f4d29eee32ab (patch)
tree72bc91ef79caf6fb505673cf2864778b3f7e7116 /WireGuard/WireGuard/ZipArchive/ZipImporter.swift
parentTunnels manager: After saving after activating on-demand, reload tunnel (diff)
downloadwireguard-apple-203d6b459610de0111eb57280f92f4d29eee32ab.tar.xz
wireguard-apple-203d6b459610de0111eb57280f92f4d29eee32ab.zip
Importing: Refactor out zip importing into a separate class
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive/ZipImporter.swift')
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipImporter.swift48
1 files changed, 48 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
new file mode 100644
index 00000000..7ee5c32f
--- /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<TunnelConfiguration?>(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
+ }
+}