2024-10-10 14:03:02 +00:00
|
|
|
//
|
|
|
|
// NEProfileRepository.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/10/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
|
2024-10-10 14:20:36 +00:00
|
|
|
import CommonLibrary
|
2024-10-10 14:03:02 +00:00
|
|
|
import Foundation
|
2024-10-19 16:22:27 +00:00
|
|
|
import NetworkExtension
|
2024-10-10 14:03:02 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
|
|
|
|
public final class NEProfileRepository: ProfileRepository {
|
|
|
|
private let repository: NETunnelManagerRepository
|
|
|
|
|
2024-10-10 22:31:32 +00:00
|
|
|
private let title: (Profile) -> String
|
|
|
|
|
2024-10-10 14:03:02 +00:00
|
|
|
private let profilesSubject: CurrentValueSubject<[Profile], Never>
|
|
|
|
|
2024-10-19 16:22:27 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable>
|
2024-10-10 14:03:02 +00:00
|
|
|
|
2024-10-10 22:31:32 +00:00
|
|
|
public init(repository: NETunnelManagerRepository, title: @escaping (Profile) -> String) {
|
2024-10-10 14:03:02 +00:00
|
|
|
self.repository = repository
|
2024-10-10 22:31:32 +00:00
|
|
|
self.title = title
|
2024-10-10 14:03:02 +00:00
|
|
|
profilesSubject = CurrentValueSubject([])
|
2024-10-19 16:22:27 +00:00
|
|
|
subscriptions = []
|
2024-10-10 14:03:02 +00:00
|
|
|
|
2024-10-19 16:22:27 +00:00
|
|
|
repository
|
2024-10-10 14:03:02 +00:00
|
|
|
.managersPublisher
|
2024-10-19 16:22:27 +00:00
|
|
|
.first()
|
|
|
|
.sink { [weak self] in
|
|
|
|
self?.onLoadedManagers($0)
|
2024-10-10 14:03:02 +00:00
|
|
|
}
|
2024-10-19 16:22:27 +00:00
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
repository
|
|
|
|
.managersPublisher
|
|
|
|
.dropFirst()
|
|
|
|
.sink { [weak self] in
|
|
|
|
self?.onUpdatedManagers($0)
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
2024-10-10 14:03:02 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:20:36 +00:00
|
|
|
public var profilesPublisher: AnyPublisher<[Profile], Never> {
|
|
|
|
profilesSubject.eraseToAnyPublisher()
|
2024-10-10 14:03:02 +00:00
|
|
|
}
|
|
|
|
|
2024-10-11 15:48:37 +00:00
|
|
|
// unused in app, rely on Tunnel.prepare()
|
|
|
|
public func loadProfiles(purge: Bool) async throws {
|
|
|
|
try await repository.load(purge: purge)
|
|
|
|
}
|
|
|
|
|
2024-10-10 14:20:36 +00:00
|
|
|
public func saveProfile(_ profile: Profile) async throws {
|
2024-10-10 22:31:32 +00:00
|
|
|
try await repository.save(profile, connect: false, title: title)
|
2024-10-19 16:22:27 +00:00
|
|
|
if let index = profilesSubject.value.firstIndex(where: { $0.id == profile.id }) {
|
|
|
|
profilesSubject.value[index] = profile
|
|
|
|
} else {
|
|
|
|
profilesSubject.value.append(profile)
|
|
|
|
}
|
2024-10-10 14:03:02 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:20:36 +00:00
|
|
|
public func removeProfiles(withIds profileIds: [Profile.ID]) async throws {
|
2024-10-19 16:22:27 +00:00
|
|
|
var removedIds: Set<Profile.ID> = []
|
|
|
|
defer {
|
|
|
|
profilesSubject.value.removeAll {
|
|
|
|
removedIds.contains($0.id)
|
|
|
|
}
|
|
|
|
}
|
2024-10-10 14:20:36 +00:00
|
|
|
for id in profileIds {
|
|
|
|
try await repository.remove(profileId: id)
|
2024-10-19 16:22:27 +00:00
|
|
|
removedIds.insert(id)
|
2024-10-10 14:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-19 16:22:27 +00:00
|
|
|
|
|
|
|
private extension NEProfileRepository {
|
|
|
|
func onLoadedManagers(_ managers: [Profile.ID: NETunnelProviderManager]) {
|
|
|
|
let profiles = managers.values.compactMap {
|
|
|
|
do {
|
|
|
|
return try repository.profile(from: $0)
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to decode profile from NE manager '\($0.localizedDescription ?? "")': \(error)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
profilesSubject.send(profiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
func onUpdatedManagers(_ managers: [Profile.ID: NETunnelProviderManager]) {
|
|
|
|
let profiles = profilesSubject
|
|
|
|
.value
|
|
|
|
.filter {
|
|
|
|
managers.keys.contains($0.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
let removedProfilesDesc = profilesSubject
|
|
|
|
.value
|
|
|
|
.filter {
|
|
|
|
!managers.keys.contains($0.id)
|
|
|
|
}
|
|
|
|
.map {
|
|
|
|
"\($0.name)(\($0.id)"
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_log(.app, .info, "Sync profiles removed externally: \(removedProfilesDesc)")
|
|
|
|
|
|
|
|
profilesSubject.send(profiles)
|
|
|
|
}
|
|
|
|
}
|