2022-04-12 13:09:14 +00:00
|
|
|
//
|
2022-08-28 07:19:15 +00:00
|
|
|
// VPNManager+Configuration.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/12/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
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
import PassepartoutUtils
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
extension VPNManager {
|
2022-06-15 18:53:37 +00:00
|
|
|
private var vpnPreferences: VPNPreferences {
|
|
|
|
DefaultVPNPreferences(
|
2022-06-17 06:27:56 +00:00
|
|
|
tunnelLogPath: tunnelLogPath,
|
2022-06-15 18:53:37 +00:00
|
|
|
tunnelLogFormat: tunnelLogFormat,
|
|
|
|
masksPrivateData: masksPrivateData
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func vpnConfigurationWithCurrentProfile() -> VPNConfiguration? {
|
|
|
|
do {
|
|
|
|
guard profileManager.isCurrentProfileActive() else {
|
|
|
|
pp_log.info("Skipping VPN configuration, current profile is not active")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return try vpnConfiguration(withProfile: profileManager.currentProfile.value)
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func vpnConfiguration(withProfile profile: Profile) throws -> VPNConfiguration {
|
|
|
|
do {
|
|
|
|
if profile.requiresCredentials {
|
|
|
|
guard !profile.account.isEmpty else {
|
|
|
|
throw PassepartoutError.missingAccount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:44:48 +00:00
|
|
|
// specific provider customizations
|
|
|
|
var newPassword: String?
|
|
|
|
if let providerName = profile.providerName {
|
|
|
|
switch providerName {
|
|
|
|
case .mullvad:
|
|
|
|
newPassword = "m"
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// IMPORTANT: must commit password to keychain (tunnel needs a password reference)
|
2022-10-15 14:44:48 +00:00
|
|
|
profileManager.savePassword(forProfile: profile, newPassword: newPassword)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
let parameters = VPNConfigurationParameters(
|
|
|
|
profile,
|
2022-06-23 21:31:01 +00:00
|
|
|
appGroup: appGroup,
|
2022-06-15 18:53:37 +00:00
|
|
|
preferences: vpnPreferences,
|
2022-04-13 17:12:16 +00:00
|
|
|
passwordReference: profileManager.passwordReference(forProfile: profile),
|
2022-04-13 18:12:25 +00:00
|
|
|
withNetworkSettings: isNetworkSettingsSupported(),
|
2022-04-13 17:12:16 +00:00
|
|
|
withCustomRules: isOnDemandRulesSupported()
|
2022-04-12 13:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
switch profile.currentVPNProtocol {
|
|
|
|
case .openVPN:
|
|
|
|
let settings: Profile.OpenVPNSettings
|
|
|
|
if profile.isProvider {
|
|
|
|
settings = try profile.providerOpenVPNSettings(withManager: providerManager)
|
|
|
|
} else {
|
|
|
|
guard let hostSettings = profile.hostOpenVPNSettings else {
|
2022-07-07 14:43:40 +00:00
|
|
|
fatalError("Profile currentVPNProtocol is OpenVPN, but host has no OpenVPN settings")
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
settings = hostSettings
|
|
|
|
}
|
2022-04-14 05:24:03 +00:00
|
|
|
return try settings.vpnConfiguration(parameters)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
case .wireGuard:
|
|
|
|
let settings: Profile.WireGuardSettings
|
|
|
|
if profile.isProvider {
|
|
|
|
settings = try profile.providerWireGuardSettings(withManager: providerManager)
|
|
|
|
} else {
|
|
|
|
guard let hostSettings = profile.hostWireGuardSettings else {
|
2022-07-07 14:43:40 +00:00
|
|
|
fatalError("Profile currentVPNProtocol is WireGuard, but host has no WireGuard settings")
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
settings = hostSettings
|
|
|
|
}
|
2022-04-14 05:24:03 +00:00
|
|
|
return try settings.vpnConfiguration(parameters)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log.error("Unable to build VPNConfiguration: \(error)")
|
|
|
|
|
|
|
|
// UI is certainly interested in configuration errors
|
2022-05-20 06:29:14 +00:00
|
|
|
configurationError.send((profile, error))
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|