2022-06-06 16:52:06 +00:00
|
|
|
//
|
|
|
|
// AppContext.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/17/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-06-06 16:52:06 +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
|
2022-06-06 16:52:06 +00:00
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@MainActor
|
2023-05-24 16:19:47 +00:00
|
|
|
final class AppContext {
|
2023-07-04 20:29:43 +00:00
|
|
|
private let coreContext: CoreContext
|
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
let upgradeManager: UpgradeManager
|
2023-09-09 18:29:04 +00:00
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
let productManager: ProductManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
let persistenceManager: PersistenceManager
|
|
|
|
|
2022-10-16 06:33:32 +00:00
|
|
|
private let reviewer: Reviewer
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
init(store: KeyValueStore) {
|
|
|
|
let logger = SwiftyBeaverLogger(
|
|
|
|
logFile: Constants.Log.App.url,
|
|
|
|
logLevel: Constants.Log.level,
|
|
|
|
logFormat: Constants.Log.App.format
|
|
|
|
)
|
|
|
|
Passepartout.shared.logger = logger
|
|
|
|
pp_log.info("Logging to: \(logger.logFile!)")
|
|
|
|
|
|
|
|
upgradeManager = UpgradeManager(
|
|
|
|
store: store,
|
|
|
|
strategy: DefaultUpgradeManagerStrategy()
|
|
|
|
)
|
|
|
|
upgradeManager.migrate(toVersion: Constants.Global.appVersionNumber)
|
2023-07-04 20:29:43 +00:00
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
productManager = ProductManager(
|
2023-12-20 19:43:39 +00:00
|
|
|
inApp: StoreKitInApp<LocalProduct>(),
|
|
|
|
receiptReader: StoreKitReceiptReader(),
|
2023-09-09 22:52:39 +00:00
|
|
|
overriddenAppType: Constants.InApp.overriddenAppType,
|
2022-06-06 16:52:06 +00:00
|
|
|
buildProducts: Constants.InApp.buildProducts
|
|
|
|
)
|
2022-10-13 06:53:50 +00:00
|
|
|
|
2023-09-10 09:20:56 +00:00
|
|
|
persistenceManager = PersistenceManager(
|
|
|
|
store: store,
|
|
|
|
ckContainerId: Constants.CloudKit.containerId,
|
2023-12-16 19:58:54 +00:00
|
|
|
ckSharedContainerId: Constants.CloudKit.sharedContainerId,
|
2023-09-10 09:20:56 +00:00
|
|
|
ckCoreDataZone: Constants.CloudKit.coreDataZone
|
|
|
|
)
|
2023-09-10 08:34:42 +00:00
|
|
|
|
2022-10-16 06:33:32 +00:00
|
|
|
reviewer = Reviewer()
|
|
|
|
reviewer.eventCountBeforeRating = Constants.Rating.eventCount
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-09-10 08:34:42 +00:00
|
|
|
coreContext = CoreContext(persistenceManager: persistenceManager)
|
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
// post
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-04 20:29:43 +00:00
|
|
|
configureObjects()
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerManager: ProviderManager {
|
|
|
|
coreContext.providerManager
|
|
|
|
}
|
|
|
|
|
|
|
|
var profileManager: ProfileManager {
|
|
|
|
coreContext.profileManager
|
|
|
|
}
|
|
|
|
|
|
|
|
var vpnManager: VPNManager {
|
|
|
|
coreContext.vpnManager
|
2022-06-06 16:52:06 +00:00
|
|
|
}
|
2023-07-02 10:51:50 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-02 10:51:50 +00:00
|
|
|
private extension AppContext {
|
2023-07-04 20:29:43 +00:00
|
|
|
func configureObjects() {
|
2023-12-16 19:58:54 +00:00
|
|
|
coreContext.profileManager.willSaveSharedProfile = { [unowned self] in
|
|
|
|
willSaveSharedProfile(withNewProfile: $0, existingProfile: $1)
|
|
|
|
}
|
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
coreContext.vpnManager.isOnDemandRulesSupported = {
|
|
|
|
self.isEligibleForOnDemandRules()
|
|
|
|
}
|
2023-12-23 06:59:23 +00:00
|
|
|
coreContext.vpnManager.isNetworkSettingsSupported = {
|
|
|
|
self.isEligibleForNetworkSettings()
|
|
|
|
}
|
2022-10-13 06:53:50 +00:00
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
coreContext.vpnManager.userData = {
|
|
|
|
if let expirationDate = $0.connectionExpirationDate {
|
|
|
|
return [Constants.Tunnel.expirationTimeIntervalKey: expirationDate.timeIntervalSinceReferenceDate]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
coreContext.vpnManager.currentState.$vpnStatus
|
|
|
|
.removeDuplicates()
|
2022-10-13 06:53:50 +00:00
|
|
|
.receive(on: DispatchQueue.main)
|
2023-09-08 14:20:01 +00:00
|
|
|
.sink { [weak self] in
|
2022-06-06 16:52:06 +00:00
|
|
|
if $0 == .connected {
|
|
|
|
pp_log.info("VPN successful connection, report to Reviewer")
|
2023-09-08 14:20:01 +00:00
|
|
|
self?.reviewer.reportEvent()
|
2022-06-06 16:52:06 +00:00
|
|
|
}
|
2022-11-06 17:51:13 +00:00
|
|
|
}.store(in: &cancellables)
|
|
|
|
|
|
|
|
productManager.didRefundProducts
|
|
|
|
.receive(on: DispatchQueue.main)
|
2023-09-08 14:20:01 +00:00
|
|
|
.sink { [weak self] in
|
2022-11-06 17:51:13 +00:00
|
|
|
Task {
|
|
|
|
pp_log.info("Refunds detected, uninstalling VPN profile")
|
2023-09-08 14:20:01 +00:00
|
|
|
await self?.coreContext.vpnManager.uninstall()
|
2022-11-06 17:51:13 +00:00
|
|
|
}
|
|
|
|
}.store(in: &cancellables)
|
2022-06-06 16:52:06 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-06 16:52:06 +00:00
|
|
|
// eligibility: ignore network settings if ineligible
|
2023-07-02 10:51:50 +00:00
|
|
|
func isEligibleForNetworkSettings() -> Bool {
|
2022-06-06 16:52:06 +00:00
|
|
|
guard productManager.isEligible(forFeature: .networkSettings) else {
|
|
|
|
pp_log.warning("Ignore network settings, not eligible")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-10-10 21:00:19 +00:00
|
|
|
// eligibility: reset on demand rules if no trusted networks
|
2023-07-02 10:51:50 +00:00
|
|
|
func isEligibleForOnDemandRules() -> Bool {
|
2022-06-06 16:52:06 +00:00
|
|
|
guard productManager.isEligible(forFeature: .trustedNetworks) else {
|
2023-10-10 21:00:19 +00:00
|
|
|
pp_log.warning("Ignore on demand rules, not eligible for trusted networks")
|
2022-06-06 16:52:06 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
|
|
|
|
// eligibility: expire restricted TV profiles after N minutes
|
|
|
|
func willSaveSharedProfile(withNewProfile newProfile: Profile, existingProfile: Profile?) -> Profile {
|
|
|
|
if let existingProfile {
|
|
|
|
assert(newProfile.id == existingProfile.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard productManager.isEligible(forFeature: .appleTV) else {
|
|
|
|
var restricted = newProfile
|
|
|
|
let remainingMinutes: Int
|
|
|
|
let expirationDate: Date
|
|
|
|
|
|
|
|
// retain current expiration period if any
|
|
|
|
if let currentExpirationDate = existingProfile?.connectionExpirationDate {
|
|
|
|
remainingMinutes = Int(currentExpirationDate.timeIntervalSinceNow / 60.0)
|
|
|
|
expirationDate = currentExpirationDate
|
|
|
|
}
|
|
|
|
// otherwise, expire in N minutes from now
|
|
|
|
else {
|
|
|
|
remainingMinutes = Constants.InApp.tvLimitedMinutes
|
|
|
|
expirationDate = Date()
|
|
|
|
.addingTimeInterval(TimeInterval(remainingMinutes) * 60.0)
|
|
|
|
|
|
|
|
restricted.connectionExpirationDate = expirationDate
|
|
|
|
}
|
|
|
|
|
|
|
|
if remainingMinutes > 0 {
|
|
|
|
pp_log.warning("\(newProfile.logDescription): TV connection expires in \(remainingMinutes) minutes (at \(expirationDate))")
|
|
|
|
} else {
|
|
|
|
pp_log.warning("\(newProfile.logDescription): TV connection expired at \(expirationDate)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return restricted
|
|
|
|
}
|
|
|
|
|
|
|
|
return newProfile
|
|
|
|
}
|
2022-06-06 16:52:06 +00:00
|
|
|
}
|