From 6ad3487a9df148db32ab8b3e843081efb08a874d Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Fri, 25 Jan 2019 18:14:48 +0530 Subject: macOS: Delay .deactivated status to workaround system bug For some time after it's connection status becomes .disconnected, if a tunnel gets started, it gets automatically killed by the system after ~25 seconds. Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'WireGuard/WireGuard') diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index 7b1a9af..29d486a 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -388,6 +388,7 @@ class TunnelContainer: NSObject { } var activationAttemptId: String? var activationTimer: Timer? + var deactivationTimer: Timer? fileprivate var tunnelProvider: NETunnelProviderManager @@ -426,8 +427,22 @@ class TunnelContainer: NSObject { } func refreshStatus() { - let status = TunnelStatus(from: tunnelProvider.connection.status) - self.status = status + #if os(macOS) + // In macOS, we wait for a few seconds after deactivation to work around a system bug. + // If a tunnel gets activated in this time interval, it's stopped by the system automatically in ~25 seconds. + if self.status == .deactivating && tunnelProvider.connection.status == .disconnected { + self.deactivationTimer?.invalidate() + let deactivationTimer = Timer(timeInterval: 5 /* seconds */, repeats: false) { [weak self] _ in + guard let self = self else { return } + self.status = TunnelStatus(from: self.tunnelProvider.connection.status) + self.isActivateOnDemandEnabled = self.tunnelProvider.isOnDemandEnabled + } + self.deactivationTimer = deactivationTimer + RunLoop.main.add(deactivationTimer, forMode: .default) + return + } + #endif + status = TunnelStatus(from: tunnelProvider.connection.status) isActivateOnDemandEnabled = tunnelProvider.isOnDemandEnabled } -- cgit v1.2.3-59-g8ed1b