diff options
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive/ZipExporter.swift')
-rw-r--r-- | WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift new file mode 100644 index 0000000..d0cf9a7 --- /dev/null +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import UIKit + +enum ZipExporterError: Error { + case noTunnelsToExport +} + +class ZipExporter { + static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to destinationURL: URL) throws { + + guard (!tunnelConfigurations.isEmpty) else { throw ZipExporterError.noTunnelsToExport } + + var inputsToArchiver: [(fileName: String, contents: Data)] = [] + + var lastTunnelName: String = "" + for tunnelConfiguration in tunnelConfigurations { + if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { + let name = tunnelConfiguration.interface.name + if (name.isEmpty || name == lastTunnelName) { continue } + inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) + lastTunnelName = name + } + } + try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL) + } +} |