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

111 lines
3.8 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.
// Copyright (c) 2022 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 Foundation
import TunnelKitCore
extension Profile {
public init(_ providerMetadata: ProviderMetadata, server: ProviderServer) {
guard let vpnProtocol = server.presets?.first?.vpnProtocol else {
fatalError("Server has no presets")
}
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)
}
2022-06-23 21:31:01 +00:00
public var providerName: String? {
provider?.name
}
2022-04-12 13:09:14 +00:00
public func providerServer(_ providerManager: ProviderManager) -> ProviderServer? {
guard let serverId = provider?.vpnSettings[currentVPNProtocol]?.serverId else {
return nil
}
return providerManager.server(withId: serverId)
}
public func providerServerId() -> String? {
return provider?.vpnSettings[currentVPNProtocol]?.serverId
}
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
}
public func providerFavoriteLocationIds() -> Set<String>? {
return provider?.vpnSettings[currentVPNProtocol]?.favoriteLocationIds
}
public mutating func setProviderFavoriteLocationIds(_ ids: Set<String>?) {
provider?.vpnSettings[currentVPNProtocol]?.favoriteLocationIds = ids
}
public func providerCustomEndpoint() -> Endpoint? {
return provider?.vpnSettings[currentVPNProtocol]?.customEndpoint
}
public mutating func setProviderCustomEndpoint(_ endpoint: Endpoint?) {
provider?.vpnSettings[currentVPNProtocol]?.customEndpoint = endpoint
}
public func providerAccount() -> Profile.Account? {
return provider?.vpnSettings[currentVPNProtocol]?.account
}
public mutating func setProviderAccount(_ account: Profile.Account?) {
provider?.vpnSettings[currentVPNProtocol]?.account = account
}
}
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
}
}