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

132 lines
4.8 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// VPNManager+Actions.swift
2022-04-12 13:09:14 +00:00
// Passepartout
//
// Created by Davide De Rosa on 3/30/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 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
import PassepartoutUtils
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
}
2022-04-12 13:09:14 +00:00
let cfg = try vpnConfiguration(withProfile: profile)
profileManager.activateProfile(profile)
2022-04-12 13:09:14 +00:00
await reconnect(cfg)
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")
throw PassepartoutError.missingProfile
}
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")
throw PassepartoutError.missingProviderServer
}
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)
let cfg = try vpnConfiguration(withProfile: profile)
2022-04-12 13:09:14 +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")
return profile
2022-04-12 13:09:14 +00:00
}
await reconnect(cfg)
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)
let cfg = try vpnConfiguration(withProfile: profile)
2022-04-12 13:09:14 +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
}
await reinstate(cfg)
}
}