passepartout-apple/Passepartout/App/Views/EndpointAdvancedView+WireGu...

106 lines
3.1 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// EndpointAdvancedView+WireGuard.swift
// Passepartout
//
// Created by Davide De Rosa on 3/8/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
import TunnelKitWireGuard
extension EndpointAdvancedView {
struct WireGuardView: View {
@Binding var builder: WireGuard.ConfigurationBuilder
let isReadonly: Bool
var body: some View {
List {
let cfg = builder.build()
keySection
addressesSection
dnsSection(configuration: cfg)
mtuSection
}
}
}
}
extension EndpointAdvancedView.WireGuardView {
private var keySection: some View {
Section {
2022-04-12 13:09:14 +00:00
themeLongContentLink(L10n.Global.Strings.privateKey, content: .constant(builder.privateKey))
themeLongContentLink(L10n.Global.Strings.publicKey, content: .constant(builder.publicKey))
} header: {
Text(L10n.Global.Strings.interface)
2022-04-12 13:09:14 +00:00
}
}
private var addressesSection: some View {
Section {
2022-04-12 13:09:14 +00:00
ForEach(builder.addresses, id: \.self, content: Text.init)
} header: {
Text(L10n.Global.Strings.addresses)
2022-04-12 13:09:14 +00:00
}
}
private func dnsSection(configuration: WireGuard.Configuration) -> some View {
configuration.dnsSettings.map { settings in
Section {
2022-04-12 13:09:14 +00:00
ForEach(settings.servers, id: \.self) {
Text(L10n.Global.Strings.address)
.withTrailingText($0)
}
ForEach(settings.domains, id: \.self) {
Text(L10n.Global.Strings.domain)
.withTrailingText($0)
}
} header: {
Text(Unlocalized.Network.dns)
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 mtuSection: some View {
builder.mtu.map { mtu in
Section {
Text(Unlocalized.Network.mtu)
.withTrailingText(Int(mtu).localizedDescriptionAsMTU)
}
}
}
}
2023-03-19 13:41:53 +00:00
private extension WireGuard.Configuration {
struct DNSOptions {
let servers: [String]
let domains: [String]
}
var dnsSettings: DNSOptions? {
2022-04-12 13:09:14 +00:00
guard !dnsServers.isEmpty || !dnsSearchDomains.isEmpty else {
return nil
}
2023-03-19 13:41:53 +00:00
return .init(servers: dnsServers, domains: dnsSearchDomains)
2022-04-12 13:09:14 +00:00
}
}