2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// AddHostViewModel.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/14/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 PassepartoutLibrary
|
2022-04-12 13:09:14 +00:00
|
|
|
import TunnelKitOpenVPN
|
|
|
|
import TunnelKitWireGuard
|
|
|
|
|
|
|
|
extension AddHostView {
|
2022-04-21 13:08:40 +00:00
|
|
|
struct ViewModel: Equatable {
|
2022-04-21 10:13:29 +00:00
|
|
|
private var isNamePreset = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var profileName = ""
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private(set) var requiresPassphrase = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var encryptionPassphrase = ""
|
|
|
|
|
|
|
|
var processedProfile: Profile = .placeholder
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private(set) var errorMessage: String?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var isAskingOverwrite = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-21 10:13:29 +00:00
|
|
|
mutating func presetName(withURL url: URL) {
|
|
|
|
guard !isNamePreset else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isNamePreset = true
|
2022-10-30 09:22:44 +00:00
|
|
|
profileName = url.filename
|
2022-04-21 10:13:29 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@MainActor
|
2022-04-12 13:09:14 +00:00
|
|
|
mutating func processURL(
|
|
|
|
_ url: URL,
|
|
|
|
with profileManager: ProfileManager,
|
|
|
|
replacingExisting: Bool,
|
|
|
|
deletingURLOnSuccess: Bool
|
|
|
|
) {
|
|
|
|
profileName = profileName.stripped
|
|
|
|
guard !profileName.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !replacingExisting {
|
|
|
|
guard !profileManager.isExistingProfile(withName: profileName) else {
|
|
|
|
isAskingOverwrite = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage = nil
|
|
|
|
do {
|
|
|
|
let profile = try profileManager.profile(
|
|
|
|
withHeader: .init(name: profileName),
|
|
|
|
fromURL: url,
|
|
|
|
passphrase: encryptionPassphrase
|
|
|
|
)
|
|
|
|
processedProfile = profile
|
|
|
|
|
|
|
|
if deletingURLOnSuccess {
|
|
|
|
try? FileManager.default.removeItem(at: url)
|
|
|
|
}
|
|
|
|
} catch {
|
2023-07-02 10:51:50 +00:00
|
|
|
if case Passepartout.ProfileError.decryptionFailure = error {
|
2022-04-12 13:09:14 +00:00
|
|
|
requiresPassphrase = true
|
2023-07-02 10:51:50 +00:00
|
|
|
} else {
|
2022-04-12 13:09:14 +00:00
|
|
|
requiresPassphrase = false
|
|
|
|
}
|
2023-07-02 10:51:50 +00:00
|
|
|
setErrorMessage(for: error)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-13 06:53:50 +00:00
|
|
|
@MainActor
|
2022-04-12 13:09:14 +00:00
|
|
|
mutating func addProcessedProfile(to profileManager: ProfileManager) -> Bool {
|
|
|
|
guard !processedProfile.isPlaceholder else {
|
|
|
|
assertionFailure("Saving profile without processing first?")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
errorMessage = nil
|
|
|
|
profileManager.saveProfile(processedProfile, isActive: nil)
|
|
|
|
return true
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-02 10:51:50 +00:00
|
|
|
private mutating func setErrorMessage(for error: Error) {
|
|
|
|
setErrorMessage(AppError(error).localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
private mutating func setErrorMessage(_ message: String) {
|
|
|
|
errorMessage = message.withTrailingDot
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|