aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
blob: d0cf9a7ec65a4caf306c4aa54fb37a189931e1fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
    }
}