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-01 22:32:35 +00:00
|
|
|
import AppLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-10-28 19:07:19 +00:00
|
|
|
import CPassepartoutOpenVPNOpenSSL
|
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
|
|
|
|
|
|
|
@ObservedObject
|
2024-10-23 13:42:54 +00:00
|
|
|
var editor: ProfileEditor
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-23 13:42:54 +00:00
|
|
|
let module: OpenVPNModule.Builder
|
2024-09-23 13:02:26 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var importURL: URL?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var importPassphrase: String?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var requiresPassphrase = 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) {
|
|
|
|
let module = OpenVPNModule.Builder(configurationBuilder: serverConfiguration.builder())
|
2024-10-10 22:24:06 +00:00
|
|
|
let editor = ProfileEditor(modules: [module])
|
2024-10-28 19:07:19 +00:00
|
|
|
assert(module.configurationBuilder != nil, "isServerPushed must imply module.configurationBuilder != nil")
|
2024-10-10 22:24:06 +00:00
|
|
|
|
|
|
|
self.editor = editor
|
2024-10-23 13:42:54 +00:00
|
|
|
self.module = module
|
2024-09-23 13:02:26 +00:00
|
|
|
isServerPushed = true
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
init(editor: ProfileEditor, module: OpenVPNModule.Builder) {
|
2024-09-23 13:02:26 +00:00
|
|
|
self.editor = editor
|
2024-10-23 13:42:54 +00:00
|
|
|
self.module = module
|
2024-09-23 13:02:26 +00:00
|
|
|
isServerPushed = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2024-10-19 22:21:38 +00:00
|
|
|
contentView
|
2024-10-23 13:42:54 +00:00
|
|
|
.moduleView(editor: editor, draft: draft.wrappedValue, withName: !isServerPushed)
|
2024-10-28 19:07:19 +00:00
|
|
|
.fileImporter(
|
|
|
|
isPresented: $isImporting,
|
|
|
|
allowedContentTypes: [.item],
|
|
|
|
onCompletion: importConfiguration
|
|
|
|
)
|
2024-10-28 18:55:42 +00:00
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-10-28 19:07:19 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
2024-10-10 22:24:06 +00:00
|
|
|
.navigationDestination(for: Subroute.self, destination: destination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
credentialsRoute: Subroute.credentials
|
|
|
|
)
|
2024-10-19 22:21:38 +00:00
|
|
|
} else {
|
2024-10-28 19:07:19 +00:00
|
|
|
importView
|
2024-10-19 22:21:38 +00:00
|
|
|
.modifier(providerModifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var importView: some View {
|
|
|
|
if providerId.wrappedValue == nil {
|
|
|
|
Button(Strings.Modules.General.Rows.importFromFile) {
|
|
|
|
isImporting = true
|
|
|
|
}
|
|
|
|
.alert(
|
2024-11-02 14:23:36 +00:00
|
|
|
module.moduleType.localizedDescription,
|
2024-10-28 19:07:19 +00:00
|
|
|
isPresented: $requiresPassphrase,
|
|
|
|
presenting: importURL,
|
|
|
|
actions: { url in
|
|
|
|
SecureField(
|
|
|
|
Strings.Placeholders.secret,
|
|
|
|
text: $importPassphrase ?? ""
|
|
|
|
)
|
|
|
|
Button(Strings.Alerts.Import.Passphrase.ok) {
|
|
|
|
importConfiguration(from: .success(url))
|
|
|
|
}
|
|
|
|
Button(Strings.Global.cancel, role: .cancel) {
|
|
|
|
isImporting = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
Text(Strings.Alerts.Import.Passphrase.message($0.lastPathComponent))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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-22 13:06:13 +00:00
|
|
|
var providerId: Binding<ProviderID?> {
|
2024-10-23 13:42:54 +00:00
|
|
|
editor.binding(forProviderOf: module.id)
|
2024-10-22 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var providerEntity: Binding<VPNEntity<OpenVPN.Configuration>?> {
|
2024-10-23 13:42:54 +00:00
|
|
|
editor.binding(forProviderEntityOf: module.id)
|
2024-10-22 13:06:13 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension OpenVPNView {
|
2024-10-23 13:42:54 +00:00
|
|
|
func onSelectServer(server: VPNServer, preset: VPNPreset<OpenVPN.Configuration>) {
|
|
|
|
providerEntity.wrappedValue = VPNEntity(server: server, preset: preset)
|
|
|
|
path.wrappedValue.removeLast()
|
|
|
|
}
|
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
func importConfiguration(from result: Result<URL, Error>) {
|
|
|
|
do {
|
|
|
|
let url = try result.get()
|
|
|
|
guard url.startAccessingSecurityScopedResource() else {
|
|
|
|
throw AppError.permissionDenied
|
|
|
|
}
|
|
|
|
defer {
|
|
|
|
url.stopAccessingSecurityScopedResource()
|
|
|
|
}
|
|
|
|
importURL = url
|
|
|
|
|
|
|
|
let parsed = try StandardOpenVPNParser(decrypter: OSSLTLSBox())
|
|
|
|
.parsed(fromURL: url, passphrase: importPassphrase)
|
|
|
|
|
|
|
|
draft.wrappedValue.configurationBuilder = parsed.configuration.builder()
|
|
|
|
} catch StandardOpenVPNParserError.encryptionPassphrase,
|
|
|
|
StandardOpenVPNParserError.unableToDecrypt {
|
|
|
|
Task {
|
|
|
|
// XXX: re-present same alert after artificial delay
|
|
|
|
try? await Task.sleep(for: .milliseconds(500))
|
|
|
|
importPassphrase = nil
|
|
|
|
requiresPassphrase = true
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to import OpenVPN configuration: \(error)")
|
|
|
|
errorHandler.handle(
|
|
|
|
(error as? StandardOpenVPNParserError)?.asPassepartoutError ?? error,
|
2024-11-02 14:23:36 +00:00
|
|
|
title: module.moduleType.localizedDescription
|
2024-10-28 19:07:19 +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-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:
|
|
|
|
providerId.wrappedValue.map {
|
|
|
|
VPNProviderServerView(
|
2024-10-26 11:29:26 +00:00
|
|
|
moduleId: module.id,
|
2024-10-23 13:42:54 +00:00
|
|
|
providerId: $0,
|
|
|
|
configurationType: OpenVPN.Configuration.self,
|
2024-10-23 15:58:04 +00:00
|
|
|
selectedEntity: providerEntity.wrappedValue,
|
2024-10-23 20:57:30 +00:00
|
|
|
filtersWithSelection: true,
|
2024-10-25 09:38:27 +00:00
|
|
|
selectTitle: Strings.Providers.selectEntity,
|
2024-10-23 13:42:54 +00:00
|
|
|
onSelect: onSelectServer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
case .credentials:
|
2024-11-01 22:32:35 +00:00
|
|
|
OpenVPNCredentialsView(
|
2024-10-23 13:42:54 +00:00
|
|
|
isInteractive: draft.isInteractive,
|
|
|
|
credentials: draft.credentials
|
2024-10-10 22:24:06 +00:00
|
|
|
)
|
2024-11-01 22:32:35 +00:00
|
|
|
.themeAnimation(on: draft.wrappedValue.isInteractive, category: .modules)
|
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-10 22:24:06 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
// swiftlint: disable force_try
|
|
|
|
#Preview {
|
|
|
|
var builder = OpenVPN.Configuration.Builder(withFallbacks: true)
|
|
|
|
builder.noPullMask = [.proxy]
|
|
|
|
builder.authUserPass = true
|
|
|
|
builder.remotes = [
|
|
|
|
.init(rawValue: "2.2.2.2:UDP:2222")!,
|
|
|
|
.init(rawValue: "6.6.6.6:UDP:6666")!,
|
2024-10-05 08:06:51 +00:00
|
|
|
.init(rawValue: "12.12.12.12:TCP:21212")!,
|
|
|
|
.init(rawValue: "12:12:12:12:20:20:20:20:TCP6:21212")!
|
2024-09-23 13:02:26 +00:00
|
|
|
]
|
|
|
|
builder.ipv4 = IPSettings(subnet: try! .init("5.5.5.5", 24))
|
|
|
|
.including(routes: [
|
|
|
|
.init(defaultWithGateway: .ip("120.1.1.1", .v4)),
|
|
|
|
.init(.init(rawValue: "55.10.20.30/32"), nil)
|
|
|
|
])
|
|
|
|
.excluding(routes: [
|
|
|
|
.init(.init(rawValue: "88.40.30.30/32"), nil),
|
|
|
|
.init(.init(rawValue: "60.60.60.60/32"), .ip("127.0.0.1", .v4))
|
|
|
|
])
|
|
|
|
builder.ipv6 = IPSettings(subnet: try! .init("::5", 24))
|
|
|
|
.including(routes: [
|
|
|
|
.init(defaultWithGateway: .ip("120::1:1:1", .v6)),
|
|
|
|
.init(.init(rawValue: "55:10:20::30/128"), nil),
|
|
|
|
.init(.init(rawValue: "60:60:60::60/128"), .ip("::2", .v6))
|
|
|
|
])
|
|
|
|
.excluding(routes: [
|
|
|
|
.init(.init(rawValue: "88:40:30::30/32"), nil)
|
|
|
|
])
|
|
|
|
builder.routingPolicies = [.IPv4, .IPv6]
|
|
|
|
builder.dnsServers = ["1.2.3.4", "4.5.6.7"]
|
|
|
|
builder.dnsDomain = "domain.com"
|
|
|
|
builder.searchDomains = ["search1.com", "search2.com"]
|
|
|
|
builder.httpProxy = try! .init("10.10.10.10", 1080)
|
|
|
|
builder.httpsProxy = try! .init("10.10.10.10", 8080)
|
|
|
|
builder.proxyAutoConfigurationURL = URL(string: "https://hello.pac")!
|
|
|
|
builder.proxyBypassDomains = ["bypass1.com", "bypass2.com"]
|
|
|
|
builder.xorMethod = .xormask(mask: .init(Data(hex: "1234")))
|
|
|
|
builder.ca = .init(mockPem: "ca-certificate")
|
|
|
|
builder.clientCertificate = .init(mockPem: "client-certificate")
|
|
|
|
builder.clientKey = .init(mockPem: "client-key")
|
|
|
|
builder.tlsWrap = .init(strategy: .auth, key: .init(biData: Data(count: 256)))
|
|
|
|
builder.keepAliveInterval = 10.0
|
|
|
|
builder.renegotiatesAfter = 60.0
|
|
|
|
builder.randomizeEndpoint = true
|
|
|
|
builder.randomizeHostnames = true
|
|
|
|
|
|
|
|
let module = OpenVPNModule.Builder(configurationBuilder: builder)
|
|
|
|
return module.preview(title: "OpenVPN")
|
|
|
|
}
|
|
|
|
// swiftlint: enable force_try
|
|
|
|
|
|
|
|
private extension OpenVPN.CryptoContainer {
|
|
|
|
init(mockPem: String) {
|
|
|
|
self.init(pem: """
|
|
|
|
-----BEGIN CERTIFICATE-----
|
|
|
|
\(mockPem)
|
|
|
|
-----END CERTIFICATE-----
|
|
|
|
""")
|
|
|
|
}
|
|
|
|
}
|