Import of zip.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts 2018-09-03 00:08:34 +02:00
parent 9bd251fe79
commit 56c543f1fc
3 changed files with 67 additions and 21 deletions

View File

@ -32,14 +32,24 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
os_log("Failed to remove item from Inbox: %{public}@", log: Log.general, type: .error, url.absoluteString) os_log("Failed to remove item from Inbox: %{public}@", log: Log.general, type: .error, url.absoluteString)
} }
} }
guard url.pathExtension == "conf" else { return false } if url.pathExtension == "conf" {
do {
do { try appCoordinator.importConfig(config: url)
try appCoordinator.importConfig(config: url) } catch {
} catch { os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString) return false
return false }
return true
} else if url.pathExtension == "zip" {
do {
try appCoordinator.importConfigs(configZip: url)
} catch {
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
return false
}
return true
} }
return true return false
} }
} }

View File

@ -99,17 +99,36 @@ class AppCoordinator: RootViewCoordinator {
func importConfig(config: URL) throws { func importConfig(config: URL) throws {
do { do {
let configString = try String(contentsOf: config) try importConfig(configString: try String(contentsOf: config), title: config.deletingPathExtension().lastPathComponent)
} catch {
throw AppCoordinatorError.configImportError(msg: "Failed")
}
}
func importConfig(configString: String, title: String) throws {
do {
let addContext = persistentContainer.newBackgroundContext() let addContext = persistentContainer.newBackgroundContext()
let tunnel = try Tunnel.fromConfig(configString, context: addContext) let tunnel = try Tunnel.fromConfig(configString, context: addContext)
let title = config.deletingPathExtension().lastPathComponent
tunnel.title = title tunnel.title = title
addContext.saveContext() addContext.saveContext()
self.saveTunnel(tunnel) self.saveTunnel(tunnel)
} catch { } catch {
throw AppCoordinatorError.configImportError(msg: "Failed") throw AppCoordinatorError.configImportError(msg: "Failed")
} }
}
func importConfigs(configZip: URL) throws {
if let archive = Archive(url: configZip, accessMode: .read) {
for entry in archive {
var entryData = Data(capacity: 0)
_ = try archive.extract(entry) { (data) in
entryData.append(data)
}
if let config = String(data: entryData, encoding: .utf8) {
try importConfig(configString: config, title: entry.path)
}
}
}
} }
// swiftlint:disable next function_body_length // swiftlint:disable next function_body_length

View File

@ -79,11 +79,6 @@
<key>UTExportedTypeDeclarations</key> <key>UTExportedTypeDeclarations</key>
<array> <array>
<dict> <dict>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
<key>UTTypeConformsTo</key> <key>UTTypeConformsTo</key>
<array> <array>
<string>public.data</string> <string>public.data</string>
@ -97,19 +92,19 @@
</array> </array>
<key>UTTypeIdentifier</key> <key>UTTypeIdentifier</key>
<string>com.wireguard.config.quick</string> <string>com.wireguard.config.quick</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
</dict> </dict>
</array> </array>
<key>UTImportedTypeDeclarations</key> <key>UTImportedTypeDeclarations</key>
<array> <array>
<dict> <dict>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
<key>UTTypeConformsTo</key> <key>UTTypeConformsTo</key>
<array> <array>
<string>public.data</string> <string>com.pkware.zip-archive</string>
</array> </array>
<key>UTTypeDescription</key> <key>UTTypeDescription</key>
<string>WireGuard configuration</string> <string>WireGuard configuration</string>
@ -120,6 +115,28 @@
</array> </array>
<key>UTTypeIdentifier</key> <key>UTTypeIdentifier</key>
<string>com.wireguard.config.quick</string> <string>com.wireguard.config.quick</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>WireGuard configurations</string>
<key>UTTypeIconFiles</key>
<array>
<string>icon_20pt.png</string>
<string>icon_60pt@3x.png</string>
</array>
<key>UTTypeIdentifier</key>
<string>com.wireguard.config.quick.confs</string>
<key>public.filename-extension</key>
<string>zip</string>
</dict> </dict>
</array> </array>
</dict> </dict>