Prepare WireGuard for provider selector (#890)

- Omit configuration on creation to show provider selector
- Take out ConfigurationView like OpenVPN
This commit is contained in:
Davide 2024-11-18 17:49:47 +01:00 committed by GitHub
parent 89d7af4df7
commit 1536551922
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 203 additions and 84 deletions

View File

@ -40,7 +40,8 @@ public final class AppUIMain: UILibraryConfiguring {
private extension AppUIMain { private extension AppUIMain {
func assertMissingImplementations(with registry: Registry) { func assertMissingImplementations(with registry: Registry) {
let providerModuleTypes: Set<ModuleType> = [ let providerModuleTypes: Set<ModuleType> = [
.openVPN .openVPN,
.wireGuard
] ]
ModuleType.allCases.forEach { moduleType in ModuleType.allCases.forEach { moduleType in
do { do {

View File

@ -23,6 +23,7 @@
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>. // along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
// //
import CommonUtils
import PassepartoutKit import PassepartoutKit
import SwiftUI import SwiftUI
@ -31,3 +32,13 @@ extension WireGuardModule.Builder: ModuleViewProviding {
WireGuardView(editor: editor, module: self, impl: impl as? WireGuardModule.Implementation) WireGuardView(editor: editor, module: self, impl: impl as? WireGuardModule.Implementation)
} }
} }
extension WireGuardModule: ProviderEntityViewProviding {
func providerEntityView(
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
) -> some View {
vpnProviderEntityView(with: provider, errorHandler: errorHandler, onSelect: onSelect)
}
}

View File

@ -0,0 +1,114 @@
//
// WireGuardView+Configuration.swift
// Passepartout
//
// Created by Davide De Rosa on 10/28/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/>.
//
import PassepartoutKit
import SwiftUI
extension WireGuardView {
struct ConfigurationView: View {
let configuration: WireGuard.Configuration.Builder
var body: some View {
moduleSection(for: interfaceRows, header: Strings.Modules.Wireguard.interface)
moduleSection(for: dnsRows, header: Strings.Unlocalized.dns)
ForEach(Array(zip(configuration.peers.indices, configuration.peers)), id: \.1.publicKey) { index, peer in
moduleSection(for: peersRows(for: peer), header: Strings.Modules.Wireguard.peer(index + 1))
}
}
}
}
private extension WireGuardView.ConfigurationView {
var interfaceRows: [ModuleRow]? {
var rows: [ModuleRow] = []
rows.append(.longContent(caption: Strings.Global.privateKey, value: configuration.interface.privateKey))
configuration.interface.addresses
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Global.addresses,
values: $0
))
}
configuration.interface.mtu.map {
rows.append(.text(caption: Strings.Unlocalized.mtu, value: $0.description))
}
return rows.nilIfEmpty
}
var dnsRows: [ModuleRow]? {
var rows: [ModuleRow] = []
configuration.interface.dns.servers
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Global.servers,
values: $0
))
}
configuration.interface.dns.domainName.map {
rows.append(.text(
caption: Strings.Global.domain,
value: $0
))
}
configuration.interface.dns.searchDomains?
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Entities.Dns.searchDomains,
values: $0
))
}
return rows.nilIfEmpty
}
func peersRows(for peer: WireGuard.RemoteInterface.Builder) -> [ModuleRow]? {
var rows: [ModuleRow] = []
rows.append(.longContent(caption: Strings.Global.publicKey, value: peer.publicKey))
peer.preSharedKey.map {
rows.append(.longContent(caption: Strings.Modules.Wireguard.presharedKey, value: $0))
}
peer.endpoint.map {
rows.append(.copiableText(caption: Strings.Global.endpoint, value: $0))
}
peer.allowedIPs
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Modules.Wireguard.allowedIps,
values: $0
))
}
peer.keepAlive.map {
rows.append(.text(caption: Strings.Global.keepAlive, value: TimeInterval($0).localizedDescription(style: .timeString)))
}
return rows.nilIfEmpty
}
}

