passepartout-apple/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/VPNManager+Extensions.swift

128 lines
4.7 KiB
Swift
Raw Normal View History

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()
extension VPNManager {
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
}
guard let profileId = profileManager.activeProfileId else {
2022-04-12 13:09:14 +00:00
pp_log.warning("No active profile")
return
}
if let newServerId = newServerId {
_ = try await connect(with: profileId, toServer: newServerId)
} else {
_ = try await connect(with: profileId)
}
2022-04-12 13:09:14 +00:00
}
@discardableResult
public func connect(with profileId: UUID, newPassword: String? = nil) async throws -> Profile {
let result = try profileManager.liveProfileEx(withId: profileId)
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")
return profile
2022-04-12 13:09:14 +00:00
}
if !result.isReady {
try await profileManager.makeProfileReady(profile)
}
pp_log.info("Connecting to: \(profile.logDescription)")
if let newPassword {
profile.account.password = newPassword
}
profileManager.activateProfile(profile)
2023-07-02 10:51:50 +00:00
try await reconnect(profile)
return profile
2022-04-12 13:09:14 +00:00
}
2023-03-17 20:55:47 +00:00
@discardableResult
public func connect(with profileId: UUID, toServer newServerId: String) async throws -> Profile {
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)
}
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-04-12 13:09:14 +00:00
pp_log.info("Profile \(profile.logDescription) is already active and connected to: \(newServer.logDescription)")
return profile
2022-04-12 13:09:14 +00:00
}
pp_log.info("Connecting to: \(profile.logDescription) @ \(newServer.logDescription)")
profile.setProviderServer(newServer)
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")
return profile
2022-04-12 13:09:14 +00:00
}
2023-07-02 10:51:50 +00:00
try await reconnect(profile)
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)
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
}
}