2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// AppContext.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 8/29/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 Combine
|
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
public final class AppContext: ObservableObject {
|
|
|
|
public let iapManager: IAPManager
|
|
|
|
|
|
|
|
public let profileManager: ProfileManager
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
public let tunnel: ExtendedTunnel
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
public let registry: Registry
|
|
|
|
|
2024-10-13 09:36:34 +00:00
|
|
|
public let providerManager: ProviderManager
|
2024-10-10 22:24:06 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable>
|
|
|
|
|
|
|
|
public init(
|
|
|
|
iapManager: IAPManager,
|
|
|
|
profileManager: ProfileManager,
|
2024-11-04 22:34:22 +00:00
|
|
|
tunnel: ExtendedTunnel,
|
2024-09-23 13:02:26 +00:00
|
|
|
registry: Registry,
|
2024-11-04 22:34:22 +00:00
|
|
|
providerManager: ProviderManager
|
2024-09-23 13:02:26 +00:00
|
|
|
) {
|
|
|
|
self.iapManager = iapManager
|
|
|
|
self.profileManager = profileManager
|
2024-11-04 22:34:22 +00:00
|
|
|
self.tunnel = tunnel
|
2024-09-30 12:56:20 +00:00
|
|
|
self.registry = registry
|
2024-10-13 09:36:34 +00:00
|
|
|
self.providerManager = providerManager
|
2024-09-30 12:56:20 +00:00
|
|
|
subscriptions = []
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-06 12:20:12 +00:00
|
|
|
observeObjects()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-11-05 09:41:02 +00:00
|
|
|
|
|
|
|
public func onApplicationActive() {
|
|
|
|
Task {
|
|
|
|
do {
|
2024-11-06 12:20:12 +00:00
|
|
|
pp_log(.app, .notice, "Application became active")
|
|
|
|
pp_log(.app, .notice, "Reload IAP receipt...")
|
|
|
|
await iapManager.reloadReceipt()
|
|
|
|
pp_log(.app, .notice, "Prepare tunnel and purge stale data...")
|
2024-11-05 09:41:02 +00:00
|
|
|
try await tunnel.prepare(purge: true)
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .fault, "Unable to prepare tunnel: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
// MARK: - Observation
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
private extension AppContext {
|
|
|
|
func observeObjects() {
|
2024-11-06 12:20:12 +00:00
|
|
|
profileManager.observeObjects()
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager
|
2024-09-30 12:56:20 +00:00
|
|
|
.didChange
|
|
|
|
.sink { [weak self] event in
|
|
|
|
switch event {
|
|
|
|
case .save(let profile):
|
|
|
|
self?.syncTunnelIfCurrentProfile(profile)
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
private extension AppContext {
|
|
|
|
func syncTunnelIfCurrentProfile(_ profile: Profile) {
|
|
|
|
guard profile.id == tunnel.currentProfile?.id else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Task {
|
2024-10-26 19:09:30 +00:00
|
|
|
guard [.active, .activating].contains(tunnel.status) else {
|
|
|
|
return
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
if profile.isInteractive {
|
|
|
|
try await tunnel.disconnect()
|
|
|
|
return
|
|
|
|
}
|
2024-10-26 19:09:30 +00:00
|
|
|
do {
|
2024-11-04 22:34:22 +00:00
|
|
|
try await tunnel.connect(with: profile)
|
2024-10-26 19:09:30 +00:00
|
|
|
} catch {
|
|
|
|
try await tunnel.disconnect()
|
|
|
|
}
|
2024-09-30 12:56:20 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|