2022-04-12 13:09:14 +00:00
|
|
|
//
|
2022-06-06 16:52:06 +00:00
|
|
|
// CoreContext.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
2022-06-06 16:52:06 +00:00
|
|
|
// Created by Davide De Rosa on 6/4/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// 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
|
2023-04-04 07:50:45 +00:00
|
|
|
import Foundation
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-05-24 16:19:47 +00:00
|
|
|
import TunnelKitCore
|
|
|
|
import TunnelKitManager
|
2022-06-23 21:31:01 +00:00
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@MainActor
|
2023-05-24 16:19:47 +00:00
|
|
|
final class CoreContext {
|
2023-09-09 18:29:04 +00:00
|
|
|
let store: KeyValueStore
|
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let providerManager: ProviderManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let profileManager: ProfileManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let vpnManager: VPNManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
init(persistenceManager: PersistenceManager) {
|
|
|
|
store = persistenceManager.store
|
2023-09-09 18:29:04 +00:00
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-09-10 09:20:56 +00:00
|
|
|
let vpnPersistence = persistenceManager.loadVPNPersistence(
|
2023-09-10 08:34:42 +00:00
|
|
|
withName: Constants.Persistence.profilesContainerName
|
2022-05-05 07:18:53 +00:00
|
|
|
)
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
|
|
|
let sharedVPNPersistence = persistenceManager.loadSharedVPNPersistence(
|
|
|
|
withName: Constants.Persistence.sharedProfilesContainerName
|
|
|
|
)
|
2023-09-10 09:20:56 +00:00
|
|
|
let providersPersistence = persistenceManager.loadProvidersPersistence(
|
2022-05-05 07:18:53 +00:00
|
|
|
withName: Constants.Persistence.providersContainerName
|
|
|
|
)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
let remoteProvidersStrategy = APIRemoteProvidersStrategy(
|
2022-04-12 13:09:14 +00:00
|
|
|
appBuild: Constants.Global.appBuildNumber,
|
2023-05-24 16:19:47 +00:00
|
|
|
bundleServices: APIWebServices.bundledServices(
|
2022-04-12 13:09:14 +00:00
|
|
|
withVersion: Constants.Services.version
|
|
|
|
),
|
2023-05-24 16:19:47 +00:00
|
|
|
remoteServices: APIWebServices(
|
2022-04-12 13:09:14 +00:00
|
|
|
Constants.Services.version,
|
|
|
|
Constants.Repos.api,
|
|
|
|
timeout: Constants.Services.connectivityTimeout
|
|
|
|
),
|
2023-05-27 10:32:53 +00:00
|
|
|
webServicesRepository: providersPersistence.webServicesRepository()
|
2023-05-24 16:19:47 +00:00
|
|
|
)
|
|
|
|
providerManager = ProviderManager(
|
2023-05-27 10:32:53 +00:00
|
|
|
localProvidersRepository: providersPersistence.localProvidersRepository(),
|
2023-05-24 16:19:47 +00:00
|
|
|
remoteProvidersStrategy: remoteProvidersStrategy
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
let tvProfileRepository = sharedVPNPersistence.profileRepository()
|
|
|
|
#if !os(tvOS)
|
|
|
|
let profileRepository = vpnPersistence.profileRepository()
|
|
|
|
let sharedProfileRepository = tvProfileRepository
|
|
|
|
#else
|
|
|
|
let profileRepository = tvProfileRepository
|
|
|
|
let sharedProfileRepository: ProfileRepository? = nil
|
|
|
|
#endif
|
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
profileManager = ProfileManager(
|
2022-06-15 18:53:37 +00:00
|
|
|
store: store,
|
2022-04-12 13:09:14 +00:00
|
|
|
providerManager: providerManager,
|
2023-12-16 19:58:54 +00:00
|
|
|
profileRepository: profileRepository,
|
|
|
|
sharedProfileRepository: sharedProfileRepository,
|
2023-05-24 16:19:47 +00:00
|
|
|
keychain: KeychainSecretRepository(appGroup: Constants.App.appGroupId),
|
|
|
|
keychainEntry: Unlocalized.Keychain.passwordEntry,
|
|
|
|
keychainLabel: Unlocalized.Keychain.passwordLabel
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
#if targetEnvironment(simulator)
|
2022-10-11 16:16:11 +00:00
|
|
|
let vpn = MockVPN()
|
2022-04-12 13:09:14 +00:00
|
|
|
#else
|
2022-10-11 16:16:11 +00:00
|
|
|
let vpn = NetworkExtensionVPN()
|
|
|
|
#endif
|
2023-05-24 16:19:47 +00:00
|
|
|
let vpnManagerStrategy = TunnelKitVPNManagerStrategy(
|
2022-06-01 19:12:42 +00:00
|
|
|
appGroup: Constants.App.appGroupId,
|
2022-10-11 16:16:11 +00:00
|
|
|
tunnelBundleIdentifier: Constants.App.tunnelBundleId,
|
|
|
|
vpn: vpn
|
2022-06-01 19:12:42 +00:00
|
|
|
)
|
2022-08-28 07:19:15 +00:00
|
|
|
vpnManager = VPNManager(
|
2022-06-15 18:53:37 +00:00
|
|
|
store: store,
|
2022-04-12 13:09:14 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
providerManager: providerManager,
|
2023-05-24 16:19:47 +00:00
|
|
|
strategy: vpnManagerStrategy
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// post
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
configureObjects(persistenceManager: persistenceManager)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-04 20:29:43 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-04 20:29:43 +00:00
|
|
|
private extension CoreContext {
|
2023-09-10 08:34:42 +00:00
|
|
|
func configureObjects(persistenceManager: PersistenceManager) {
|
2022-04-12 13:09:14 +00:00
|
|
|
providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
|
2022-10-16 06:33:32 +00:00
|
|
|
vpnManager.tunnelLogPath = Constants.Log.Tunnel.path
|
|
|
|
vpnManager.tunnelLogFormat = Constants.Log.Tunnel.format
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-04 08:17:54 +00:00
|
|
|
profileManager.observeUpdates()
|
|
|
|
vpnManager.observeUpdates()
|
2022-11-10 06:42:08 +00:00
|
|
|
|
|
|
|
CoreConfiguration.masksPrivateData = vpnManager.masksPrivateData
|
2023-09-10 08:34:42 +00:00
|
|
|
vpnManager.didUpdatePreferences
|
|
|
|
.sink {
|
|
|
|
CoreConfiguration.masksPrivateData = $0.masksPrivateData
|
|
|
|
}.store(in: &cancellables)
|
|
|
|
|
|
|
|
persistenceManager.didChangePersistence
|
|
|
|
.sink { [weak self] in
|
2023-09-10 09:20:56 +00:00
|
|
|
self?.reloadPersistenceObjects(persistenceManager: persistenceManager)
|
2023-09-10 08:34:42 +00:00
|
|
|
}.store(in: &cancellables)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-09-09 18:29:04 +00:00
|
|
|
|
2023-09-10 09:20:56 +00:00
|
|
|
func reloadPersistenceObjects(persistenceManager: PersistenceManager) {
|
|
|
|
let vpnPersistence = persistenceManager.loadVPNPersistence(
|
2023-09-10 08:34:42 +00:00
|
|
|
withName: Constants.Persistence.profilesContainerName
|
2023-09-09 18:29:04 +00:00
|
|
|
)
|
|
|
|
profileManager.swapProfileRepository(vpnPersistence.profileRepository())
|
|
|
|
}
|
|
|
|
}
|