2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// OpenVPNView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/17/24.
|
|
|
|
// Copyright (c) 2024 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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-10-23 13:42:54 +00:00
|
|
|
struct OpenVPNView: View, ModuleDraftEditing {
|
|
|
|
|
|
|
|
@Environment(\.navigationPath)
|
|
|
|
private var path
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-12-06 10:24:51 +00:00
|
|
|
let module: OpenVPNModule.Builder
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
@ObservedObject
|
2024-10-23 13:42:54 +00:00
|
|
|
var editor: ProfileEditor
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-12-10 13:13:10 +00:00
|
|
|
@ObservedObject
|
|
|
|
var modulePreferences: ModulePreferences
|
|
|
|
|
2024-11-08 11:37:09 +00:00
|
|
|
let impl: OpenVPNModule.Implementation?
|
|
|
|
|
2024-10-23 13:42:54 +00:00
|
|
|
private let isServerPushed: Bool
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
@State
|
|
|
|
private var isImporting = false
|
|
|
|
|
2024-10-28 18:55:42 +00:00
|
|
|
@State
|
|
|
|
private var paywallReason: PaywallReason?
|
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
init(serverConfiguration: OpenVPN.Configuration) {
|
2024-12-06 10:24:51 +00:00
|
|
|
module = OpenVPNModule.Builder(configurationBuilder: serverConfiguration.builder())
|
|
|
|
editor = ProfileEditor(modules: [module])
|
2024-12-10 13:13:10 +00:00
|
|
|
modulePreferences = ModulePreferences()
|
2024-10-28 19:07:19 +00:00
|
|
|
assert(module.configurationBuilder != nil, "isServerPushed must imply module.configurationBuilder != nil")
|
2024-11-08 11:37:09 +00:00
|
|
|
impl = nil
|
2024-09-23 13:02:26 +00:00
|
|
|
isServerPushed = true
|
|
|
|
}
|
|
|
|
|
2024-12-06 10:24:51 +00:00
|
|
|
init(module: OpenVPNModule.Builder, parameters: ModuleViewParameters) {
|
2024-10-23 13:42:54 +00:00
|
|
|
self.module = module
|
2024-12-06 10:24:51 +00:00
|
|
|
editor = parameters.editor
|
2024-12-10 13:13:10 +00:00
|
|
|
modulePreferences = parameters.preferences
|
2024-12-06 10:24:51 +00:00
|
|
|
impl = parameters.impl as? OpenVPNModule.Implementation
|
2024-09-23 13:02:26 +00:00
|
|
|
isServerPushed = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2024-10-19 22:21:38 +00:00
|
|
|
contentView
|
2024-11-22 09:05:08 +00:00
|
|
|
.moduleView(editor: editor, draft: draft.wrappedValue)
|
2024-12-09 01:00:55 +00:00
|
|
|
.modifier(ImportModifier(
|
|
|
|
draft: draft,
|
|
|
|
impl: impl,
|
|
|
|
isImporting: $isImporting,
|
|
|
|
errorHandler: errorHandler
|
|
|
|
))
|
2024-10-28 18:55:42 +00:00
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-10-10 22:24:06 +00:00
|
|
|
.navigationDestination(for: Subroute.self, destination: destination)
|
2024-12-03 15:18:05 +00:00
|
|
|
.themeAnimation(on: draft.wrappedValue.providerId, category: .modules)
|
2024-11-05 12:43:23 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
2024-10-10 22:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Content
|
|
|
|
|
|
|
|
private extension OpenVPNView {
|
|
|
|
|
2024-10-19 22:21:38 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var contentView: some View {
|
2024-10-28 19:07:19 +00:00
|
|
|
if let configuration = draft.wrappedValue.configurationBuilder {
|
|
|
|
ConfigurationView(
|
|
|
|
isServerPushed: isServerPushed,
|
|
|
|
configuration: configuration,
|
2024-12-09 01:00:55 +00:00
|
|
|
credentialsRoute: Subroute.credentials,
|
2024-12-09 07:44:13 +00:00
|
|
|
excludedEndpoints: excludedEndpoints
|
2024-10-28 19:07:19 +00:00
|
|
|
)
|
2024-10-19 22:21:38 +00:00
|
|
|
} else {
|
2024-12-04 19:50:51 +00:00
|
|
|
emptyConfigurationView
|
2024-10-19 22:21:38 +00:00
|
|
|
.modifier(providerModifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
@ViewBuilder
|
2024-12-04 19:50:51 +00:00
|
|
|
var emptyConfigurationView: some View {
|
|
|
|
if draft.wrappedValue.providerSelection == nil {
|
|
|
|
importButton
|
|
|
|
} else if let configuration = try? draft.wrappedValue.providerSelection?.configuration() {
|
|
|
|
providerConfigurationLink(with: configuration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigurationLink(with configuration: OpenVPN.Configuration) -> some View {
|
|
|
|
NavigationLink(Strings.Global.Nouns.configuration, value: Subroute.providerConfiguration(configuration))
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:18:05 +00:00
|
|
|
var importButton: some View {
|
2024-12-04 19:50:51 +00:00
|
|
|
Button(Strings.Modules.General.Rows.importFromFile.withTrailingDots) {
|
|
|
|
isImporting = true
|
2024-10-28 19:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
var providerModifier: some ViewModifier {
|
2024-10-15 19:34:02 +00:00
|
|
|
VPNProviderContentModifier(
|
2024-10-22 13:06:13 +00:00
|
|
|
providerId: providerId,
|
2024-12-10 13:13:10 +00:00
|
|
|
providerPreferences: nil,
|
2024-10-22 13:06:13 +00:00
|
|
|
selectedEntity: providerEntity,
|
2024-10-28 18:55:42 +00:00
|
|
|
paywallReason: $paywallReason,
|
2024-10-23 13:42:54 +00:00
|
|
|
entityDestination: Subroute.providerServer,
|
2024-10-15 19:34:02 +00:00
|
|
|
providerRows: {
|
|
|
|
moduleGroup(for: providerAccountRows)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-10-15 19:34:02 +00:00
|
|
|
)
|
2024-10-10 22:24:06 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 19:34:02 +00:00
|
|
|
var providerAccountRows: [ModuleRow]? {
|
|
|
|
[.push(caption: Strings.Modules.Openvpn.credentials, route: HashableRoute(Subroute.credentials))]
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
// MARK: - Destinations
|
|
|
|
|
|
|
|
private extension OpenVPNView {
|
|
|
|
enum Subroute: Hashable {
|
2024-10-23 13:42:54 +00:00
|
|
|
case providerServer
|
|
|
|
|
2024-12-04 19:50:51 +00:00
|
|
|
case providerConfiguration(OpenVPN.Configuration)
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
case credentials
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func destination(for route: Subroute) -> some View {
|
|
|
|
switch route {
|
2024-10-23 13:42:54 +00:00
|
|
|
case .providerServer:
|
2024-12-03 15:18:05 +00:00
|
|
|
draft.wrappedValue.providerSelection.map {
|
2024-10-23 13:42:54 +00:00
|
|
|
VPNProviderServerView(
|
2024-10-26 11:29:26 +00:00
|
|
|
moduleId: module.id,
|
2024-12-03 15:18:05 +00:00
|
|
|
providerId: $0.id,
|
|
|
|
selectedEntity: $0.entity,
|
2024-10-23 20:57:30 +00:00
|
|
|
filtersWithSelection: true,
|
2024-10-23 13:42:54 +00:00
|
|
|
onSelect: onSelectServer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:50:51 +00:00
|
|
|
case .providerConfiguration(let configuration):
|
|
|
|
Form {
|
|
|
|
ConfigurationView(
|
|
|
|
isServerPushed: false,
|
|
|
|
configuration: configuration.builder(),
|
2024-12-09 01:00:55 +00:00
|
|
|
credentialsRoute: nil,
|
2024-12-09 07:44:13 +00:00
|
|
|
excludedEndpoints: excludedEndpoints
|
2024-12-04 19:50:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
.themeForm()
|
|
|
|
.navigationTitle(Strings.Global.Nouns.configuration)
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
case .credentials:
|
2024-11-02 14:24:41 +00:00
|
|
|
Form {
|
|
|
|
OpenVPNCredentialsView(
|
2024-12-03 15:18:05 +00:00
|
|
|
providerId: draft.wrappedValue.providerId,
|
2024-11-02 14:24:41 +00:00
|
|
|
isInteractive: draft.isInteractive,
|
|
|
|
credentials: draft.credentials
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.navigationTitle(Strings.Modules.Openvpn.credentials)
|
|
|
|
.themeForm()
|
2024-11-01 22:32:35 +00:00
|
|
|
.themeAnimation(on: draft.wrappedValue.isInteractive, category: .modules)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-10 22:24:06 +00:00
|
|
|
}
|
|
|
|
|
2024-12-09 01:00:55 +00:00
|
|
|
// MARK: - Logic
|
|
|
|
|
|
|
|
private extension OpenVPNView {
|
2024-12-09 07:44:13 +00:00
|
|
|
var excludedEndpoints: ObservableList<ExtendedEndpoint> {
|
2024-12-10 13:13:10 +00:00
|
|
|
editor.excludedEndpoints(for: module.id, preferences: modulePreferences)
|
2024-12-09 01:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func onSelectServer(server: VPNServer, preset: VPNPreset<OpenVPN.Configuration>) {
|
|
|
|
draft.wrappedValue.providerEntity = VPNEntity(server: server, preset: preset)
|
2024-12-10 13:13:10 +00:00
|
|
|
resetExcludedEndpointsWithCurrentProviderEntity()
|
2024-12-09 01:00:55 +00:00
|
|
|
path.wrappedValue.removeLast()
|
|
|
|
}
|
2024-12-10 13:13:10 +00:00
|
|
|
|
|
|
|
// filter out exclusions unrelated to current server
|
|
|
|
func resetExcludedEndpointsWithCurrentProviderEntity() {
|
|
|
|
do {
|
|
|
|
let cfg = try draft.wrappedValue.providerSelection?.configuration()
|
|
|
|
editor.profile.attributes.editPreferences(inModule: module.id) {
|
|
|
|
if let cfg {
|
|
|
|
$0.excludedEndpoints = Set(cfg.remotes?.filter {
|
|
|
|
modulePreferences.isExcludedEndpoint($0)
|
|
|
|
} ?? [])
|
|
|
|
} else {
|
|
|
|
$0.excludedEndpoints = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to build provider configuration for excluded endpoints: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2024-12-09 01:00:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
#Preview {
|
2024-12-09 01:00:55 +00:00
|
|
|
let module = OpenVPNModule.Builder(configurationBuilder: .forPreviews)
|
2024-09-23 13:02:26 +00:00
|
|
|
return module.preview(title: "OpenVPN")
|
|
|
|
}
|