2022-04-12 13:09:14 +00:00
|
|
|
//
|
2023-05-24 16:19:47 +00:00
|
|
|
// VPNManager+Extensions.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/30/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
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// IMPORTANT: if active profile is set/modified and it happens to also be
|
|
|
|
// current profile, this must be updated too. this is done in
|
|
|
|
// ProfileManager.activateProfile()
|
|
|
|
|
2022-08-28 07:19:15 +00:00
|
|
|
extension VPNManager {
|
2022-06-25 20:11:45 +00:00
|
|
|
public func connectWithActiveProfile(toServer newServerId: String?) async throws {
|
2022-04-12 13:09:14 +00:00
|
|
|
guard currentState.vpnStatus != .connected else {
|
|
|
|
pp_log.warning("VPN is already connected")
|
|
|
|
return
|
|
|
|
}
|
2022-05-03 08:25:10 +00:00
|
|
|
guard let profileId = profileManager.activeProfileId else {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.warning("No active profile")
|
|
|
|
return
|
|
|
|
}
|
2022-06-25 20:11:45 +00:00
|
|
|
if let newServerId = newServerId {
|
2022-10-15 20:29:29 +00:00
|
|
|
_ = try await connect(with: profileId, toServer: newServerId)
|
2022-06-25 20:11:45 +00:00
|
|
|
} else {
|
2022-10-15 20:29:29 +00:00
|
|
|
_ = try await connect(with: profileId)
|
2022-06-25 20:11:45 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 06:27:31 +00:00
|
|
|
@discardableResult
|
2023-03-17 15:49:35 +00:00
|
|
|
public func connect(with profileId: UUID, newPassword: String? = nil) async throws -> Profile {
|
2022-05-03 10:38:10 +00:00
|
|
|
let result = try profileManager.liveProfileEx(withId: profileId)
|
2023-03-17 15:49:35 +00:00
|
|
|
var profile = result.profile
|
2022-04-12 13:09:14 +00:00
|
|
|
guard !profileManager.isActiveProfile(profileId) ||
|
|
|
|
currentState.vpnStatus != .connected else {
|
|
|
|
|
|
|
|
pp_log.warning("Profile \(profile.logDescription) is already active and connected")
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
if !result.isReady {
|
|
|
|
try await profileManager.makeProfileReady(profile)
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:19:35 +00:00
|
|
|
pp_log.info("Connecting to: \(profile.logDescription)")
|
2023-03-17 15:49:35 +00:00
|
|
|
if let newPassword {
|
|
|
|
profile.account.password = newPassword
|
|
|
|
}
|
2022-05-19 18:19:35 +00:00
|
|
|
|
|
|
|
profileManager.activateProfile(profile)
|
2023-07-02 10:51:50 +00:00
|
|
|
try await reconnect(profile)
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-20 06:27:31 +00:00
|
|
|
@discardableResult
|
|
|
|
public func connect(with profileId: UUID, toServer newServerId: String) async throws -> Profile {
|
2022-05-03 10:38:10 +00:00
|
|
|
let result = try profileManager.liveProfileEx(withId: profileId)
|
2022-04-12 13:09:14 +00:00
|
|
|
var profile = result.profile
|
|
|
|
guard profile.isProvider else {
|
|
|
|
assertionFailure("Profile \(profile.logDescription) is not a provider")
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.notProvider(profile: profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
if !result.isReady {
|
|
|
|
try await profileManager.makeProfileReady(profile)
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:19:32 +00:00
|
|
|
let oldServerId = profile.providerServerId
|
2022-04-12 13:09:14 +00:00
|
|
|
guard let newServer = providerManager.server(withId: newServerId) else {
|
|
|
|
pp_log.warning("Server \(newServerId) not found")
|
2023-07-02 10:51:50 +00:00
|
|
|
throw Passepartout.VPNError.providerServerNotFound(profile: profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
guard !profileManager.isActiveProfile(profileId) ||
|
|
|
|
currentState.vpnStatus != .connected ||
|
|
|
|
oldServerId != newServer.id else {
|
2022-06-25 20:11:45 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.info("Profile \(profile.logDescription) is already active and connected to: \(newServer.logDescription)")
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp_log.info("Connecting to: \(profile.logDescription) @ \(newServer.logDescription)")
|
|
|
|
profile.setProviderServer(newServer)
|
|
|
|
|
2022-05-19 18:19:35 +00:00
|
|
|
profileManager.activateProfile(profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
guard !profileManager.isCurrentProfile(profileId) else {
|
|
|
|
pp_log.debug("Active profile is current, will reconnect via observation")
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-02 10:51:50 +00:00
|
|
|
try await reconnect(profile)
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public func modifyActiveProfile(_ block: (inout Profile) -> Void) async throws {
|
|
|
|
guard var profile = profileManager.activeProfile else {
|
|
|
|
pp_log.warning("Nothing to modify, no active profile")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_log.info("Modifying active profile")
|
|
|
|
block(&profile)
|
|
|
|
|
2022-05-19 18:19:35 +00:00
|
|
|
profileManager.activateProfile(profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
guard !profileManager.isCurrentProfile(profile.id) else {
|
|
|
|
pp_log.debug("Active profile is current, will reinstate via observation")
|
|
|
|
return
|
|
|
|
}
|
2023-07-02 10:51:50 +00:00
|
|
|
try await reinstate(profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|