Remove the feature of waiting for another tunnel to deactivate

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-12-11 03:24:28 +05:30
parent e59dbe6364
commit 1fd0c56f08
3 changed files with 11 additions and 59 deletions

View File

@ -269,14 +269,12 @@ class TunnelDetailTableViewStatusCell: UITableViewCell {
text = "Reactivating" text = "Reactivating"
case .restarting: case .restarting:
text = "Restarting" text = "Restarting"
case .waiting:
text = "Waiting"
} }
textLabel?.text = text textLabel?.text = text
DispatchQueue.main.async { [weak statusSwitch] in DispatchQueue.main.async { [weak statusSwitch] in
guard let statusSwitch = statusSwitch else { return } guard let statusSwitch = statusSwitch else { return }
statusSwitch.isOn = !(status == .deactivating || status == .inactive) statusSwitch.isOn = !(status == .deactivating || status == .inactive)
statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active || status == .waiting) statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
} }
textLabel?.textColor = (status == .active || status == .inactive) ? UIColor.black : UIColor.gray textLabel?.textColor = (status == .active || status == .inactive) ? UIColor.black : UIColor.gray
} }

View File

@ -390,7 +390,7 @@ class TunnelsListTableViewCell: UITableViewCell {
DispatchQueue.main.async { [weak statusSwitch, weak busyIndicator] in DispatchQueue.main.async { [weak statusSwitch, weak busyIndicator] in
guard let statusSwitch = statusSwitch, let busyIndicator = busyIndicator else { return } guard let statusSwitch = statusSwitch, let busyIndicator = busyIndicator else { return }
statusSwitch.isOn = !(status == .deactivating || status == .inactive) statusSwitch.isOn = !(status == .deactivating || status == .inactive)
statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active || status == .waiting) statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
if (status == .inactive || status == .active) { if (status == .inactive || status == .active) {
busyIndicator.stopAnimating() busyIndicator.stopAnimating()
} else { } else {

View File

@ -27,7 +27,7 @@ enum TunnelsManagerError: WireGuardAppError {
// Tunnel activation // Tunnel activation
case attemptingActivationWhenTunnelIsNotInactive case attemptingActivationWhenTunnelIsNotInactive
case tunnelActivationSupercededWhileWaiting // Another tunnel activation was initiated while this tunnel was waiting case attemptingActivationWhenAnotherTunnelIsOperational(otherTunnelName: String)
case tunnelActivationAttemptFailed // startTunnel() throwed case tunnelActivationAttemptFailed // startTunnel() throwed
case tunnelActivationFailedInternalError // startTunnel() succeeded, but activation failed case tunnelActivationFailedInternalError // startTunnel() succeeded, but activation failed
case tunnelActivationFailedNoInternetConnection // startTunnel() succeeded, but activation failed since no internet case tunnelActivationFailedNoInternetConnection // startTunnel() succeeded, but activation failed since no internet
@ -49,8 +49,8 @@ enum TunnelsManagerError: WireGuardAppError {
case .attemptingActivationWhenTunnelIsNotInactive: case .attemptingActivationWhenTunnelIsNotInactive:
return ("Activation failure", "The tunnel is already active or in the process of being activated") return ("Activation failure", "The tunnel is already active or in the process of being activated")
case .tunnelActivationSupercededWhileWaiting: case .attemptingActivationWhenAnotherTunnelIsOperational(let otherTunnelName):
return nil return ("Activation failure", "Please disconnect '\(otherTunnelName)' before enabling this tunnel.")
case .tunnelActivationAttemptFailed: case .tunnelActivationAttemptFailed:
return ("Activation failure", "The tunnel could not be activated due to an internal error") return ("Activation failure", "The tunnel could not be activated due to an internal error")
case .tunnelActivationFailedInternalError: case .tunnelActivationFailedInternalError:
@ -69,7 +69,6 @@ class TunnelsManager {
private var statusObservationToken: AnyObject? private var statusObservationToken: AnyObject?
var tunnelBeingActivated: TunnelContainer? var tunnelBeingActivated: TunnelContainer?
var tunnelWaitingForActivation: (tunnel: TunnelContainer, completionHandler: (TunnelsManagerError?) -> Void)?
init(tunnelProviders: [NETunnelProviderManager]) { init(tunnelProviders: [NETunnelProviderManager]) {
self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name } self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name }
@ -248,36 +247,16 @@ class TunnelsManager {
return return
} }
if let (waitingTunnel, waitingTunnelCompletionHandler) = self.tunnelWaitingForActivation { if let tunnelInOperation = tunnels.first(where: { $0.status != .inactive }) {
precondition(waitingTunnel.status == .waiting) completionHandler(TunnelsManagerError.attemptingActivationWhenAnotherTunnelIsOperational(otherTunnelName: tunnelInOperation.name))
self.tunnelWaitingForActivation = nil return
waitingTunnel.status = .inactive
waitingTunnelCompletionHandler(TunnelsManagerError.tunnelActivationSupercededWhileWaiting)
} }
if let tunnelInOperation = tunnels.first(where: { $0.status != .inactive && $0.status != .waiting }) { tunnelBeingActivated = tunnel
tunnel.status = .waiting tunnel.startActivation(completionHandler: completionHandler)
tunnelWaitingForActivation = (tunnel, completionHandler)
os_log("Tunnel '%{public}@' is waiting for deactivation of '%{public}@' (status: %{public}@)",
log: OSLog.default, type: .debug, tunnel.name, tunnelInOperation.name, "\(tunnelInOperation.status)")
if (tunnelInOperation.status == .active) {
tunnelBeingActivated = nil
startDeactivation(of: tunnelInOperation)
}
} else {
tunnelBeingActivated = tunnel
tunnel.startActivation(completionHandler: completionHandler)
}
} }
func startDeactivation(of tunnel: TunnelContainer) { func startDeactivation(of tunnel: TunnelContainer) {
if (tunnel.status == .waiting) {
let inferredStatus = TunnelStatus(from: tunnel.tunnelProvider.connection.status)
if (inferredStatus == .inactive) {
tunnel.status = .inactive
return
}
}
if (tunnel.status == .inactive || tunnel.status == .deactivating) { if (tunnel.status == .inactive || tunnel.status == .deactivating) {
return return
} }
@ -329,23 +308,6 @@ class TunnelsManager {
// Update tunnel status // Update tunnel status
tunnel.refreshStatus() tunnel.refreshStatus()
// In case some other tunnel is waiting on this tunnel's deactivation
if let (waitingTunnel, waitingTunnelCompletionHandler) = s.tunnelWaitingForActivation {
if (tunnel.status == .active) {
os_log("Deactivating tunnel '%{public}@' because tunnel '%{public}@' is waiting for activation",
log: OSLog.default, type: .debug, tunnel.name, waitingTunnel.name)
tunnel.startDeactivation()
} else if (tunnel.status == .inactive) {
os_log("Activating waiting tunnel '%{public}@' after deactivation of '%{public}@'",
log: OSLog.default, type: .debug, waitingTunnel.name, tunnel.name)
precondition(waitingTunnel.status == .waiting)
s.tunnelWaitingForActivation = nil
s.tunnelBeingActivated = waitingTunnel
waitingTunnel.startActivation(completionHandler: waitingTunnelCompletionHandler)
}
}
} }
} }
@ -393,7 +355,7 @@ class TunnelContainer: NSObject {
} }
fileprivate func startActivation(completionHandler: @escaping (TunnelsManagerError?) -> Void) { fileprivate func startActivation(completionHandler: @escaping (TunnelsManagerError?) -> Void) {
assert(status == .inactive || status == .restarting || status == .waiting) assert(status == .inactive || status == .restarting)
guard let tunnelConfiguration = tunnelConfiguration() else { fatalError() } guard let tunnelConfiguration = tunnelConfiguration() else { fatalError() }
@ -468,12 +430,6 @@ class TunnelContainer: NSObject {
fileprivate func startDeactivation() { fileprivate func startDeactivation() {
let session = (tunnelProvider.connection as! NETunnelProviderSession) let session = (tunnelProvider.connection as! NETunnelProviderSession)
if (status == .waiting && (session.status == .disconnected || session.status == .invalid)) {
status = .inactive
self.onDeactivationComplete?()
self.onDeactivationComplete = nil
return
}
session.stopTunnel() session.stopTunnel()
} }
} }
@ -486,7 +442,6 @@ class TunnelContainer: NSObject {
case reasserting // Not a possible state at present case reasserting // Not a possible state at present
case restarting // Restarting tunnel (done after saving modifications to an active tunnel) case restarting // Restarting tunnel (done after saving modifications to an active tunnel)
case waiting // Waiting for another tunnel to be brought down
init(from vpnStatus: NEVPNStatus) { init(from vpnStatus: NEVPNStatus) {
switch (vpnStatus) { switch (vpnStatus) {
@ -515,7 +470,6 @@ extension TunnelStatus: CustomDebugStringConvertible {
case .deactivating: return "deactivating" case .deactivating: return "deactivating"
case .reasserting: return "reasserting" case .reasserting: return "reasserting"
case .restarting: return "restarting" case .restarting: return "restarting"
case .waiting: return "waiting"
} }
} }
} }