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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2022-04-12 13:09:14 +00:00
|
|
|
import TunnelKitOpenVPN
|
|
|
|
|
|
|
|
extension EndpointView {
|
|
|
|
struct OpenVPNView: View {
|
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
|
|
|
|
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
|
|
|
@Binding private var customEndpoint: Endpoint?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var isConfigurationReadonly: Bool {
|
|
|
|
currentProfile.value.isProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@State private var selectedSocketType: SocketType = .udp
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var selectedPort: UInt16 = 0
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// XXX: do not escape mutating 'self', use constant providerManager
|
|
|
|
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
|
|
|
|
|
|
|
|
_builder = .init {
|
|
|
|
if currentProfile.value.isProvider {
|
|
|
|
guard let server = currentProfile.value.providerServer(providerManager) else {
|
|
|
|
assertionFailure("Server not found")
|
|
|
|
return .init()
|
|
|
|
}
|
|
|
|
guard let preset = currentProfile.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 = currentProfile.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 currentProfile.value.isProvider {
|
|
|
|
// readonly
|
|
|
|
} else {
|
|
|
|
pp_log.debug("Saving OpenVPN configuration: \($0)")
|
|
|
|
currentProfile.value.hostOpenVPNSettings?.configuration = $0.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_customEndpoint = .init {
|
|
|
|
if currentProfile.value.isProvider {
|
|
|
|
return currentProfile.value.providerCustomEndpoint()
|
|
|
|
} else {
|
|
|
|
return currentProfile.value.hostOpenVPNSettings?.customEndpoint
|
|
|
|
}
|
|
|
|
} set: {
|
|
|
|
if currentProfile.value.isProvider {
|
|
|
|
currentProfile.value.setProviderCustomEndpoint($0)
|
|
|
|
} else {
|
|
|
|
currentProfile.value.hostOpenVPNSettings?.customEndpoint = $0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
if !isAutomatic {
|
|
|
|
filtersSection
|
|
|
|
addressesSection
|
|
|
|
}
|
|
|
|
advancedSection
|
|
|
|
}.onAppear {
|
|
|
|
scrollToCustomEndpoint(scrollProxy)
|
|
|
|
preselectFilters(once: true)
|
|
|
|
}.onChange(of: isAutomatic, perform: onToggleAutomatic)
|
|
|
|
.onChange(of: selectedSocketType, perform: preselectPort)
|
|
|
|
.onChange(of: customEndpoint) { _ in
|
|
|
|
withAnimation {
|
|
|
|
preselectFilters(once: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.navigationTitle(L10n.Global.Strings.endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EndpointView.OpenVPNView {
|
|
|
|
private var mainSection: some View {
|
|
|
|
Section {
|
2022-04-23 10:08:24 +00:00
|
|
|
Toggle(L10n.Global.Strings.automatic, isOn: $isAutomatic.themeAnimation())
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var filtersSection: some View {
|
|
|
|
Section {
|
|
|
|
themeTextPicker(
|
|
|
|
L10n.Global.Strings.protocol,
|
|
|
|
selection: $selectedSocketType,
|
2022-04-13 09:10:33 +00:00
|
|
|
values: availableSocketTypes,
|
2022-04-12 13:09:14 +00:00
|
|
|
description: \.rawValue
|
|
|
|
)
|
|
|
|
themeTextPicker(
|
|
|
|
L10n.Global.Strings.port,
|
|
|
|
selection: $selectedPort,
|
|
|
|
values: allPorts(forSocketType: selectedSocketType),
|
|
|
|
description: \.description
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var addressesSection: some View {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-12 13:09:14 +00:00
|
|
|
filteredRemotes.map {
|
|
|
|
ForEach($0, content: button(forEndpoint:))
|
|
|
|
}
|
2022-05-01 17:43:33 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.Global.Strings.addresses)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var advancedSection: some View {
|
|
|
|
Section {
|
|
|
|
let caption = L10n.Endpoint.Advanced.title
|
|
|
|
NavigationLink(caption) {
|
|
|
|
EndpointAdvancedView.OpenVPNView(
|
|
|
|
builder: $builder,
|
|
|
|
isReadonly: isConfigurationReadonly,
|
|
|
|
isServerPushed: false
|
|
|
|
).navigationTitle(caption)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func button(forEndpoint endpoint: Endpoint?) -> some View {
|
|
|
|
Button {
|
|
|
|
customEndpoint = endpoint
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
} label: {
|
|
|
|
text(forEndpoint: endpoint)
|
|
|
|
}.withTrailingCheckmark(when: customEndpoint == endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func text(forEndpoint endpoint: Endpoint?) -> some View {
|
|
|
|
Text(endpoint?.address ?? L10n.Global.Strings.automatic)
|
2022-04-23 09:42:26 +00:00
|
|
|
.themeLongTextStyle()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EndpointView.OpenVPNView {
|
|
|
|
private func onToggleAutomatic(_ value: Bool) {
|
|
|
|
if value {
|
|
|
|
guard customEndpoint != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
customEndpoint = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func preselectFilters(once: Bool) {
|
|
|
|
guard !once || isFirstAppearance else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isFirstAppearance = false
|
|
|
|
|
|
|
|
if let customEndpoint = customEndpoint {
|
|
|
|
isAutomatic = false
|
|
|
|
selectedSocketType = customEndpoint.proto.socketType
|
|
|
|
selectedPort = customEndpoint.proto.port
|
|
|
|
} else {
|
|
|
|
isAutomatic = true
|
2022-04-13 09:10:33 +00:00
|
|
|
guard let socketType = availableSocketTypes.first else {
|
2022-04-12 13:09:14 +00:00
|
|
|
assertionFailure("No socket types, empty remotes?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
selectedSocketType = socketType
|
|
|
|
preselectPort(forSocketType: socketType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func preselectPort(forSocketType socketType: SocketType) {
|
|
|
|
let supported = allPorts(forSocketType: socketType)
|
|
|
|
guard !supported.contains(selectedPort) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let port = supported.first else {
|
|
|
|
assertionFailure("No ports, empty remotes?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
selectedPort = port
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:10:33 +00:00
|
|
|
private var availableSocketTypes: [SocketType] {
|
2022-04-12 13:09:14 +00:00
|
|
|
guard let remotes = builder.remotes else {
|
|
|
|
return []
|
|
|
|
}
|
2022-04-13 09:10:33 +00:00
|
|
|
let allTypes: [SocketType] = [
|
|
|
|
SocketType.udp,
|
|
|
|
SocketType.tcp,
|
|
|
|
SocketType.udp4,
|
|
|
|
SocketType.tcp4
|
|
|
|
]
|
|
|
|
var availableTypes: [SocketType] = []
|
|
|
|
allTypes.forEach { socketType in
|
2022-04-12 13:09:14 +00:00
|
|
|
guard remotes.contains(where: {
|
|
|
|
$0.proto.socketType == socketType
|
|
|
|
}) else {
|
|
|
|
return
|
|
|
|
}
|
2022-04-13 09:10:33 +00:00
|
|
|
availableTypes.append(socketType)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-13 09:10:33 +00:00
|
|
|
return availableTypes
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func allPorts(forSocketType socketType: SocketType) -> [UInt16] {
|
|
|
|
guard let remotes = builder.remotes else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let allPorts = Set(remotes.filter {
|
|
|
|
$0.proto.socketType == socketType
|
|
|
|
}.map(\.proto.port))
|
|
|
|
return Array(allPorts).sorted()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var filteredRemotes: [Endpoint]? {
|
2022-09-04 18:09:31 +00:00
|
|
|
builder.remotes?.filter {
|
2022-04-12 13:09:14 +00:00
|
|
|
$0.proto.socketType == selectedSocketType && $0.proto.port == selectedPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EndpointView.OpenVPNView {
|
|
|
|
private func scrollToCustomEndpoint(_ proxy: ScrollViewProxy) {
|
|
|
|
proxy.maybeScrollTo(customEndpoint?.id)
|
|
|
|
}
|
|
|
|
}
|