2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// AddHostViewModel.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/14/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 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
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var profileName = ""
|
|
|
|
|
|
|
|
private(set) var requiresPassphrase = false
|
|
|
|
|
|
|
|
var encryptionPassphrase = ""
|
|
|
|
|
|
|
|
var processedProfile: Profile = .placeholder
|
|
|
|
|
|
|
|
private(set) var errorMessage: String?
|
|
|
|
|
|
|
|
var isAskingOverwrite = false
|
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 {
|
|
|
|
switch error {
|
|
|
|
case OpenVPN.ConfigurationError.encryptionPassphrase,
|
|
|
|
OpenVPN.ConfigurationError.unableToDecrypt:
|
|
|
|
|
|
|
|
requiresPassphrase = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
requiresPassphrase = false
|
|
|
|
}
|
|
|
|
setMessage(forParsingError: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
private mutating func setMessage(forParsingError error: Error) {
|
|
|
|
errorMessage = error.localizedVPNParsingDescription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|