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
|
|
|
|
|
|
|
|
// shared registry and environment are picked from Shared.swift
|
|
|
|
|
|
|
|
extension AppContext {
|
|
|
|
static let shared: AppContext = {
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let profileManager: ProfileManager = {
|
|
|
|
return ProfileManager(
|
|
|
|
repository: Configuration.ProfileManager.mainProfileRepository,
|
|
|
|
backupRepository: Configuration.ProfileManager.backupProfileRepository,
|
|
|
|
remoteRepositoryBlock: remoteRepositoryBlock,
|
|
|
|
mirrorsRemoteRepository: Configuration.ProfileManager.mirrorsRemoteRepository,
|
|
|
|
processor: IAPManager.sharedProcessor
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// MARK: ExtendedTunnel
|
|
|
|
|
|
|
|
let tunnel = ExtendedTunnel(
|
|
|
|
tunnel: Tunnel(strategy: Configuration.ExtendedTunnel.strategy),
|
|
|
|
environment: .shared,
|
|
|
|
processor: IAPManager.sharedProcessor,
|
|
|
|
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,
|
|
|
|
profilesContainerName: Constants.shared.containers.legacyV2,
|
|
|
|
cloudKitIdentifier: BundleConfiguration.mainString(for: .legacyV2CloudKitId)
|
|
|
|
)
|
2024-11-16 20:16:25 +00:00
|
|
|
let migrationSimulation: MigrationManager.Simulation?
|
|
|
|
if Configuration.Environment.isFakeMigration {
|
|
|
|
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-14 18:12:51 +00:00
|
|
|
iapManager: .sharedForApp,
|
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
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Configuration
|
|
|
|
|
|
|
|
private extension Configuration {
|
|
|
|
enum ExtendedTunnel {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Simulator
|
|
|
|
|
|
|
|
#if targetEnvironment(simulator)
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private extension Configuration.ProfileManager {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Configuration.ExtendedTunnel {
|
|
|
|
static var strategy: TunnelObservableStrategy {
|
|
|
|
FakeTunnelStrategy(environment: .shared, dataCountInterval: 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// MARK: Device
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private extension Configuration.ProfileManager {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private extension Configuration.ExtendedTunnel {
|
|
|
|
static var strategy: TunnelObservableStrategy {
|
|
|
|
Configuration.ProfileManager.neStrategy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MARK: Common
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private extension Configuration.ProfileManager {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|