From 439f8f5a150cfb3384fd6c06dfc47a74e481d7c8 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 31 Oct 2018 14:29:54 +0530 Subject: [PATCH] VPN: Support adding multiple configurations in one shot Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/VPN/TunnelsManager.swift | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index d9b2195..79d5ae8 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -97,6 +97,27 @@ class TunnelsManager { } } + func addMultiple(tunnelConfigurations: [TunnelConfiguration], completionHandler: @escaping (Int, Error?) -> Void) { + addMultiple(tunnelConfigurations: tunnelConfigurations[0...], completionHandler: completionHandler) + } + + private func addMultiple(tunnelConfigurations: ArraySlice, completionHandler: @escaping (Int, Error?) -> Void) { + assert(!tunnelConfigurations.isEmpty) + let head = tunnelConfigurations.first! + let tail = tunnelConfigurations[1 ..< tunnelConfigurations.count] + self.add(tunnelConfiguration: head) { [weak self] (tunnel, error) in + if (error != nil) { + completionHandler(tail.count, error) + } else if (tail.isEmpty) { + completionHandler(0, nil) + } else { + DispatchQueue.main.async { + self?.addMultiple(tunnelConfigurations: tail, completionHandler: completionHandler) + } + } + } + } + func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) { let tunnelName = tunnelConfiguration.interface.name assert(!tunnelName.isEmpty)