2021-12-01 12:54:00 +00:00
|
|
|
//
|
|
|
|
// Configuration.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/23/21.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2021-12-01 12:54:00 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit 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.
|
|
|
|
//
|
|
|
|
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WireGuardKit
|
2022-02-11 17:45:40 +00:00
|
|
|
import NetworkExtension
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public protocol WireGuardConfigurationProviding {
|
|
|
|
var interface: InterfaceConfiguration { get }
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
var peers: [PeerConfiguration] { get }
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
var privateKey: String { get }
|
|
|
|
|
|
|
|
var publicKey: String { get }
|
|
|
|
|
|
|
|
var addresses: [String] { get }
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
var dnsServers: [String] { get }
|
|
|
|
|
|
|
|
var dnsSearchDomains: [String] { get }
|
|
|
|
|
2023-03-19 07:22:30 +00:00
|
|
|
var dnsHTTPSURL: URL? { get }
|
|
|
|
|
|
|
|
var dnsTLSServerName: String? { get }
|
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
var mtu: UInt16? { get }
|
|
|
|
|
|
|
|
var peersCount: Int { get }
|
|
|
|
|
|
|
|
func publicKey(ofPeer peerIndex: Int) -> String
|
|
|
|
|
|
|
|
func preSharedKey(ofPeer peerIndex: Int) -> String?
|
|
|
|
|
|
|
|
func endpoint(ofPeer peerIndex: Int) -> String?
|
|
|
|
|
|
|
|
func allowedIPs(ofPeer peerIndex: Int) -> [String]
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
func keepAlive(ofPeer peerIndex: Int) -> UInt16?
|
|
|
|
}
|
|
|
|
|
|
|
|
extension WireGuard {
|
|
|
|
public struct ConfigurationBuilder: WireGuardConfigurationProviding {
|
|
|
|
private static let defaultGateway4 = IPAddressRange(from: "0.0.0.0/0")!
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
private static let defaultGateway6 = IPAddressRange(from: "::/0")!
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public private(set) var interface: InterfaceConfiguration
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public private(set) var peers: [PeerConfiguration]
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public init() {
|
|
|
|
self.init(PrivateKey())
|
2022-02-11 17:45:40 +00:00
|
|
|
}
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public init(_ base64PrivateKey: String) throws {
|
|
|
|
guard let privateKey = PrivateKey(base64Key: base64PrivateKey) else {
|
2023-04-02 21:42:05 +00:00
|
|
|
throw WireGuard.ConfigurationError.interfaceHasInvalidPrivateKey(base64PrivateKey)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
self.init(privateKey)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
private init(_ privateKey: PrivateKey) {
|
|
|
|
interface = InterfaceConfiguration(privateKey: privateKey)
|
|
|
|
peers = []
|
2022-02-11 17:45:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public init(_ tunnelConfiguration: TunnelConfiguration) {
|
|
|
|
interface = tunnelConfiguration.interface
|
|
|
|
peers = tunnelConfiguration.peers
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
// MARK: WireGuardConfigurationProviding
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var privateKey: String {
|
|
|
|
get {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.privateKey.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let key = PrivateKey(base64Key: newValue) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
interface.privateKey = key
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 17:45:40 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var addresses: [String] {
|
|
|
|
get {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.addresses.map(\.stringRepresentation)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.addresses = newValue.compactMap(IPAddressRange.init)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var dnsServers: [String] {
|
|
|
|
get {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.dns.map(\.stringRepresentation)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.dns = newValue.compactMap(DNSServer.init)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var dnsSearchDomains: [String] {
|
|
|
|
get {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.dnsSearch
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.dnsSearch = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:22:30 +00:00
|
|
|
public var dnsHTTPSURL: URL? {
|
|
|
|
get {
|
|
|
|
interface.dnsHTTPSURL
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.dnsHTTPSURL = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var dnsTLSServerName: String? {
|
|
|
|
get {
|
|
|
|
interface.dnsTLSServerName
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.dnsTLSServerName = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var mtu: UInt16? {
|
|
|
|
get {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.mtu
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
interface.mtu = newValue
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
// MARK: Modification
|
|
|
|
|
|
|
|
public mutating func addPeer(_ base64PublicKey: String, endpoint: String, allowedIPs: [String] = []) throws {
|
|
|
|
guard let publicKey = PublicKey(base64Key: base64PublicKey) else {
|
2023-04-02 21:42:05 +00:00
|
|
|
throw WireGuard.ConfigurationError.peerHasInvalidPublicKey(base64PublicKey)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
var peer = PeerConfiguration(publicKey: publicKey)
|
|
|
|
peer.endpoint = Endpoint(from: endpoint)
|
|
|
|
peer.allowedIPs = allowedIPs.compactMap(IPAddressRange.init)
|
|
|
|
peers.append(peer)
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public mutating func setPreSharedKey(_ base64Key: String, ofPeer peerIndex: Int) throws {
|
|
|
|
guard let preSharedKey = PreSharedKey(base64Key: base64Key) else {
|
2023-04-02 21:42:05 +00:00
|
|
|
throw WireGuard.ConfigurationError.peerHasInvalidPreSharedKey(base64Key)
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
2022-03-12 09:09:40 +00:00
|
|
|
peers[peerIndex].preSharedKey = preSharedKey
|
|
|
|
}
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public mutating func addDefaultGatewayIPv4(toPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].allowedIPs.append(Self.defaultGateway4)
|
|
|
|
}
|
|
|
|
|
|
|
|
public mutating func addDefaultGatewayIPv6(toPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].allowedIPs.append(Self.defaultGateway6)
|
|
|
|
}
|
|
|
|
|
|
|
|
public mutating func removeDefaultGatewayIPv4(fromPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].allowedIPs.removeAll {
|
|
|
|
$0 == Self.defaultGateway4
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public mutating func removeDefaultGatewayIPv6(fromPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].allowedIPs.removeAll {
|
|
|
|
$0 == Self.defaultGateway6
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-09-14 16:42:13 +00:00
|
|
|
public mutating func removeDefaultGateways(fromPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].allowedIPs.removeAll {
|
|
|
|
$0 == Self.defaultGateway4 || $0 == Self.defaultGateway6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public mutating func removeAllDefaultGateways() {
|
|
|
|
peers.indices.forEach {
|
|
|
|
removeDefaultGateways(fromPeer: $0)
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 17:45:40 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public mutating func addAllowedIP(_ allowedIP: String, toPeer peerIndex: Int) {
|
|
|
|
guard let addr = IPAddressRange(from: allowedIP) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
peers[peerIndex].allowedIPs.append(addr)
|
|
|
|
}
|
2022-02-11 17:45:40 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public mutating func removeAllowedIP(_ allowedIP: String, fromPeer peerIndex: Int) {
|
|
|
|
guard let addr = IPAddressRange(from: allowedIP) else {
|
|
|
|
return
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
2022-03-12 09:09:40 +00:00
|
|
|
peers[peerIndex].allowedIPs.removeAll {
|
|
|
|
$0 == addr
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2021-12-01 12:54:00 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public mutating func setKeepAlive(_ keepAlive: UInt16, forPeer peerIndex: Int) {
|
|
|
|
peers[peerIndex].persistentKeepAlive = keepAlive
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public func build() -> Configuration {
|
|
|
|
let tunnelConfiguration = TunnelConfiguration(name: nil, interface: interface, peers: peers)
|
2021-12-01 12:54:00 +00:00
|
|
|
return Configuration(tunnelConfiguration: tunnelConfiguration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 06:14:57 +00:00
|
|
|
public struct Configuration: Codable, Equatable, WireGuardConfigurationProviding {
|
2021-12-01 12:54:00 +00:00
|
|
|
public let tunnelConfiguration: TunnelConfiguration
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var interface: InterfaceConfiguration {
|
2022-11-04 22:04:47 +00:00
|
|
|
tunnelConfiguration.interface
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var peers: [PeerConfiguration] {
|
2022-11-04 22:04:47 +00:00
|
|
|
tunnelConfiguration.peers
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-02-11 17:45:40 +00:00
|
|
|
public init(tunnelConfiguration: TunnelConfiguration) {
|
|
|
|
self.tunnelConfiguration = tunnelConfiguration
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-02-11 17:45:40 +00:00
|
|
|
public func builder() -> WireGuard.ConfigurationBuilder {
|
2022-11-04 22:04:47 +00:00
|
|
|
WireGuard.ConfigurationBuilder(tunnelConfiguration)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WireGuardConfigurationProviding
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var privateKey: String {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.privateKey.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var publicKey: String {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.privateKey.publicKey.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var addresses: [String] {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.addresses.map(\.stringRepresentation)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var dnsServers: [String] {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.dns.map(\.stringRepresentation)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var dnsSearchDomains: [String] {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.dnsSearch
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 07:22:30 +00:00
|
|
|
public var dnsHTTPSURL: URL? {
|
|
|
|
interface.dnsHTTPSURL
|
|
|
|
}
|
|
|
|
|
|
|
|
public var dnsTLSServerName: String? {
|
|
|
|
interface.dnsTLSServerName
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public var mtu: UInt16? {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.mtu
|
2022-02-11 17:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Codable
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-02-11 17:45:40 +00:00
|
|
|
public init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
let wg = try container.decode(String.self)
|
|
|
|
let cfg = try TunnelConfiguration(fromWgQuickConfig: wg, called: nil)
|
|
|
|
self.init(tunnelConfiguration: cfg)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-02-11 17:45:40 +00:00
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
|
|
let wg = tunnelConfiguration.asWgQuickConfig()
|
|
|
|
var container = encoder.singleValueContainer()
|
|
|
|
try container.encode(wg)
|
|
|
|
}
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
extension WireGuardConfigurationProviding {
|
|
|
|
public var publicKey: String {
|
2022-11-04 22:04:47 +00:00
|
|
|
interface.privateKey.publicKey.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var peersCount: Int {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers.count
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-12 09:09:40 +00:00
|
|
|
public func publicKey(ofPeer peerIndex: Int) -> String {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers[peerIndex].publicKey.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func preSharedKey(ofPeer peerIndex: Int) -> String? {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers[peerIndex].preSharedKey?.base64Key
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func endpoint(ofPeer peerIndex: Int) -> String? {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers[peerIndex].endpoint?.stringRepresentation
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func allowedIPs(ofPeer peerIndex: Int) -> [String] {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers[peerIndex].allowedIPs.map(\.stringRepresentation)
|
2022-03-12 09:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func keepAlive(ofPeer peerIndex: Int) -> UInt16? {
|
2022-11-04 22:04:47 +00:00
|
|
|
peers[peerIndex].persistentKeepAlive
|
2021-12-01 12:54:00 +00:00
|
|
|
}
|
|
|
|
}
|