Check and clear old configs on app start and app foreground.

This commit is contained in:
Jeroen Leenarts 2018-10-01 15:36:11 +02:00
parent 65220e8d81
commit 07be1c5398
2 changed files with 44 additions and 2 deletions

View File

@ -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()
}
}

View File

@ -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