2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// PassepartoutProviders+TunnelKit.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/26/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 Combine
|
2023-04-04 07:50:45 +00:00
|
|
|
import Foundation
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
2023-05-24 16:19:47 +00:00
|
|
|
import PassepartoutProviders
|
2023-04-04 07:50:45 +00:00
|
|
|
import TunnelKitOpenVPN
|
|
|
|
import TunnelKitWireGuard
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
extension ProviderServer.Preset {
|
|
|
|
public var openVPNConfiguration: OpenVPN.Configuration? {
|
|
|
|
guard vpnProtocol == .openVPN else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let json = vpnConfiguration["cfg"] else {
|
|
|
|
pp_log.error("Unable to parse preset OpenVPN configuration: no cfg")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
return try json.decode(OpenVPN.Configuration.self)
|
|
|
|
} catch {
|
|
|
|
pp_log.error("Unable to parse preset OpenVPN configuration: \(error)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var openVPNEndpoints: [EndpointProtocol] {
|
|
|
|
guard vpnProtocol == .openVPN else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
guard let json = vpnConfiguration["endpoints"]?.arrayValue else {
|
|
|
|
pp_log.error("Unable to parse preset OpenVPN configuration: no endpoints")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let endpoints = json
|
|
|
|
.compactMap(\.stringValue)
|
|
|
|
.compactMap(EndpointProtocol.init)
|
|
|
|
|
|
|
|
guard !endpoints.isEmpty else {
|
|
|
|
pp_log.error("Unable to parse preset OpenVPN configuration: empty/malformed endpoints")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return endpoints
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var wireGuardConfiguration: WireGuard.Configuration? {
|
|
|
|
guard vpnProtocol == .wireGuard else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
return try vpnConfiguration.decode(WireGuard.Configuration.self)
|
|
|
|
} catch {
|
|
|
|
pp_log.error("Unable to parse preset WireGuard configuration: \(error)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension OpenVPN.ConfigurationBuilder {
|
|
|
|
public mutating func setRemotes(
|
|
|
|
from preset: ProviderServer.Preset,
|
|
|
|
with server: ProviderServer,
|
|
|
|
excludingHostname: Bool
|
|
|
|
) throws {
|
|
|
|
var remotes: [Endpoint] = []
|
|
|
|
|
|
|
|
let endpoints = preset.openVPNEndpoints
|
|
|
|
if !excludingHostname, let hostname = server.hostname {
|
|
|
|
endpoints.forEach { ep in
|
|
|
|
remotes.append(.init(hostname, ep))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endpoints.forEach { ep in
|
|
|
|
server.ipAddresses.forEach { addr in
|
|
|
|
remotes.append(.init(addr, ep))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
guard !remotes.isEmpty else {
|
|
|
|
pp_log.warning("Excluding hostname but server has no ipAddresses either")
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.emptyEndpoints(server: server)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.remotes = remotes
|
|
|
|
}
|
|
|
|
}
|