2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// EndpointView+OpenVPN.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/19/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 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/>.
|
|
|
|
//
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
import TunnelKitOpenVPN
|
|
|
|
|
|
|
|
extension EndpointView {
|
|
|
|
struct OpenVPNView: View {
|
2022-08-28 07:19:15 +00:00
|
|
|
@ObservedObject private var providerManager: ProviderManager
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
@ObservedObject private var currentProfile: ObservableProfile
|
|
|
|
|
|
|
|
@Binding private var builder: OpenVPN.ConfigurationBuilder
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var isFirstAppearance = true
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var isAutomatic = false
|
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
@State private var isExpanded: [String: Bool] = [:]
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 10:45:47 +00:00
|
|
|
@State private var isAdding = false
|
|
|
|
|
|
|
|
@State private var editedEndpoint: Endpoint?
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(currentProfile: ObservableProfile) {
|
2022-08-28 07:19:15 +00:00
|
|
|
let providerManager: ProviderManager = .shared
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
self.providerManager = providerManager
|
|
|
|
self.currentProfile = currentProfile
|
|
|
|
|
2023-07-17 19:44:18 +00:00
|
|
|
_builder = currentProfile.builderBinding(providerManager: providerManager)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
|
|
|
ScrollViewReader { scrollProxy in
|
|
|
|
List {
|
|
|
|
mainSection
|
2023-07-23 10:45:47 +00:00
|
|
|
if isConfigurationReadonly {
|
|
|
|
groupedEndpointsSections
|
|
|
|
} else {
|
|
|
|
endpointsSection
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
advancedSection
|
|
|
|
}.onAppear {
|
2023-07-22 15:10:16 +00:00
|
|
|
isAutomatic = (currentProfile.value.customEndpoint == nil)
|
|
|
|
if let customEndpoint = currentProfile.value.customEndpoint {
|
|
|
|
isExpanded[customEndpoint.address] = true
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
scrollToCustomEndpoint(scrollProxy)
|
|
|
|
}.onChange(of: isAutomatic, perform: onToggleAutomatic)
|
2023-07-23 10:45:47 +00:00
|
|
|
.toolbar {
|
|
|
|
if !isConfigurationReadonly {
|
|
|
|
addButton
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}.navigationTitle(L10n.Global.Strings.endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension EndpointView.OpenVPNView {
|
2023-07-23 10:45:47 +00:00
|
|
|
var isConfigurationReadonly: Bool {
|
|
|
|
currentProfile.value.isProvider
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var mainSection: some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
Section {
|
2022-04-23 10:08:24 +00:00
|
|
|
Toggle(L10n.Global.Strings.automatic, isOn: $isAutomatic.themeAnimation())
|
2023-07-22 15:10:16 +00:00
|
|
|
} footer: {
|
|
|
|
themeErrorMessage(isManualEndpointRequired ? L10n.Endpoint.Errors.endpointRequired : nil)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 10:45:47 +00:00
|
|
|
var advancedSection: some View {
|
|
|
|
Section {
|
|
|
|
let caption = L10n.Endpoint.Advanced.title
|
|
|
|
NavigationLink(caption) {
|
|
|
|
EndpointAdvancedView.OpenVPNView(
|
|
|
|
builder: $builder,
|
|
|
|
isReadonly: isConfigurationReadonly,
|
|
|
|
isServerPushed: false
|
|
|
|
).navigationTitle(caption)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isManualEndpointRequired: Bool {
|
|
|
|
!isAutomatic && currentProfile.value.customEndpoint == nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension EndpointView.OpenVPNView {
|
|
|
|
func onToggleAutomatic(_ value: Bool) {
|
|
|
|
guard value else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard currentProfile.value.customEndpoint != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
withAnimation {
|
|
|
|
currentProfile.value.customEndpoint = nil
|
|
|
|
isExpanded.removeAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrollToCustomEndpoint(_ proxy: ScrollViewProxy) {
|
|
|
|
proxy.maybeScrollTo(currentProfile.value.customEndpoint?.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Editable: linear
|
|
|
|
|
|
|
|
private extension EndpointView.OpenVPNView {
|
|
|
|
var endpointsSection: some View {
|
|
|
|
Section {
|
|
|
|
ForEach(builder.remotes ?? []) { endpoint in
|
2023-07-23 13:17:28 +00:00
|
|
|
rowForEndpoint(endpoint)
|
2023-07-23 10:45:47 +00:00
|
|
|
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
|
|
|
|
actions(forEndpoint: endpoint)
|
|
|
|
}
|
|
|
|
}.onMove(perform: moveEndpoints)
|
|
|
|
.disabled(isAutomatic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:17:28 +00:00
|
|
|
func rowForEndpoint(_ endpoint: Endpoint) -> some View {
|
2023-07-23 10:45:47 +00:00
|
|
|
Button {
|
|
|
|
withAnimation {
|
|
|
|
currentProfile.value.customEndpoint = endpoint
|
|
|
|
}
|
|
|
|
} label: {
|
2023-07-23 13:17:28 +00:00
|
|
|
labelForEndpoint(endpoint)
|
2023-07-23 10:45:47 +00:00
|
|
|
}.sheet(item: $editedEndpoint) { endpoint in
|
|
|
|
NavigationView {
|
|
|
|
EndpointView.AddView(L10n.Global.Strings.edit, endpoint: endpoint, onSave: commitEndpoint)
|
|
|
|
}.themeGlobal()
|
|
|
|
}.withTrailingCheckmark(when: currentProfile.value.customEndpoint == endpoint)
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:17:28 +00:00
|
|
|
func labelForEndpoint(_ endpoint: Endpoint) -> some View {
|
|
|
|
VStack {
|
|
|
|
Text(endpoint.address)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
Text(endpoint.proto.rawValue)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 10:45:47 +00:00
|
|
|
@ViewBuilder
|
|
|
|
func actions(forEndpoint endpoint: Endpoint) -> some View {
|
|
|
|
if !isConfigurationReadonly {
|
|
|
|
if (builder.remotes?.count ?? 0) > 1 {
|
|
|
|
removeButton(forEndpoint: endpoint)
|
|
|
|
}
|
|
|
|
editButton(forEndpoint: endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addButton: some View {
|
|
|
|
Button {
|
|
|
|
isAdding = true
|
|
|
|
} label: {
|
|
|
|
themeAddMenuImage.asSystemImage
|
|
|
|
}.sheet(isPresented: $isAdding) {
|
|
|
|
NavigationView {
|
|
|
|
EndpointView.AddView(L10n.Global.Strings.add, onSave: commitEndpoint)
|
|
|
|
}.themeGlobal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeButton(forEndpoint endpoint: Endpoint) -> some View {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
deleteEndpoint(endpoint)
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.delete)
|
|
|
|
}.themeDestructiveTintStyle()
|
|
|
|
}
|
|
|
|
|
|
|
|
func editButton(forEndpoint endpoint: Endpoint) -> some View {
|
|
|
|
Button {
|
|
|
|
editedEndpoint = endpoint
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.edit)
|
|
|
|
}.themePrimaryTintStyle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension EndpointView.OpenVPNView {
|
|
|
|
func commitEndpoint(_ newEndpoint: Endpoint, editedEndpoint: Endpoint?) {
|
|
|
|
withAnimation {
|
|
|
|
|
|
|
|
// replace existing
|
|
|
|
if let editedEndpoint,
|
|
|
|
let editedIndex = builder.remotes?.firstIndex(where: { $0 == editedEndpoint }) {
|
|
|
|
|
|
|
|
builder.remotes?[editedIndex] = newEndpoint
|
|
|
|
if currentProfile.value.customEndpoint == editedEndpoint {
|
|
|
|
currentProfile.value.customEndpoint = newEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add new
|
|
|
|
else {
|
|
|
|
if builder.remotes != nil {
|
|
|
|
builder.remotes?.append(newEndpoint)
|
|
|
|
} else {
|
|
|
|
assertionFailure("Nil remotes, how did we get here?")
|
|
|
|
builder.remotes = [newEndpoint]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveEndpoints(fromOffsets: IndexSet, toOffset: Int) {
|
|
|
|
builder.remotes?.move(fromOffsets: fromOffsets, toOffset: toOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteEndpoint(_ endpoint: Endpoint) {
|
|
|
|
withAnimation {
|
|
|
|
builder.remotes?.removeAll {
|
|
|
|
$0 == endpoint
|
|
|
|
}
|
|
|
|
if currentProfile.value.customEndpoint == endpoint {
|
|
|
|
currentProfile.value.customEndpoint = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Non-editable: group by address
|
|
|
|
|
|
|
|
private extension EndpointView.OpenVPNView {
|
|
|
|
var groupedEndpointsSections: some View {
|
2023-07-23 13:17:28 +00:00
|
|
|
ForEach(endpointsByAddress, content: group(forEndpointsSection:))
|
2023-07-22 15:10:16 +00:00
|
|
|
.disabled(isAutomatic)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:17:28 +00:00
|
|
|
func group(forEndpointsSection section: EndpointsByAddress) -> some View {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2023-07-22 15:10:16 +00:00
|
|
|
DisclosureGroup(isExpanded: isExpandedBinding(address: section.address)) {
|
2023-07-23 13:17:28 +00:00
|
|
|
ForEach(section.endpoints, content: rowForEndpointProtocol)
|
2023-07-22 15:10:16 +00:00
|
|
|
} label: {
|
2023-07-23 13:17:28 +00:00
|
|
|
labelForEndpointAddress(section.address)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 13:17:28 +00:00
|
|
|
func labelForEndpointAddress(_ address: String) -> some View {
|
|
|
|
Text(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rowForEndpointProtocol(_ endpoint: Endpoint) -> some View {
|
2023-07-22 15:10:16 +00:00
|
|
|
Button {
|
|
|
|
withAnimation {
|
|
|
|
currentProfile.value.customEndpoint = endpoint
|
|
|
|
}
|
|
|
|
} label: {
|
2023-07-23 13:17:28 +00:00
|
|
|
labelForEndpointProtocol(endpoint.proto)
|
2023-07-22 15:10:16 +00:00
|
|
|
}.withTrailingCheckmark(when: currentProfile.value.customEndpoint == endpoint)
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:17:28 +00:00
|
|
|
func labelForEndpointProtocol(_ proto: EndpointProtocol) -> some View {
|
|
|
|
Text(proto.rawValue)
|
|
|
|
}
|
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
var endpointsByAddress: [EndpointsByAddress] {
|
|
|
|
guard let remotes = builder.remotes, !remotes.isEmpty else {
|
2023-07-03 14:54:43 +00:00
|
|
|
return []
|
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
var uniqueAddresses: [String] = []
|
|
|
|
remotes.forEach {
|
|
|
|
guard !uniqueAddresses.contains($0.address) else {
|
2023-07-03 14:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
uniqueAddresses.append($0.address)
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
return uniqueAddresses.map {
|
|
|
|
EndpointsByAddress(address: $0, remotes: remotes)
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
private struct EndpointsByAddress: Identifiable {
|
|
|
|
let address: String
|
2023-07-03 14:54:43 +00:00
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
let endpoints: [Endpoint]
|
|
|
|
|
|
|
|
init(address: String, remotes: [Endpoint]?) {
|
|
|
|
self.address = address
|
|
|
|
endpoints = remotes?.filter {
|
|
|
|
$0.address == address
|
|
|
|
}.sorted() ?? []
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
// MARK: Identifiable
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
address
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-17 19:44:18 +00:00
|
|
|
// MARK: - Bindings
|
|
|
|
|
|
|
|
private extension ObservableProfile {
|
2023-09-10 18:36:52 +00:00
|
|
|
|
|
|
|
@MainActor
|
2023-07-17 19:44:18 +00:00
|
|
|
func builderBinding(providerManager: ProviderManager) -> Binding<OpenVPN.ConfigurationBuilder> {
|
|
|
|
.init {
|
|
|
|
if self.value.isProvider {
|
|
|
|
guard let server = self.value.providerServer(providerManager) else {
|
|
|
|
assertionFailure("Server not found")
|
|
|
|
return .init()
|
|
|
|
}
|
|
|
|
guard let preset = self.value.providerPreset(server) else {
|
|
|
|
assertionFailure("Preset not found")
|
|
|
|
return .init()
|
|
|
|
}
|
|
|
|
guard let cfg = preset.openVPNConfiguration else {
|
|
|
|
assertionFailure("Preset \(preset.id) (\(preset.name)) has no OpenVPN configuration")
|
|
|
|
return .init()
|
|
|
|
}
|
|
|
|
var builder = cfg.builder(withFallbacks: true)
|
|
|
|
try? builder.setRemotes(from: preset, with: server, excludingHostname: false)
|
|
|
|
return builder
|
|
|
|
} else if let cfg = self.value.hostOpenVPNSettings?.configuration {
|
|
|
|
let builder = cfg.builder(withFallbacks: true)
|
|
|
|
// pp_log.debug("Loading OpenVPN configuration: \(builder)")
|
|
|
|
return builder
|
|
|
|
}
|
|
|
|
// fall back gracefully
|
|
|
|
return .init()
|
|
|
|
} set: {
|
|
|
|
if self.value.isProvider {
|
|
|
|
// readonly
|
|
|
|
} else {
|
|
|
|
pp_log.debug("Saving OpenVPN configuration: \($0)")
|
|
|
|
self.value.hostOpenVPNSettings?.configuration = $0.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
}
|
2023-07-17 19:44:18 +00:00
|
|
|
|
2023-07-22 15:10:16 +00:00
|
|
|
private extension EndpointView.OpenVPNView {
|
|
|
|
func isExpandedBinding(address: String) -> Binding<Bool> {
|
2023-07-17 19:44:18 +00:00
|
|
|
.init {
|
2023-07-22 15:10:16 +00:00
|
|
|
isExpanded[address] ?? false
|
|
|
|
} set: {
|
|
|
|
isExpanded[address] = $0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Profile {
|
|
|
|
var customEndpoint: Endpoint? {
|
|
|
|
get {
|
|
|
|
if isProvider {
|
|
|
|
return providerCustomEndpoint
|
2023-07-17 19:44:18 +00:00
|
|
|
} else {
|
2023-07-22 15:10:16 +00:00
|
|
|
return hostOpenVPNSettings?.customEndpoint
|
2023-07-17 19:44:18 +00:00
|
|
|
}
|
2023-07-22 15:10:16 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
if isProvider {
|
|
|
|
providerCustomEndpoint = newValue
|
2023-07-17 19:44:18 +00:00
|
|
|
} else {
|
2023-07-22 15:10:16 +00:00
|
|
|
hostOpenVPNSettings?.customEndpoint = newValue
|
2023-07-17 19:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|