From b81943aaac082d8b3d6b4f1cab6ff9f1fa449413 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 3 Nov 2018 06:22:57 +0100 Subject: [PATCH] TunnelsManager: do not track currentTunnel It's error prone and does not give us anything. --- WireGuard/WireGuard/VPN/TunnelsManager.swift | 39 +++----------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index eb69ccd..2aa415a 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -38,23 +38,8 @@ class TunnelsManager { private var isModifyingTunnel: Bool = false private var isDeletingTunnel: Bool = false - private var currentTunnel: TunnelContainer? - private var currentTunnelStatusObservationToken: AnyObject? - init(tunnelProviders: [NETunnelProviderManager]) { - var tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) } - tunnels.sort { $0.name < $1.name } - var currentTunnel: TunnelContainer? = nil - for tunnel in tunnels { - if (tunnel.status != .inactive) { - currentTunnel = tunnel - break - } - } - self.tunnels = tunnels - if let currentTunnel = currentTunnel { - setCurrentTunnel(tunnel: currentTunnel) - } + self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name } } static func create(completionHandler: @escaping (TunnelsManager?) -> Void) { @@ -203,11 +188,12 @@ class TunnelsManager { completionHandler(TunnelActivationError.attemptingActivationWhenTunnelIsNotInactive) return } - guard (currentTunnel == nil) else { - completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: currentTunnel!.status)) - return + for t in tunnels { + if t.status != .inactive { + completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: t.status)) + return + } } - setCurrentTunnel(tunnel: tunnel) tunnel.startActivation(completionHandler: completionHandler) } @@ -216,21 +202,8 @@ class TunnelsManager { completionHandler(TunnelActivationError.attemptingDeactivationWhenTunnelIsInactive) return } - assert(tunnel == currentTunnel!) - tunnel.startDeactivation() } - - private func setCurrentTunnel(tunnel: TunnelContainer) { - currentTunnel = tunnel - currentTunnelStatusObservationToken = tunnel.observe(\.status) { [weak self] (tunnel, change) in - guard let s = self else { return } - if (tunnel.status == .inactive) { - s.currentTunnel = nil - s.currentTunnelStatusObservationToken = nil - } - } - } } extension NETunnelProviderProtocol {