From 8c3557a90723c20329cbdc7eff676787bfcd5872 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 4 Feb 2019 07:37:26 +0100 Subject: Keychain: store configurations in keychain instead of providerConfig Signed-off-by: Jason A. Donenfeld --- WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'WireGuard/WireGuard/Tunnel') diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index 6bcf6f7..e10ba77 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -44,12 +44,21 @@ class TunnelsManager { return } - let tunnelManagers = managers ?? [] - tunnelManagers.forEach { tunnelManager in - if (tunnelManager.protocolConfiguration as? NETunnelProviderProtocol)?.migrateConfigurationIfNeeded() == true { + var tunnelManagers = managers ?? [] + var refs: Set = [] + for (index, tunnelManager) in tunnelManagers.enumerated().reversed() { + let proto = tunnelManager.protocolConfiguration as? NETunnelProviderProtocol + if proto?.migrateConfigurationIfNeeded(called: tunnelManager.localizedDescription ?? "unknown") ?? false { tunnelManager.saveToPreferences { _ in } } + if let ref = proto?.verifyConfigurationReference() { + refs.insert(ref) + } else { + tunnelManager.removeFromPreferences { _ in } + tunnelManagers.remove(at: index) + } } + Keychain.deleteReferences(except: refs) completionHandler(.success(TunnelsManager(tunnelProviders: tunnelManagers))) } #endif @@ -105,6 +114,7 @@ class TunnelsManager { tunnelProviderManager.saveToPreferences { [weak self] error in guard error == nil else { wg_log(.error, message: "Add: Saving configuration failed: \(error!)") + (tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.destroyConfigurationReference() completionHandler(.failure(TunnelsManagerError.systemErrorOnAddTunnel(systemError: error!))) return } @@ -153,7 +163,7 @@ class TunnelsManager { tunnel.name = tunnelName } - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) + tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, previouslyFrom: tunnelProviderManager.protocolConfiguration) tunnelProviderManager.localizedDescription = tunnelConfiguration.name tunnelProviderManager.isEnabled = true @@ -162,6 +172,7 @@ class TunnelsManager { tunnelProviderManager.saveToPreferences { [weak self] error in guard error == nil else { + //TODO: the passwordReference for the old one has already been removed at this point and we can't easily roll back! wg_log(.error, message: "Modify: Saving configuration failed: \(error!)") completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error!)) return @@ -202,6 +213,7 @@ class TunnelsManager { func remove(tunnel: TunnelContainer, completionHandler: @escaping (TunnelsManagerError?) -> Void) { let tunnelProviderManager = tunnel.tunnelProvider + (tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.destroyConfigurationReference() tunnelProviderManager.removeFromPreferences { [weak self] error in guard error == nil else { -- cgit v1.2.3-59-g8ed1b