Add string conversion for tunnel config.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts 2018-08-29 15:01:59 +02:00
parent cd6cb37163
commit b1aadaf82c
3 changed files with 52 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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? {