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.
|
2022-04-12 13:09:14 +00:00
|
|
|
// Copyright (c) 2022 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 Foundation
|
|
|
|
import Combine
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@MainActor
|
2022-06-06 16:52:06 +00:00
|
|
|
class CoreContext {
|
|
|
|
let store: KeyValueStore
|
2022-06-15 18:53:37 +00:00
|
|
|
|
2022-05-05 07:18:53 +00:00
|
|
|
private let profilesPersistence: Persistence
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-05 07:18:53 +00:00
|
|
|
private let providersPersistence: Persistence
|
|
|
|
|
|
|
|
var urlsForProfiles: [URL]? {
|
|
|
|
profilesPersistence.containerURLs
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlsForProviders: [URL]? {
|
|
|
|
providersPersistence.containerURLs
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
let upgradeManager: UpgradeManager
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let providerManager: ProviderManager
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let profileManager: ProfileManager
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
let vpnManager: VPNManager
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
init(store: KeyValueStore) {
|
|
|
|
self.store = store
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
let persistenceManager = PersistenceManager(store: store)
|
2022-05-05 07:18:53 +00:00
|
|
|
profilesPersistence = persistenceManager.profilesPersistence(
|
|
|
|
withName: Constants.Persistence.profilesContainerName
|
|
|
|
)
|
|
|
|
providersPersistence = persistenceManager.providersPersistence(
|
|
|
|
withName: Constants.Persistence.providersContainerName
|
|
|
|
)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
upgradeManager = UpgradeManager(store: store)
|
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
providerManager = ProviderManager(
|
2022-04-12 13:09:14 +00:00
|
|
|
appBuild: Constants.Global.appBuildNumber,
|
|
|
|
bundleServices: DefaultWebServices.bundledServices(
|
|
|
|
withVersion: Constants.Services.version
|
|
|
|
),
|
|
|
|
webServices: DefaultWebServices(
|
|
|
|
Constants.Services.version,
|
|
|
|
Constants.Repos.api,
|
|
|
|
timeout: Constants.Services.connectivityTimeout
|
|
|
|
),
|
2022-05-05 07:18:53 +00:00
|
|
|
persistence: providersPersistence
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
appGroup: Constants.App.appGroupId,
|
|
|
|
keychainLabel: Unlocalized.Keychain.passwordLabel,
|
2022-06-23 21:31:01 +00:00
|
|
|
strategy: CoreDataProfileManagerStrategy(
|
2022-05-05 07:18:53 +00:00
|
|
|
persistence: profilesPersistence
|
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
|
2022-06-23 21:31:01 +00:00
|
|
|
let strategy = 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-23 21:31:01 +00:00
|
|
|
appGroup: Constants.App.appGroupId,
|
2022-06-15 18:53:37 +00:00
|
|
|
store: store,
|
2022-04-12 13:09:14 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
providerManager: providerManager,
|
2022-06-01 19:12:42 +00:00
|
|
|
strategy: strategy
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
|
|
|
configureObjects()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func configureObjects() {
|
|
|
|
providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
|
2022-06-17 06:27:56 +00:00
|
|
|
vpnManager.tunnelLogPath = Constants.Log.tunnelLogPath
|
2022-06-15 18:53:37 +00:00
|
|
|
vpnManager.tunnelLogFormat = Constants.Log.tunnelLogFormat
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-04 08:17:54 +00:00
|
|
|
profileManager.observeUpdates()
|
|
|
|
vpnManager.observeUpdates()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|