2022-06-23 21:31:01 +00:00
|
|
|
//
|
2023-05-24 16:19:47 +00:00
|
|
|
// Profile+ProviderManager.swift
|
2022-06-23 21:31:01 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/20/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-06-23 21:31:01 +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
|
|
|
|
import PassepartoutCore
|
|
|
|
import PassepartoutProviders
|
2023-05-24 16:19:47 +00:00
|
|
|
import PassepartoutVPN
|
2022-06-23 21:31:01 +00:00
|
|
|
|
|
|
|
extension Profile {
|
2022-08-28 07:19:15 +00:00
|
|
|
public func providerServer(_ providerManager: ProviderManager) -> ProviderServer? {
|
2023-03-19 07:19:32 +00:00
|
|
|
guard let serverId = providerServerId else {
|
2022-08-28 07:19:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return providerManager.server(withId: serverId)
|
|
|
|
}
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
public func providerOpenVPNSettings(withManager providerManager: ProviderManager) throws -> Profile.OpenVPNSettings {
|
|
|
|
guard isProvider else {
|
|
|
|
fatalError("Not a provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
// infer remotes from preset + server
|
2023-03-19 07:19:32 +00:00
|
|
|
guard let selectedServer = providerServer(providerManager) else {
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.providerServerNotFound(profile: self)
|
2022-06-23 21:31:01 +00:00
|
|
|
}
|
2023-03-19 07:19:32 +00:00
|
|
|
let server: ProviderServer
|
|
|
|
if providerRandomizesServer ?? false {
|
|
|
|
let location = selectedServer.location(withVPNProtocol: currentVPNProtocol)
|
|
|
|
let servers = providerManager.servers(forLocation: location)
|
|
|
|
guard let randomServerId = servers.randomElement()?.id,
|
|
|
|
let randomServer = providerManager.server(withId: randomServerId) else {
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.providerServerNotFound(profile: self)
|
2023-03-19 07:19:32 +00:00
|
|
|
}
|
|
|
|
server = randomServer
|
|
|
|
} else {
|
|
|
|
server = selectedServer
|
|
|
|
}
|
2022-06-23 21:31:01 +00:00
|
|
|
guard let preset = providerPreset(server) else {
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.providerPresetNotFound(profile: self)
|
2022-06-23 21:31:01 +00:00
|
|
|
}
|
|
|
|
guard var builder = preset.openVPNConfiguration?.builder() else {
|
|
|
|
fatalError("Preset \(preset.id) has no OpenVPN configuration")
|
|
|
|
}
|
|
|
|
try builder.setRemotes(from: preset, with: server, excludingHostname: !networkSettings.resolvesHostname)
|
|
|
|
|
|
|
|
// enforce default gateway
|
|
|
|
builder.routingPolicies = [.IPv4, .IPv6]
|
|
|
|
|
|
|
|
// apply provider settings (username, custom endpoint)
|
|
|
|
let cfg = builder.build()
|
|
|
|
var settings = OpenVPNSettings(configuration: cfg)
|
2023-03-19 07:19:32 +00:00
|
|
|
settings.account = providerAccount
|
|
|
|
settings.customEndpoint = providerCustomEndpoint
|
2022-06-23 21:31:01 +00:00
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
public func providerWireGuardSettings(withManager providerManager: ProviderManager) throws -> Profile.WireGuardSettings {
|
|
|
|
guard isProvider else {
|
|
|
|
fatalError("Not a provider")
|
|
|
|
}
|
|
|
|
fatalError("WireGuard not yet implemented for providers")
|
|
|
|
}
|
|
|
|
}
|