From 179a5b0519537212bbe56e0f74b7ae319cb97bb2 Mon Sep 17 00:00:00 2001 From: Sander Bruens Date: Tue, 18 Jul 2023 21:21:34 -0400 Subject: [PATCH] Use the `NETunnelProviderManager` to determine whether Outline was connected and the launcher should launch the application. --- .../ios/Outline.xcodeproj/project.pbxproj | 2 + .../ios/OutlineLauncher/AppDelegate.swift | 48 ++++++++++++------- .../OutlineLauncher.entitlements | 6 +++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/cordova/apple/xcode/ios/Outline.xcodeproj/project.pbxproj b/src/cordova/apple/xcode/ios/Outline.xcodeproj/project.pbxproj index 2ce1a8eacea..f17a58bbdce 100755 --- a/src/cordova/apple/xcode/ios/Outline.xcodeproj/project.pbxproj +++ b/src/cordova/apple/xcode/ios/Outline.xcodeproj/project.pbxproj @@ -931,6 +931,7 @@ CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_ENTITLEMENTS = OutlineLauncher/OutlineLauncher.entitlements; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; @@ -988,6 +989,7 @@ CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_ENTITLEMENTS = OutlineLauncher/OutlineLauncher.entitlements; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; diff --git a/src/cordova/apple/xcode/ios/OutlineLauncher/AppDelegate.swift b/src/cordova/apple/xcode/ios/OutlineLauncher/AppDelegate.swift index 431b27fd416..9a43c23bb95 100644 --- a/src/cordova/apple/xcode/ios/OutlineLauncher/AppDelegate.swift +++ b/src/cordova/apple/xcode/ios/OutlineLauncher/AppDelegate.swift @@ -13,6 +13,7 @@ // limitations under the License. import Cocoa +import NetworkExtension import OutlineTunnel import UIKit @@ -22,28 +23,39 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let appKitBundle = AppKitBundleLoader() - defer { - NSLog("Exiting...") - appKitBundle.appKitBridge!.terminate() - } - - if !self.shouldLaunchMainApp() { - NSLog("Not launching, Outline not connected at shutdown") - return false - } - NSLog("Outline connected at shutdown. Launching") - - guard let launcherBundleId = Bundle.main.bundleIdentifier else { - NSLog("Failed to retrieve the bundle ID for the launcher app.") - return false + self.shouldLaunchMainApp() { shouldLaunch in + defer { + NSLog("Exiting launcher...") + appKitBundle.appKitBridge!.terminate() + } + if !shouldLaunch { + NSLog("Not launching, Outline not connected at shutdown") + return + } + NSLog("Outline connected at shutdown. Launching") + + guard let launcherBundleId = Bundle.main.bundleIdentifier else { + NSLog("Failed to retrieve the bundle ID for the launcher app.") + return + } + appKitBundle.appKitBridge!.loadMainApp(launcherBundleId) } - appKitBundle.appKitBridge!.loadMainApp(launcherBundleId) return true } // Returns whether the launcher should launch the main app. - private func shouldLaunchMainApp() -> Bool { - let tunnelStore = OutlineTunnelStore(appGroup: AppDelegate.kAppGroup) - return tunnelStore.status == OutlineTunnel.TunnelStatus.connected + private func shouldLaunchMainApp(completion: @escaping(Bool) -> Void) { + NETunnelProviderManager.loadAllFromPreferences() { (managers, error) in + guard error == nil, managers != nil else { + NSLog("Failed to get tunnel manager: \(String(describing: error))") + return completion(false) + } + guard let manager: NETunnelProviderManager = managers!.first, managers!.count > 0 else { + NSLog("No tunnel managers found.") + return completion(false) + } + NSLog("Tunnel manager found.") + return completion(manager.isOnDemandEnabled) + } } } diff --git a/src/cordova/apple/xcode/ios/OutlineLauncher/OutlineLauncher.entitlements b/src/cordova/apple/xcode/ios/OutlineLauncher/OutlineLauncher.entitlements index 3c02119f35c..c58245a4e21 100644 --- a/src/cordova/apple/xcode/ios/OutlineLauncher/OutlineLauncher.entitlements +++ b/src/cordova/apple/xcode/ios/OutlineLauncher/OutlineLauncher.entitlements @@ -2,6 +2,12 @@ + com.apple.developer.networking.networkextension + + packet-tunnel-provider + app-proxy-provider + content-filter-provider + com.apple.security.app-sandbox com.apple.security.application-groups