2022-04-12 13:09:14 +00:00
|
|
|
//
|
2022-08-28 07:19:15 +00:00
|
|
|
// VPNManager+Actions.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/30/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
|
|
|
|
|
|
|
// 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-04-12 13:09:14 +00:00
|
|
|
let cfg = try vpnConfiguration(withProfile: profile)
|
2022-05-19 18:19:35 +00:00
|
|
|
|
|
|
|
profileManager.activateProfile(profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
await reconnect(cfg)
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
2022-04-12 13:09:14 +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")
|
|
|
|
throw PassepartoutError.missingProfile
|
|
|
|
}
|
|
|
|
if !result.isReady {
|
|
|
|
try await profileManager.makeProfileReady(profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldServerId = profile.providerServerId()
|
|
|
|
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-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
|
|
|
let cfg = try vpnConfiguration(withProfile: profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
await reconnect(cfg)
|
2022-05-20 06:27:31 +00:00
|
|
|
return profile
|
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
|
|
|
let cfg = try vpnConfiguration(withProfile: profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
await reinstate(cfg)
|
|
|
|
}
|
|
|
|
}
|