From af026c106e691623cddce875d1e1c64b1b2979ef Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 7 Feb 2019 18:18:04 +0530 Subject: [PATCH] TunnelsManager: When creating/modifying a tunnel, update the associated object --- WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index e10ba77..a2fc00b 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -105,8 +105,7 @@ class TunnelsManager { } let tunnelProviderManager = NETunnelProviderManager() - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) - tunnelProviderManager.localizedDescription = tunnelConfiguration.name + tunnelProviderManager.setTunnelConfiguration(tunnelConfiguration) tunnelProviderManager.isEnabled = true activateOnDemandSetting.apply(on: tunnelProviderManager) @@ -163,8 +162,7 @@ class TunnelsManager { tunnel.name = tunnelName } - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, previouslyFrom: tunnelProviderManager.protocolConfiguration) - tunnelProviderManager.localizedDescription = tunnelConfiguration.name + tunnelProviderManager.setTunnelConfiguration(tunnelConfiguration) tunnelProviderManager.isEnabled = true let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && activateOnDemandSetting.isActivateOnDemandEnabled @@ -178,7 +176,6 @@ class TunnelsManager { return } guard let self = self else { return } - if isNameChanged { let oldIndex = self.tunnels.firstIndex(of: tunnel)! self.tunnels.sort { $0.name < $1.name } @@ -549,4 +546,9 @@ extension NETunnelProviderManager { } return config } + func setTunnelConfiguration(_ tunnelConfiguration: TunnelConfiguration) { + protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, previouslyFrom: protocolConfiguration) + localizedDescription = tunnelConfiguration.name + objc_setAssociatedObject(self, &NETunnelProviderManager.cachedConfigKey, tunnelConfiguration, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } }