diff options
author | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-10-01 15:36:11 +0200 |
---|---|---|
committer | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-10-01 15:36:11 +0200 |
commit | 86646448acfe5f5ebafe56e2e7e5cbd95f873e39 (patch) | |
tree | 408569aac006f53d0e6b5f10af93019c05e0b503 /WireGuard | |
parent | Convert DNS names to IP address. (diff) | |
download | wireguard-apple-86646448acfe5f5ebafe56e2e7e5cbd95f873e39.tar.xz wireguard-apple-86646448acfe5f5ebafe56e2e7e5cbd95f873e39.zip |
Check and clear old configs on app start and app foreground.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuard')
-rw-r--r-- | WireGuard/AppDelegate.swift | 10 | ||||
-rw-r--r-- | WireGuard/Coordinators/AppCoordinator.swift | 36 |
2 files changed, 44 insertions, 2 deletions
diff --git a/WireGuard/AppDelegate.swift b/WireGuard/AppDelegate.swift index 6b3eee8..44464f8 100644 --- a/WireGuard/AppDelegate.swift +++ b/WireGuard/AppDelegate.swift @@ -14,8 +14,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) - self.appCoordinator = AppCoordinator(window: self.window!) - self.appCoordinator.start() + appCoordinator = AppCoordinator(window: self.window!) + appCoordinator.start() + + appCoordinator.checkAndCleanConfigs() return true } @@ -48,4 +50,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return false } + + func applicationWillEnterForeground(_ application: UIApplication) { + appCoordinator.checkAndCleanConfigs() + } } diff --git a/WireGuard/Coordinators/AppCoordinator.swift b/WireGuard/Coordinators/AppCoordinator.swift index 923e9ae..9554b33 100644 --- a/WireGuard/Coordinators/AppCoordinator.swift +++ b/WireGuard/Coordinators/AppCoordinator.swift @@ -142,6 +142,42 @@ class AppCoordinator: RootViewCoordinator { } } + func checkAndCleanConfigs() { + _ = refreshProviderManagers().then { () -> Promise<Void> in + guard let providerManagers = self.providerManagers else { + return Promise.value(()) + } + let tunnels = try Tunnel.allInContext(self.persistentContainer.viewContext) + let tunnelIdentifiers = tunnels.compactMap {$0.tunnelIdentifier} + + let unknownManagers = providerManagers.filter { + guard let prot = $0.protocolConfiguration as? NETunnelProviderProtocol else { + return false + } + guard let candidateTunnelIdentifier = prot.providerConfiguration?[PCKeys.tunnelIdentifier.rawValue] as? String else { + return false + } + + return !tunnelIdentifiers.contains(candidateTunnelIdentifier) + } + + let deletionPromises = unknownManagers.map({ (manager) -> Promise<NETunnelProviderManager> in + return Promise(resolver: { resolver in + return manager.removeFromPreferences(completionHandler: { (error) in + if let error = error { + resolver.reject(error) + } else { + resolver.fulfill(manager) + } + }) + }) + }) + + return when(resolved: deletionPromises).asVoid() + + } + } + // swiftlint:disable next function_body_length func exportConfigs(sourceView: UIView) { guard let path = FileManager.default |