Check and clear old configs on app start and app foreground.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
0c1520d6f8
commit
86646448ac
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue