From b1aadaf82c861f5e74c1ae350b82dee51d9a6a70 Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Wed, 29 Aug 2018 15:01:59 +0200 Subject: [PATCH] Add string conversion for tunnel config. Signed-off-by: Jason A. Donenfeld --- WireGuard/Models/Interface+Extension.swift | 14 +++++++++++++ WireGuard/Models/Peer+Extension.swift | 23 ++++++++++++++++++++++ WireGuard/Models/Tunnel+Extension.swift | 15 ++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/WireGuard/Models/Interface+Extension.swift b/WireGuard/Models/Interface+Extension.swift index 060b4b7..3b6ef62 100644 --- a/WireGuard/Models/Interface+Extension.swift +++ b/WireGuard/Models/Interface+Extension.swift @@ -57,6 +57,20 @@ extension Interface { } } + func export() -> String { + var exportString = "[Interface]\n" + if let privateKey = privateKey { + exportString.append("PrivateKey=\(privateKey)") + } + if listenPort > 0 { + exportString.append("ListenPort=\(listenPort)") + } + + exportString.append("\n") + + return exportString + } + } enum InterfaceValidationError: Error { diff --git a/WireGuard/Models/Peer+Extension.swift b/WireGuard/Models/Peer+Extension.swift index f83c038..3ffd941 100644 --- a/WireGuard/Models/Peer+Extension.swift +++ b/WireGuard/Models/Peer+Extension.swift @@ -63,6 +63,29 @@ extension Peer { } } + func export() -> String { + var exportString = "[Peer]\n" + if let publicKey = publicKey { + exportString.append("PublicKey=\(publicKey)\n") + } + if let presharedKey = presharedKey { + exportString.append("PresharedKey=\(presharedKey)\n") + } + if let allowedIPs = allowedIPs { + exportString.append("AllowedIPs=\(allowedIPs)\n") + } + if let endpoint = endpoint { + exportString.append("Endpoint=\(endpoint)\n") + } + if persistentKeepalive > 0 { + exportString.append("PersistentKeepalive=\(persistentKeepalive)\n") + } + + exportString.append("\n") + + return exportString + } + } enum PeerValidationError: Error { diff --git a/WireGuard/Models/Tunnel+Extension.swift b/WireGuard/Models/Tunnel+Extension.swift index 62014f1..0cced3c 100644 --- a/WireGuard/Models/Tunnel+Extension.swift +++ b/WireGuard/Models/Tunnel+Extension.swift @@ -159,6 +159,21 @@ extension Tunnel { return tunnel } + func export() -> String { + var exportString = "" + if let interfaceExport = self.interface?.export() { + exportString.append(interfaceExport) + } + + if let peers = peers?.array as? [Peer] { + peers.forEach { + exportString.append($0.export()) + } + } + + return exportString + } + } private func base64KeyToHex(_ base64: String?) -> String? {