TunnelsManager: do not track currentTunnel

It's error prone and does not give us anything.
This commit is contained in:
Jason A. Donenfeld 2018-11-03 06:22:57 +01:00
parent 8bbc49015c
commit b81943aaac
1 changed files with 6 additions and 33 deletions

View File

@ -38,23 +38,8 @@ class TunnelsManager {
private var isModifyingTunnel: Bool = false private var isModifyingTunnel: Bool = false
private var isDeletingTunnel: Bool = false private var isDeletingTunnel: Bool = false
private var currentTunnel: TunnelContainer?
private var currentTunnelStatusObservationToken: AnyObject?
init(tunnelProviders: [NETunnelProviderManager]) { init(tunnelProviders: [NETunnelProviderManager]) {
var tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) } self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name }
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)
}
} }
static func create(completionHandler: @escaping (TunnelsManager?) -> Void) { static func create(completionHandler: @escaping (TunnelsManager?) -> Void) {
@ -203,11 +188,12 @@ class TunnelsManager {
completionHandler(TunnelActivationError.attemptingActivationWhenTunnelIsNotInactive) completionHandler(TunnelActivationError.attemptingActivationWhenTunnelIsNotInactive)
return return
} }
guard (currentTunnel == nil) else { for t in tunnels {
completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: currentTunnel!.status)) if t.status != .inactive {
return completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: t.status))
return
}
} }
setCurrentTunnel(tunnel: tunnel)
tunnel.startActivation(completionHandler: completionHandler) tunnel.startActivation(completionHandler: completionHandler)
} }
@ -216,21 +202,8 @@ class TunnelsManager {
completionHandler(TunnelActivationError.attemptingDeactivationWhenTunnelIsInactive) completionHandler(TunnelActivationError.attemptingDeactivationWhenTunnelIsInactive)
return return
} }
assert(tunnel == currentTunnel!)
tunnel.startDeactivation() 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 { extension NETunnelProviderProtocol {