2022-06-25 20:11:45 +00:00
|
|
|
//
|
|
|
|
// DefaultLightProviderManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 7/7/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-06-25 20:11:45 +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-25 20:11:45 +00:00
|
|
|
import PassepartoutLibrary
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProviderCategory: LightProviderCategory {
|
2022-06-25 20:11:45 +00:00
|
|
|
let name: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
var locations: [LightProviderLocation]
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
init(_ category: ProviderCategory) {
|
|
|
|
name = category.name
|
|
|
|
locations = category.locations
|
|
|
|
.sorted()
|
|
|
|
.map(DefaultLightProviderLocation.init)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProviderLocation: LightProviderLocation {
|
2022-06-25 20:11:45 +00:00
|
|
|
let description: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let id: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let countryCode: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let servers: [LightProviderServer]
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
init(_ location: ProviderLocation) {
|
2023-07-05 15:18:33 +00:00
|
|
|
description = location.localizedDescription(style: .country)
|
2022-06-25 20:11:45 +00:00
|
|
|
id = location.id
|
|
|
|
countryCode = location.countryCode
|
|
|
|
servers = location.servers?
|
|
|
|
.sorted()
|
|
|
|
.map(DefaultLightProviderServer.init) ?? []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProviderServer: LightProviderServer {
|
2022-06-25 20:11:45 +00:00
|
|
|
let description: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-09-04 07:26:03 +00:00
|
|
|
let longDescription: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let categoryName: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let locationId: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
let serverId: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
init(_ server: ProviderServer) {
|
2023-07-05 15:18:33 +00:00
|
|
|
description = server.localizedDescription(style: .shortWithDefault)
|
|
|
|
longDescription = server.localizedDescription(style: .longWithCategory(withCategory: false))
|
2022-06-25 20:11:45 +00:00
|
|
|
categoryName = server.categoryName
|
|
|
|
locationId = server.locationId
|
|
|
|
serverId = server.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
final class DefaultLightProviderManager: LightProviderManager {
|
2022-08-28 07:19:15 +00:00
|
|
|
private let providerManager = ProviderManager.shared
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable> = []
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
weak var delegate: LightProviderManagerDelegate?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
init() {
|
|
|
|
providerManager.didUpdateProviders
|
|
|
|
.receive(on: DispatchQueue.main)
|
2023-09-08 14:20:01 +00:00
|
|
|
.sink { [weak self] in
|
|
|
|
self?.delegate?.didUpdateProviders()
|
2022-06-25 20:11:45 +00:00
|
|
|
}.store(in: &subscriptions)
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-06-25 20:11:45 +00:00
|
|
|
func categories(_ name: String, vpnProtocol: String) -> [LightProviderCategory] {
|
|
|
|
guard let vpnProtocolType = VPNProtocolType(rawValue: vpnProtocol) else {
|
|
|
|
fatalError("Unrecognized VPN protocol: \(vpnProtocol)")
|
|
|
|
}
|
|
|
|
return providerManager.categories(name, vpnProtocol: vpnProtocolType)
|
2022-08-13 14:44:08 +00:00
|
|
|
.sorted()
|
2022-06-25 20:11:45 +00:00
|
|
|
.map(DefaultLightProviderCategory.init)
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadIfNeeded(_ name: String, vpnProtocol: String) {
|
|
|
|
guard let vpnProtocolType = VPNProtocolType(rawValue: vpnProtocol) else {
|
|
|
|
fatalError("Unrecognized VPN protocol: \(vpnProtocol)")
|
|
|
|
}
|
|
|
|
guard !providerManager.isAvailable(name, vpnProtocol: vpnProtocolType) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Task {
|
2023-07-02 10:51:50 +00:00
|
|
|
do {
|
|
|
|
try await providerManager.fetchProviderPublisher(
|
|
|
|
withName: name,
|
|
|
|
vpnProtocol: vpnProtocolType,
|
|
|
|
priority: .remoteThenBundle
|
|
|
|
).async()
|
|
|
|
} catch {
|
|
|
|
ErrorHandler.shared.handle(error)
|
|
|
|
}
|
2022-06-25 20:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|