From 3ddfa87b58c76e8eb4df9738a68742ed9461952f Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 3 Mar 2019 13:00:42 +0100 Subject: [PATCH] Disconnect before switching active profile Make sure that completionHandler is ALWAYS called, despite vpn object being nil. --- CHANGELOG.md | 4 +++ .../Scenes/ServiceViewController.swift | 13 +++---- Passepartout/Sources/VPN/GracefulVPN.swift | 36 +++++++++++++++---- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec0f50cc..aed94cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +- Profile sometimes not connecting right after add. + ## 1.0.2 Beta 1315 (2019-03-03) ### Fixed diff --git a/Passepartout-iOS/Scenes/ServiceViewController.swift b/Passepartout-iOS/Scenes/ServiceViewController.swift index 3b6ef0ee..20f303d6 100644 --- a/Passepartout-iOS/Scenes/ServiceViewController.swift +++ b/Passepartout-iOS/Scenes/ServiceViewController.swift @@ -185,12 +185,13 @@ class ServiceViewController: UIViewController, TableModelHost { } private func activateProfile() { - service.activateProfile(uncheckedProfile) - - reloadModel() - tableView.reloadData() - - vpn.disconnect(completionHandler: nil) + vpn.disconnect { (error) in + self.service.activateProfile(self.uncheckedProfile) + self.vpn.prepare(withProfile: self.uncheckedProfile) { + self.reloadModel() + self.tableView.reloadData() + } + } } @IBAction private func renameProfile() { diff --git a/Passepartout/Sources/VPN/GracefulVPN.swift b/Passepartout/Sources/VPN/GracefulVPN.swift index bd778f76..7c57abaf 100644 --- a/Passepartout/Sources/VPN/GracefulVPN.swift +++ b/Passepartout/Sources/VPN/GracefulVPN.swift @@ -59,14 +59,22 @@ class GracefulVPN { self.profile = profile log.info("Preparing...") service.clearVpnLastError() - vpn?.prepare(completionHandler: completionHandler) + guard let vpn = vpn else { + completionHandler?() + return + } + vpn.prepare(completionHandler: completionHandler) } func reconnect(completionHandler: ((Error?) -> Void)?) { service.clearVpnLastError() + guard let vpn = vpn else { + completionHandler?(nil) + return + } do { log.info("Reconnecting...") - try vpn?.reconnect(configuration: service.vpnConfiguration(), completionHandler: completionHandler) + try vpn.reconnect(configuration: service.vpnConfiguration(), completionHandler: completionHandler) } catch let e { log.error("Could not reconnect: \(e)") } @@ -74,9 +82,13 @@ class GracefulVPN { func reinstall(completionHandler: ((Error?) -> Void)?) { service.clearVpnLastError() + guard let vpn = vpn else { + completionHandler?(nil) + return + } do { log.info("Reinstalling...") - try vpn?.install(configuration: service.vpnConfiguration(), completionHandler: completionHandler) + try vpn.install(configuration: service.vpnConfiguration(), completionHandler: completionHandler) } catch let e { log.error("Could not reinstall: \(e)") } @@ -95,14 +107,26 @@ class GracefulVPN { } func disconnect(completionHandler: ((Error?) -> Void)?) { - vpn?.disconnect(completionHandler: completionHandler) + guard let vpn = vpn else { + completionHandler?(nil) + return + } + vpn.disconnect(completionHandler: completionHandler) } func uninstall(completionHandler: (() -> Void)?) { - vpn?.uninstall(completionHandler: completionHandler) + guard let vpn = vpn else { + completionHandler?() + return + } + vpn.uninstall(completionHandler: completionHandler) } func requestBytesCount(completionHandler: @escaping ((UInt, UInt)?) -> Void) { - vpn?.requestBytesCount(completionHandler: completionHandler) + guard let vpn = vpn else { + completionHandler(nil) + return + } + vpn.requestBytesCount(completionHandler: completionHandler) } }