View File

@ -24,11 +24,15 @@
// //
import CommonLibrary import CommonLibrary
import CommonUtils
import PassepartoutKit import PassepartoutKit
import SwiftUI import SwiftUI
struct WireGuardView: View, ModuleDraftEditing { struct WireGuardView: View, ModuleDraftEditing {
@Environment(\.navigationPath)
private var path
@ObservedObject @ObservedObject
var editor: ProfileEditor var editor: ProfileEditor
@ -36,113 +40,103 @@ struct WireGuardView: View, ModuleDraftEditing {
let impl: WireGuardModule.Implementation? let impl: WireGuardModule.Implementation?
@State
private var paywallReason: PaywallReason?
@State
private var errorHandler: ErrorHandler = .default()
var body: some View { var body: some View {
contentView contentView
.moduleView(editor: editor, draft: draft.wrappedValue) .moduleView(editor: editor, draft: draft.wrappedValue)
.modifier(PaywallModifier(reason: $paywallReason))
.navigationDestination(for: Subroute.self, destination: destination)
.themeAnimation(on: providerId.wrappedValue, category: .modules)
.withErrorHandler(errorHandler)
} }
} }
// MARK: - Content // MARK: - Content
private extension WireGuardView { private extension WireGuardView {
var configuration: WireGuard.Configuration.Builder {
guard let impl else {
fatalError("Requires WireGuardModule implementation")
}
return draft.wrappedValue.configurationBuilder ?? .init(keyGenerator: impl.keyGenerator)
}
@ViewBuilder @ViewBuilder
var contentView: some View { var contentView: some View {
moduleSection(for: interfaceRows, header: Strings.Modules.Wireguard.interface) if let configuration = draft.wrappedValue.configurationBuilder {
moduleSection(for: dnsRows, header: Strings.Unlocalized.dns) ConfigurationView(configuration: configuration)
ForEach(Array(zip(configuration.peers.indices, configuration.peers)), id: \.1.publicKey) { index, peer in } else {
moduleSection(for: peersRows(for: peer), header: Strings.Modules.Wireguard.peer(index + 1)) EmptyView()
.modifier(providerModifier)
} }
} }
}
// MARK: - Subviews var providerModifier: some ViewModifier {
VPNProviderContentModifier(
private extension WireGuardView { providerId: providerId,
var interfaceRows: [ModuleRow]? { selectedEntity: providerEntity,
var rows: [ModuleRow] = [] paywallReason: $paywallReason,
rows.append(.longContent(caption: Strings.Global.privateKey, value: configuration.interface.privateKey)) entityDestination: Subroute.providerServer,
configuration.interface.addresses providerRows: {
.nilIfEmpty moduleGroup(for: providerKeyRows)
.map {
rows.append(.textList(
caption: Strings.Global.addresses,
values: $0
))
} }
configuration.interface.mtu.map { )
rows.append(.text(caption: Strings.Unlocalized.mtu, value: $0.description))
}
return rows.nilIfEmpty
} }
var dnsRows: [ModuleRow]? { var providerId: Binding<ProviderID?> {
var rows: [ModuleRow] = [] editor.binding(forProviderOf: module.id)
configuration.interface.dns.servers
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Global.servers,
values: $0
))
}
configuration.interface.dns.domainName.map {
rows.append(.text(
caption: Strings.Global.domain,
value: $0
))
}
configuration.interface.dns.searchDomains?
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Entities.Dns.searchDomains,
values: $0
))
}
return rows.nilIfEmpty
} }
func peersRows(for peer: WireGuard.RemoteInterface.Builder) -> [ModuleRow]? { var providerEntity: Binding<VPNEntity<WireGuard.Configuration>?> {
var rows: [ModuleRow] = [] editor.binding(forProviderEntityOf: module.id)
rows.append(.longContent(caption: Strings.Global.publicKey, value: peer.publicKey)) }
peer.preSharedKey.map {
rows.append(.longContent(caption: Strings.Modules.Wireguard.presharedKey, value: $0)) var providerKeyRows: [ModuleRow]? {
} [.push(caption: Strings.Modules.Wireguard.providerKey, route: HashableRoute(Subroute.providerKey))]
peer.endpoint.map {
rows.append(.copiableText(caption: Strings.Global.endpoint, value: $0))
}
peer.allowedIPs
.nilIfEmpty
.map {
rows.append(.textList(
caption: Strings.Modules.Wireguard.allowedIps,
values: $0
))
}
peer.keepAlive.map {
rows.append(.text(caption: Strings.Global.keepAlive, value: TimeInterval($0).localizedDescription(style: .timeString)))
}
return rows.nilIfEmpty
} }
} }
private extension WireGuardView { private extension WireGuardView {
func onSelectServer(server: VPNServer, preset: VPNPreset<WireGuard.Configuration>) {
providerEntity.wrappedValue = VPNEntity(server: server, preset: preset)
path.wrappedValue.removeLast()
}
func importConfiguration(from url: URL) { func importConfiguration(from url: URL) {
// TODO: #657, import draft from external URL // TODO: #657, import draft from external URL
} }
} }
// MARK: - Destinations
private extension WireGuardView {
enum Subroute: Hashable {
case providerServer
case providerKey
}
@ViewBuilder
func destination(for route: Subroute) -> some View {
switch route {
case .providerServer:
providerId.wrappedValue.map {
VPNProviderServerView(
moduleId: module.id,
providerId: $0,
configurationType: WireGuard.Configuration.self,
selectedEntity: providerEntity.wrappedValue,
filtersWithSelection: true,
onSelect: onSelectServer
)
}
case .providerKey:
// TODO: #339, WireGuard upload public key to provider
EmptyView()
}
}
}
// MARK: - Previews // MARK: - Previews
// swiftlint: disable force_try // swiftlint: disable force_try

