From a6b2f2edca2ece142162f2dd8df7816145a3ee9f Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Mon, 6 Aug 2018 09:04:29 +0200 Subject: [PATCH] Add provider configuration generation. --- WireGuard/Models/Tunnel+Extension.swift | 87 ++++++++++++++++++- .../WireGuard.xcdatamodel/contents | 13 +-- .../PacketTunnelProvider.swift | 6 -- 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/WireGuard/Models/Tunnel+Extension.swift b/WireGuard/Models/Tunnel+Extension.swift index 2d9f951..b424cde 100644 --- a/WireGuard/Models/Tunnel+Extension.swift +++ b/WireGuard/Models/Tunnel+Extension.swift @@ -10,7 +10,90 @@ import Foundation extension Tunnel { public func generateProviderConfiguration() -> [String: Any] { - //TODO: generate ProviderConfiguration from tunnel with WireGuard config. - return [:] + var providerConfiguration = [String: Any]() + + providerConfiguration["title"] = self.title + var settingsString = "replace_peers=true\n" + if let interface = interface { + settingsString += generateInterfaceProviderConfiguration(interface) + } + + if let peers = peers?.array as? [Peer] { + peers.forEach { + settingsString += generatePeerProviderConfiguration($0) + } + + } + + providerConfiguration["settings"] = settingsString + + return providerConfiguration + } + + private func generateInterfaceProviderConfiguration(_ interface: Interface) -> String { + var settingsString = "replace_peers=true\n" + + if let hexPrivateKey = base64KeyToHex(interface.privateKey) { + settingsString += "private_key=\(hexPrivateKey)\n" + } + if interface.listenPort > 0 { + settingsString += "listen_port=\(interface.listenPort)\n" + } + if let dns = interface.dns { + settingsString += "dns=\(dns)\n" + } + if interface.mtu > 0 { + settingsString += "mtu=\(interface.mtu)\n" + } + + return settingsString + } + + private func generatePeerProviderConfiguration(_ peer: Peer) -> String { + var settingsString = "" + + if let hexPublicKey = base64KeyToHex(peer.publicKey) { + settingsString += "public_key=\(hexPublicKey)" + } + if let presharedKey = peer.presharedKey { + settingsString += "preshared_key=\(presharedKey)" + } + if let endpoint = peer.endpoint { + settingsString += "endpoint=\(endpoint)" + } + if peer.persistentKeepalive > 0 { + settingsString += "persistent_keepalive_interval=\(peer.persistentKeepalive)" + } + if let allowedIPs = peer.allowedIPs { + settingsString += "allowed_ip=\(allowedIPs)" // TODO: split on , + } + + return settingsString } } + +private func base64KeyToHex(_ base64: String?) -> String? { + guard let base64 = base64 else { + return nil + } + + guard base64.count == 44 else { + return nil + } + + guard base64.last == "=" else { + return nil + } + + guard let keyData = Data(base64Encoded: base64) else { + return nil + } + + guard keyData.count == 32 else { + return nil + } + + let hexKey = keyData.reduce("") {$0 + String(format: "%02x", $1)} + + return hexKey +} diff --git a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents index 1af139b..48e9027 100644 --- a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents +++ b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents @@ -1,18 +1,10 @@ - - - - - + - - - - @@ -29,8 +21,7 @@ - - + diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index c845a43..3cf2730 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -21,12 +21,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider { /// A reference to the WireGuard wrapper object. let wireGuardWrapper = WireGuardGoWrapper() - /// The completion handler to call when the tunnel is fully established. - var pendingStartCompletion: ((Error?) -> Void)? - - /// The completion handler to call when the tunnel is fully disconnected. - var pendingStopCompletion: (() -> Void)? - // MARK: NEPacketTunnelProvider /// Begin the process of establishing the tunnel.