2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// AppContext.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 8/29/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
2024-12-02 09:40:25 +00:00
|
|
|
import UITesting
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@MainActor
|
2024-12-08 17:56:39 +00:00
|
|
|
public final class AppContext: ObservableObject, Sendable {
|
2024-09-23 13:02:26 +00:00
|
|
|
public let iapManager: IAPManager
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public let migrationManager: MigrationManager
|
2024-11-09 14:20:59 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
public let profileManager: ProfileManager
|
|
|
|
|
2024-10-13 09:36:34 +00:00
|
|
|
public let providerManager: ProviderManager
|
2024-10-10 22:24:06 +00:00
|
|
|
|
2024-12-06 10:24:51 +00:00
|
|
|
public let preferencesManager: PreferencesManager
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public let registry: Registry
|
|
|
|
|
|
|
|
public let tunnel: ExtendedTunnel
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
private let tunnelReceiptURL: URL
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
private var launchTask: Task<Void, Error>?
|
|
|
|
|
|
|
|
private var pendingTask: Task<Void, Never>?
|
2024-11-06 17:42:42 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable>
|
|
|
|
|
|
|
|
public init(
|
|
|
|
iapManager: IAPManager,
|
2024-11-14 10:02:26 +00:00
|
|
|
migrationManager: MigrationManager,
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager: ProfileManager,
|
2024-11-14 10:02:26 +00:00
|
|
|
providerManager: ProviderManager,
|
2024-12-06 10:24:51 +00:00
|
|
|
preferencesManager: PreferencesManager,
|
2024-11-14 10:02:26 +00:00
|
|
|
registry: Registry,
|
2024-11-17 10:34:43 +00:00
|
|
|
tunnel: ExtendedTunnel,
|
|
|
|
tunnelReceiptURL: URL
|
2024-09-23 13:02:26 +00:00
|
|
|
) {
|
|
|
|
self.iapManager = iapManager
|
2024-11-14 10:02:26 +00:00
|
|
|
self.migrationManager = migrationManager
|
2024-09-23 13:02:26 +00:00
|
|
|
self.profileManager = profileManager
|
2024-10-13 09:36:34 +00:00
|
|
|
self.providerManager = providerManager
|
2024-12-06 10:24:51 +00:00
|
|
|
self.preferencesManager = preferencesManager
|
2024-11-14 10:02:26 +00:00
|
|
|
self.registry = registry
|
|
|
|
self.tunnel = tunnel
|
2024-11-17 10:34:43 +00:00
|
|
|
self.tunnelReceiptURL = tunnelReceiptURL
|
2024-09-30 12:56:20 +00:00
|
|
|
subscriptions = []
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Observation
|
2024-11-05 09:41:02 +00:00
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
// invoked by AppDelegate
|
|
|
|
extension AppContext {
|
2024-11-05 09:41:02 +00:00
|
|
|
public func onApplicationActive() {
|
|
|
|
Task {
|
2024-11-10 16:51:28 +00:00
|
|
|
// TODO: ###, should handle AppError.couldNotLaunch (although extremely rare)
|
|
|
|
try await onForeground()
|
2024-11-05 09:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
// invoked on internal events
|
2024-09-23 13:02:26 +00:00
|
|
|
private extension AppContext {
|
2024-11-10 16:51:28 +00:00
|
|
|
func onLaunch() async throws {
|
|
|
|
pp_log(.app, .notice, "Application did launch")
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tRead and observe local profiles...")
|
2024-11-10 16:51:28 +00:00
|
|
|
try await profileManager.observeLocal()
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tObserve in-app events...")
|
2024-11-10 16:51:28 +00:00
|
|
|
iapManager.observeObjects()
|
|
|
|
await iapManager.reloadReceipt()
|
2024-11-09 14:20:59 +00:00
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tObserve eligible features...")
|
2024-11-09 14:20:59 +00:00
|
|
|
iapManager
|
|
|
|
.$eligibleFeatures
|
|
|
|
.removeDuplicates()
|
2024-11-10 16:51:28 +00:00
|
|
|
.sink { [weak self] eligible in
|
|
|
|
Task {
|
|
|
|
try await self?.onEligibleFeatures(eligible)
|
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tObserve changes in ProfileManager...")
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager
|
2024-09-30 12:56:20 +00:00
|
|
|
.didChange
|
|
|
|
.sink { [weak self] event in
|
|
|
|
switch event {
|
|
|
|
case .save(let profile):
|
2024-11-10 16:51:28 +00:00
|
|
|
Task {
|
|
|
|
try await self?.onSaveProfile(profile)
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
2024-11-10 16:51:28 +00:00
|
|
|
|
2024-11-14 18:42:38 +00:00
|
|
|
// copy release receipt to tunnel for TestFlight eligibility (once is enough, it won't change)
|
|
|
|
if let appReceiptURL = Bundle.main.appStoreProductionReceiptURL {
|
|
|
|
do {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.iap, .info, "\tCopy release receipt to tunnel...")
|
2024-11-14 18:42:38 +00:00
|
|
|
try? FileManager.default.removeItem(at: tunnelReceiptURL)
|
|
|
|
try FileManager.default.copyItem(at: appReceiptURL, to: tunnelReceiptURL)
|
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.iap, .error, "\tUnable to copy release receipt to tunnel: \(error)")
|
2024-11-14 18:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
do {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .info, "\tFetch providers index...")
|
2024-11-10 16:51:28 +00:00
|
|
|
try await providerManager.fetchIndex(from: API.shared)
|
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .error, "\tUnable to fetch providers index: \(error)")
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
func onForeground() async throws {
|
|
|
|
let didLaunch = try await waitForTasks()
|
|
|
|
guard !didLaunch else {
|
|
|
|
return // foreground is redundant after launch
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_log(.app, .notice, "Application did enter foreground")
|
|
|
|
pendingTask = Task {
|
|
|
|
do {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tRefresh local profiles observers...")
|
2024-11-10 16:51:28 +00:00
|
|
|
try await profileManager.observeLocal()
|
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .error, "\tUnable to re-observe local profiles: \(error)")
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
2024-11-14 18:42:38 +00:00
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
await iapManager.reloadReceipt()
|
|
|
|
}
|
|
|
|
await pendingTask?.value
|
|
|
|
pendingTask = nil
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
func onEligibleFeatures(_ features: Set<AppFeature>) async throws {
|
|
|
|
try await waitForTasks()
|
|
|
|
|
|
|
|
pp_log(.app, .notice, "Application did update eligible features")
|
|
|
|
pendingTask = Task {
|
2024-11-11 11:50:26 +00:00
|
|
|
|
|
|
|
// toggle sync based on .sharing eligibility
|
|
|
|
let isEligibleForSharing = features.contains(.sharing)
|
2024-11-10 16:51:28 +00:00
|
|
|
do {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .info, "\tRefresh remote profiles observers (eligible=\(isEligibleForSharing), CloudKit=\(isCloudKitEnabled))...")
|
2024-11-11 11:50:26 +00:00
|
|
|
try await profileManager.observeRemote(isEligibleForSharing && isCloudKitEnabled)
|
2024-11-10 16:51:28 +00:00
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.App.profiles, .error, "\tUnable to re-observe remote profiles: \(error)")
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
2024-11-19 18:44:31 +00:00
|
|
|
|
|
|
|
// refresh required profile features
|
|
|
|
pp_log(.App.profiles, .info, "\tReload profiles required features...")
|
|
|
|
profileManager.reloadRequiredFeatures()
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
|
|
|
await pendingTask?.value
|
|
|
|
pendingTask = nil
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 16:51:28 +00:00
|
|
|
func onSaveProfile(_ profile: Profile) async throws {
|
|
|
|
try await waitForTasks()
|
|
|
|
|
|
|
|
pp_log(.app, .notice, "Application did save profile (\(profile.id))")
|
2024-09-30 12:56:20 +00:00
|
|
|
guard profile.id == tunnel.currentProfile?.id else {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .debug, "\tProfile \(profile.id) is not current, do nothing")
|
2024-09-30 12:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
2024-11-10 16:51:28 +00:00
|
|
|
guard [.active, .activating].contains(tunnel.status) else {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .debug, "\tConnection is not active (\(tunnel.status)), do nothing")
|
2024-11-10 16:51:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pendingTask = Task {
|
2024-10-26 19:09:30 +00:00
|
|
|
do {
|
2024-11-10 16:51:28 +00:00
|
|
|
do {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .info, "\tReconnect profile \(profile.id)")
|
2024-11-10 16:51:28 +00:00
|
|
|
try await tunnel.connect(with: profile)
|
2024-12-01 21:34:41 +00:00
|
|
|
} catch AppError.interactiveLogin {
|
|
|
|
pp_log(.app, .info, "\tProfile \(profile.id) is interactive, disconnect")
|
|
|
|
try await tunnel.disconnect()
|
2024-11-10 16:51:28 +00:00
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .error, "\tUnable to reconnect profile \(profile.id), disconnect: \(error)")
|
2024-11-10 16:51:28 +00:00
|
|
|
try await tunnel.disconnect()
|
|
|
|
}
|
2024-10-26 19:09:30 +00:00
|
|
|
} catch {
|
2024-11-17 10:34:43 +00:00
|
|
|
pp_log(.app, .error, "\tUnable to reinstate connection on save profile \(profile.id): \(error)")
|
2024-11-10 16:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
await pendingTask?.value
|
|
|
|
pendingTask = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func waitForTasks() async throws -> Bool {
|
|
|
|
var didLaunch = false
|
|
|
|
|
|
|
|
// must launch once before anything else
|
|
|
|
if launchTask == nil {
|
|
|
|
launchTask = Task {
|
|
|
|
do {
|
|
|
|
try await onLaunch()
|
|
|
|
} catch {
|
|
|
|
launchTask = nil // redo launch
|
|
|
|
throw AppError.couldNotLaunch(reason: error)
|
|
|
|
}
|
2024-10-26 19:09:30 +00:00
|
|
|
}
|
2024-11-10 16:51:28 +00:00
|
|
|
didLaunch = true
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-11-10 16:51:28 +00:00
|
|
|
|
|
|
|
// will throw on .couldNotLaunch
|
|
|
|
// next wait will re-attempt launch (launchTask == nil)
|
|
|
|
try await launchTask?.value
|
|
|
|
|
|
|
|
// wait for pending task if any
|
|
|
|
await pendingTask?.value
|
|
|
|
pendingTask = nil
|
|
|
|
|
|
|
|
return didLaunch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension AppContext {
|
|
|
|
var isCloudKitEnabled: Bool {
|
|
|
|
#if os(tvOS)
|
|
|
|
true
|
|
|
|
#else
|
2024-12-02 09:40:25 +00:00
|
|
|
if AppCommandLine.contains(.uiTesting) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return FileManager.default.ubiquityIdentityToken != nil
|
2024-11-10 16:51:28 +00:00
|
|
|
#endif
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|