2024-11-09 14:20:59 +00:00
|
|
|
//
|
|
|
|
// AppContext+Shared.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/24/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 AppData
|
|
|
|
import AppDataProfiles
|
|
|
|
import AppDataProviders
|
|
|
|
import CommonLibrary
|
|
|
|
import CommonUtils
|
|
|
|
import Foundation
|
2024-11-14 10:02:26 +00:00
|
|
|
import LegacyV2
|
2024-11-09 14:20:59 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import UILibrary
|
2024-11-28 00:30:26 +00:00
|
|
|
import UITesting
|
2024-11-09 14:20:59 +00:00
|
|
|
|
|
|
|
// shared registry and environment are picked from Shared.swift
|
|
|
|
|
|
|
|
extension AppContext {
|
|
|
|
static let shared: AppContext = {
|
2024-11-28 16:31:17 +00:00
|
|
|
let iapManager: IAPManager = .sharedForApp
|
|
|
|
let processor = InAppProcessor.shared(iapManager) {
|
|
|
|
$0.localizedPreview
|
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
|
|
|
|
// MARK: ProfileManager
|
|
|
|
|
|
|
|
let remoteRepositoryBlock: (Bool) -> ProfileRepository = {
|
|
|
|
let remoteStore = CoreDataPersistentStore(
|
|
|
|
logger: .default,
|
|
|
|
containerName: Constants.shared.containers.remote,
|
|
|
|
model: AppData.cdProfilesModel,
|
|
|
|
cloudKitIdentifier: $0 ? BundleConfiguration.mainString(for: .cloudKitId) : nil,
|
|
|
|
author: nil
|
|
|
|
)
|
|
|
|
return AppData.cdProfileRepositoryV3(
|
|
|
|
registry: .shared,
|
|
|
|
coder: CodableProfileCoder(),
|
|
|
|
context: remoteStore.context,
|
|
|
|
observingResults: true
|
|
|
|
) { error in
|
|
|
|
pp_log(.app, .error, "Unable to decode remote result: \(error)")
|
|
|
|
return .ignore
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 09:40:25 +00:00
|
|
|
let profileManager = ProfileManager(
|
|
|
|
repository: Dependencies.ProfileManager.mainProfileRepository,
|
|
|
|
backupRepository: Dependencies.ProfileManager.backupProfileRepository,
|
|
|
|
remoteRepositoryBlock: remoteRepositoryBlock,
|
|
|
|
mirrorsRemoteRepository: Dependencies.ProfileManager.mirrorsRemoteRepository,
|
|
|
|
processor: processor
|
|
|
|
)
|
2024-11-09 14:20:59 +00:00
|
|
|
|
|
|
|
// MARK: ExtendedTunnel
|
|
|
|
|
|
|
|
let tunnel = ExtendedTunnel(
|
2024-12-02 09:40:25 +00:00
|
|
|
tunnel: Tunnel(strategy: Dependencies.ExtendedTunnel.strategy),
|
2024-11-09 14:20:59 +00:00
|
|
|
environment: .shared,
|
2024-11-28 16:31:17 +00:00
|
|
|
processor: processor,
|
2024-11-09 14:20:59 +00:00
|
|
|
interval: Constants.shared.tunnel.refreshInterval
|
|
|
|
)
|
|
|
|
|
|
|
|
// MARK: ProviderManager
|
|
|
|
|
|
|
|
let providerManager: ProviderManager = {
|
|
|
|
let store = CoreDataPersistentStore(
|
|
|
|
logger: .default,
|
|
|
|
containerName: Constants.shared.containers.providers,
|
|
|
|
model: AppData.cdProvidersModel,
|
|
|
|
cloudKitIdentifier: nil,
|
|
|
|
author: nil
|
|
|
|
)
|
2024-11-17 10:34:43 +00:00
|
|
|
let repository = AppData.cdProviderRepositoryV3(context: store.backgroundContext)
|
2024-11-09 14:20:59 +00:00
|
|
|
return ProviderManager(repository: repository)
|
|
|
|
}()
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
// MARK: MigrationManager
|
|
|
|
|
|
|
|
let profileStrategy = ProfileV2MigrationStrategy(
|
|
|
|
coreDataLogger: .default,
|
2024-11-23 12:33:02 +00:00
|
|
|
profilesContainer: .init(
|
|
|
|
Constants.shared.containers.legacyV2,
|
|
|
|
BundleConfiguration.mainString(for: .legacyV2CloudKitId)
|
|
|
|
),
|
|
|
|
tvProfilesContainer: .init(
|
|
|
|
Constants.shared.containers.legacyV2TV,
|
|
|
|
BundleConfiguration.mainString(for: .legacyV2TVCloudKitId)
|
|
|
|
)
|
2024-11-14 10:02:26 +00:00
|
|
|
)
|
2024-11-16 20:16:25 +00:00
|
|
|
let migrationSimulation: MigrationManager.Simulation?
|
2024-11-28 00:30:26 +00:00
|
|
|
if AppCommandLine.contains(.fakeMigration) {
|
2024-11-16 20:16:25 +00:00
|
|
|
migrationSimulation = MigrationManager.Simulation(
|
|
|
|
fakeProfiles: true,
|
|
|
|
maxMigrationTime: 3.0,
|
|
|
|
randomFailures: true
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
migrationSimulation = nil
|
|
|
|
}
|
|
|
|
let migrationManager = MigrationManager(profileStrategy: profileStrategy, simulation: migrationSimulation)
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
return AppContext(
|
2024-11-28 16:31:17 +00:00
|
|
|
iapManager: iapManager,
|
2024-11-14 10:02:26 +00:00
|
|
|
migrationManager: migrationManager,
|
2024-11-09 14:20:59 +00:00
|
|
|
profileManager: profileManager,
|
2024-11-14 10:02:26 +00:00
|
|
|
providerManager: providerManager,
|
|
|
|
registry: .shared,
|
2024-11-17 10:34:43 +00:00
|
|
|
tunnel: tunnel,
|
|
|
|
tunnelReceiptURL: BundleConfiguration.urlForBetaReceipt
|
2024-11-09 14:20:59 +00:00
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-12-02 09:40:25 +00:00
|
|
|
// MARK: - Dependencies
|
2024-11-09 14:20:59 +00:00
|
|
|
|
|
|
|
// MARK: Simulator
|
|
|
|
|
|
|
|
#if targetEnvironment(simulator)
|
|
|
|
|
2024-12-02 09:40:25 +00:00
|
|
|
private extension Dependencies.ExtendedTunnel {
|
|
|
|
static var strategy: TunnelObservableStrategy {
|
|
|
|
FakeTunnelStrategy(environment: .shared, dataCountInterval: 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 14:20:59 +00:00
|
|
|
@MainActor
|
2024-12-02 09:40:25 +00:00
|
|
|
private extension Dependencies.ProfileManager {
|
2024-11-09 14:20:59 +00:00
|
|
|
static var mainProfileRepository: ProfileRepository {
|
2024-11-17 14:28:31 +00:00
|
|
|
coreDataProfileRepository(observingResults: true)
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static var backupProfileRepository: ProfileRepository? {
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// MARK: Device
|
|
|
|
|
|
|
|
@MainActor
|
2024-12-02 09:40:25 +00:00
|
|
|
private extension Dependencies.ExtendedTunnel {
|
|
|
|
static var strategy: TunnelObservableStrategy {
|
|
|
|
Dependencies.ProfileManager.neStrategy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private extension Dependencies.ProfileManager {
|
2024-11-09 14:20:59 +00:00
|
|
|
static var mainProfileRepository: ProfileRepository {
|
|
|
|
neProfileRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
static var backupProfileRepository: ProfileRepository? {
|
2024-11-17 14:28:31 +00:00
|
|
|
coreDataProfileRepository(observingResults: false)
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MARK: Common
|
|
|
|
|
|
|
|
@MainActor
|
2024-12-02 09:40:25 +00:00
|
|
|
private extension Dependencies.ProfileManager {
|
2024-11-09 14:20:59 +00:00
|
|
|
static let neProfileRepository: ProfileRepository = {
|
|
|
|
NEProfileRepository(repository: neStrategy) {
|
|
|
|
sharedTitle($0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
static let neStrategy: NETunnelStrategy = {
|
|
|
|
NETunnelStrategy(
|
|
|
|
bundleIdentifier: BundleConfiguration.mainString(for: .tunnelId),
|
|
|
|
coder: Registry.sharedProtocolCoder,
|
|
|
|
environment: .shared
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
2024-11-17 14:28:31 +00:00
|
|
|
static func coreDataProfileRepository(observingResults: Bool) -> ProfileRepository {
|
2024-11-09 14:20:59 +00:00
|
|
|
let store = CoreDataPersistentStore(
|
|
|
|
logger: .default,
|
|
|
|
containerName: Constants.shared.containers.local,
|
|
|
|
model: AppData.cdProfilesModel,
|
|
|
|
cloudKitIdentifier: nil,
|
|
|
|
author: nil
|
|
|
|
)
|
|
|
|
return AppData.cdProfileRepositoryV3(
|
|
|
|
registry: .shared,
|
|
|
|
coder: CodableProfileCoder(),
|
|
|
|
context: store.context,
|
2024-11-17 14:28:31 +00:00
|
|
|
observingResults: observingResults
|
2024-11-09 14:20:59 +00:00
|
|
|
) { error in
|
|
|
|
pp_log(.app, .error, "Unable to decode local result: \(error)")
|
|
|
|
return .ignore
|
|
|
|
}
|
2024-11-17 14:28:31 +00:00
|
|
|
}
|
2024-11-09 14:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Logging
|
|
|
|
|
|
|
|
private extension CoreDataPersistentStoreLogger where Self == DefaultCoreDataPersistentStoreLogger {
|
|
|
|
static var `default`: CoreDataPersistentStoreLogger {
|
|
|
|
DefaultCoreDataPersistentStoreLogger()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct DefaultCoreDataPersistentStoreLogger: CoreDataPersistentStoreLogger {
|
|
|
|
func debug(_ msg: String) {
|
|
|
|
pp_log(.app, .info, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func warning(_ msg: String) {
|
|
|
|
pp_log(.app, .error, msg)
|
|
|
|
}
|
|
|
|
}
|