View File

@ -33,11 +33,7 @@ extension ModuleType {
return OpenVPNModule.Builder() return OpenVPNModule.Builder()
case .wireGuard: case .wireGuard:
let impl = registry.implementation(for: WireGuardModule.moduleHandler.id) return WireGuardModule.Builder()
guard let wireGuard = impl as? WireGuardModule.Implementation else {
fatalError("Missing WireGuardModule implementation from Registry?")
}
return WireGuardModule.Builder(configurationBuilder: .init(keyGenerator: wireGuard.keyGenerator))
case .dns: case .dns:
return DNSModule.Builder() return DNSModule.Builder()

View File

@ -510,6 +510,8 @@ public enum Strings {
} }
/// Pre-shared key /// Pre-shared key
public static let presharedKey = Strings.tr("Localizable", "modules.wireguard.preshared_key", fallback: "Pre-shared key") public static let presharedKey = Strings.tr("Localizable", "modules.wireguard.preshared_key", fallback: "Pre-shared key")
/// Private key
public static let providerKey = Strings.tr("Localizable", "modules.wireguard.provider_key", fallback: "Private key")
} }
} }
public enum Paywall { public enum Paywall {

View File

@ -233,6 +233,7 @@
"modules.openvpn.credentials.otp_method.approach.append" = "The OTP will be appended to the password."; "modules.openvpn.credentials.otp_method.approach.append" = "The OTP will be appended to the password.";
"modules.openvpn.credentials.otp_method.approach.encode" = "The OTP will be encoded in Base64 with the password."; "modules.openvpn.credentials.otp_method.approach.encode" = "The OTP will be encoded in Base64 with the password.";
"modules.wireguard.provider_key" = "Private key";
"modules.wireguard.interface" = "Interface"; "modules.wireguard.interface" = "Interface";
"modules.wireguard.peer" = "Peer #%d"; "modules.wireguard.peer" = "Peer #%d";
"modules.wireguard.preshared_key" = "Pre-shared key"; "modules.wireguard.preshared_key" = "Pre-shared key";