2018-11-15 07:59:49 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-11-15 07:59:49 +00:00
|
|
|
|
2018-12-22 11:38:55 +00:00
|
|
|
import Foundation
|
2020-11-09 09:52:06 +00:00
|
|
|
import WireGuardKit
|
2018-11-15 07:59:49 +00:00
|
|
|
|
2018-12-06 13:35:46 +00:00
|
|
|
enum ZipExporterError: WireGuardAppError {
|
2018-11-15 07:59:49 +00:00
|
|
|
case noTunnelsToExport
|
2018-12-06 13:35:46 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
var alertText: AlertText {
|
2018-12-18 11:00:16 +00:00
|
|
|
return (tr("alertNoTunnelsToExportTitle"), tr("alertNoTunnelsToExportMessage"))
|
2018-12-06 13:35:46 +00:00
|
|
|
}
|
2018-11-15 07:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ZipExporter {
|
2018-12-13 03:09:52 +00:00
|
|
|
static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, completion: @escaping (WireGuardAppError?) -> Void) {
|
2018-11-15 07:59:49 +00:00
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
guard !tunnelConfigurations.isEmpty else {
|
2018-11-15 08:07:59 +00:00
|
|
|
completion(ZipExporterError.noTunnelsToExport)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
|
|
var inputsToArchiver: [(fileName: String, contents: Data)] = []
|
|
|
|
var lastTunnelName: String = ""
|
|
|
|
for tunnelConfiguration in tunnelConfigurations {
|
2018-12-21 21:16:09 +00:00
|
|
|
if let contents = tunnelConfiguration.asWgQuickConfig().data(using: .utf8) {
|
2018-12-21 23:28:18 +00:00
|
|
|
let name = tunnelConfiguration.name ?? "untitled"
|
2018-12-12 18:28:27 +00:00
|
|
|
if name.isEmpty || name == lastTunnelName { continue }
|
2018-11-15 08:07:59 +00:00
|
|
|
inputsToArchiver.append((fileName: "\(name).conf", contents: contents))
|
|
|
|
lastTunnelName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try ZipArchive.archive(inputs: inputsToArchiver, to: url)
|
2018-12-12 18:28:27 +00:00
|
|
|
} catch let error as WireGuardAppError {
|
2018-12-06 13:35:46 +00:00
|
|
|
DispatchQueue.main.async { completion(error) }
|
2018-11-15 08:07:59 +00:00
|
|
|
return
|
2018-12-06 13:35:46 +00:00
|
|
|
} catch {
|
|
|
|
fatalError()
|
2018-11-15 07:59:49 +00:00
|
|
|
}
|
2018-11-15 08:07:59 +00:00
|
|
|
DispatchQueue.main.async { completion(nil) }
|
2018-11-15 07:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|