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)
}
}
guard url.pathExtension == "conf" else { return false }
do {
try appCoordinator.importConfig(config: url)
} catch {
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
return false
if url.pathExtension == "conf" {
do {
try appCoordinator.importConfig(config: url)
} catch {
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
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 {
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 tunnel = try Tunnel.fromConfig(configString, context: addContext)
let title = config.deletingPathExtension().lastPathComponent
tunnel.title = title
addContext.saveContext()
self.saveTunnel(tunnel)
} catch {
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

View File

@ -79,11 +79,6 @@
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
@ -97,19 +92,19 @@
</array>
<key>UTTypeIdentifier</key>
<string>com.wireguard.config.quick</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
</dict>
</array>
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>conf</string>
</dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
<string>com.pkware.zip-archive</string>
</array>
<key>UTTypeDescription</key>
<string>WireGuard configuration</string>
@ -120,6 +115,28 @@
</array>
<key>UTTypeIdentifier</key>
<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>
</array>
</dict>