passepartout-apple/PassepartoutLibrary/Sources/PassepartoutVPN/Extensions/Provider+Extensions.swift

118 lines
3.6 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// Provider+Extensions.swift
// Passepartout
//
// Created by Davide De Rosa on 3/14/22.
2024-01-14 13:34:21 +00:00
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +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 Foundation
2023-05-24 16:19:47 +00:00
import PassepartoutCore
import PassepartoutProviders
2022-04-12 13:09:14 +00:00
import TunnelKitCore
extension Profile {
public init(_ providerMetadata: ProviderMetadata, server: ProviderServer) {
guard let vpnProtocol = server.presets?.first?.vpnProtocol else {
fatalError("Server has no presets")
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var provider = Provider(providerMetadata.name)
var settings = Provider.Settings()
settings.serverId = server.id
settings.presetId = server.presetIds.first
provider.vpnSettings[vpnProtocol] = settings
self.init(name: providerMetadata.fullName, provider: provider)
}
2023-03-17 20:55:47 +00:00
2022-06-23 21:31:01 +00:00
public var providerName: String? {
provider?.name
}
2022-04-12 13:09:14 +00:00
public var providerServerId: String? {
2022-09-04 18:09:31 +00:00
provider?.vpnSettings[currentVPNProtocol]?.serverId
2022-04-12 13:09:14 +00:00
}
public mutating func setProviderServer(_ server: ProviderServer) {
let oldServerId = provider?.vpnSettings[currentVPNProtocol]?.serverId
provider?.vpnSettings[currentVPNProtocol]?.serverId = server.id
provider?.vpnSettings[currentVPNProtocol]?.customEndpoint = nil
// if server changed, pick first preset
if server.id != oldServerId {
provider?.vpnSettings[currentVPNProtocol]?.presetId = server.presetIds.first
}
}
public func providerPreset(_ server: ProviderServer) -> ProviderServer.Preset? {
guard let presetId = provider?.vpnSettings[currentVPNProtocol]?.presetId else {
return nil
}
return server.preset(withId: presetId)
}
public mutating func setProviderPreset(_ preset: ProviderServer.Preset) {
provider?.vpnSettings[currentVPNProtocol]?.presetId = preset.id
}
2023-03-17 20:55:47 +00:00
public var providerFavoriteLocationIds: Set<String>? {
get {
provider?.vpnSettings[currentVPNProtocol]?.favoriteLocationIds
}
set {
provider?.vpnSettings[currentVPNProtocol]?.favoriteLocationIds = newValue
}
2022-04-12 13:09:14 +00:00
}
public var providerCustomEndpoint: Endpoint? {
get {
provider?.vpnSettings[currentVPNProtocol]?.customEndpoint
}
set {
provider?.vpnSettings[currentVPNProtocol]?.customEndpoint = newValue
}
2022-04-12 13:09:14 +00:00
}
2023-03-17 20:55:47 +00:00
public var providerAccount: Profile.Account? {
get {
provider?.vpnSettings[currentVPNProtocol]?.account
}
set {
provider?.vpnSettings[currentVPNProtocol]?.account = newValue
}
2022-04-12 13:09:14 +00:00
}
2023-03-17 20:55:47 +00:00
public var providerRandomizesServer: Bool? {
get {
provider?.randomizesServer
}
set {
provider?.randomizesServer = newValue
}
2022-04-12 13:09:14 +00:00
}
}
extension Profile.Provider: ProfileSubtype {
public var vpnProtocols: [VPNProtocolType] {
2022-06-23 21:31:01 +00:00
Array(vpnSettings.keys)
2022-04-12 13:09:14 +00:00
}
}