From 3bd611aa7c2f4fb8ea24cfcc94826eec393c75f1 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Fri, 5 Apr 2019 13:29:17 +0530 Subject: [PATCH] TunnelsManager: Cache isTunnelConfigurationAvailableInKeychain Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index e50e458..19aeaef 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -465,7 +465,7 @@ class TunnelContainer: NSObject { } var isTunnelConfigurationAvailableInKeychain: Bool { - return (tunnelProvider.protocolConfiguration as? NETunnelProviderProtocol)?.verifyConfigurationReference() ?? false + return tunnelProvider.isTunnelConfigurationAvailableInKeychain } var onDemandOption: ActivateOnDemandOption { @@ -580,7 +580,18 @@ class TunnelContainer: NSObject { } extension NETunnelProviderManager { + private static var cachedIsConfigAvailableInKeychainKey: UInt8 = 0 private static var cachedConfigKey: UInt8 = 0 + + var isTunnelConfigurationAvailableInKeychain: Bool { + if let cachedNumber = objc_getAssociatedObject(self, &NETunnelProviderManager.cachedIsConfigAvailableInKeychainKey) as? NSNumber { + return cachedNumber.boolValue + } + let isAvailable = (protocolConfiguration as? NETunnelProviderProtocol)?.verifyConfigurationReference() ?? false + objc_setAssociatedObject(self, &NETunnelProviderManager.cachedIsConfigAvailableInKeychainKey, NSNumber(value: isAvailable), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) + return isAvailable + } + var tunnelConfiguration: TunnelConfiguration? { if let cached = objc_getAssociatedObject(self, &NETunnelProviderManager.cachedConfigKey) as? TunnelConfiguration { return cached