2018-08-04 19:28:19 +00:00
|
|
|
//
|
|
|
|
// Tunnel+Extension.swift
|
|
|
|
// WireGuard
|
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 04-08-18.
|
2018-08-05 05:48:36 +00:00
|
|
|
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
|
2018-08-04 19:28:19 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension Tunnel {
|
|
|
|
public func generateProviderConfiguration() -> [String: Any] {
|
2018-08-06 07:04:29 +00:00
|
|
|
var providerConfiguration = [String: Any]()
|
|
|
|
|
|
|
|
providerConfiguration["title"] = self.title
|
2018-08-08 04:59:56 +00:00
|
|
|
providerConfiguration["tunnelIdentifier"] = self.tunnelIdentifier
|
2018-08-06 07:04:29 +00:00
|
|
|
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 {
|
2018-08-09 20:45:28 +00:00
|
|
|
var settingsString = ""
|
2018-08-06 07:04:29 +00:00
|
|
|
|
|
|
|
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) {
|
2018-08-09 20:45:28 +00:00
|
|
|
settingsString += "public_key=\(hexPublicKey)\n"
|
2018-08-06 07:04:29 +00:00
|
|
|
}
|
|
|
|
if let presharedKey = peer.presharedKey {
|
2018-08-09 20:45:28 +00:00
|
|
|
settingsString += "preshared_key=\(presharedKey)\n"
|
2018-08-06 07:04:29 +00:00
|
|
|
}
|
|
|
|
if let endpoint = peer.endpoint {
|
2018-08-09 20:45:28 +00:00
|
|
|
settingsString += "endpoint=\(endpoint)\n"
|
2018-08-06 07:04:29 +00:00
|
|
|
}
|
|
|
|
if peer.persistentKeepalive > 0 {
|
2018-08-09 20:45:28 +00:00
|
|
|
settingsString += "persistent_keepalive_interval=\(peer.persistentKeepalive)\n"
|
2018-08-06 07:04:29 +00:00
|
|
|
}
|
2018-08-07 19:31:43 +00:00
|
|
|
if let allowedIPs = peer.allowedIPs?.split(separator: ",") {
|
|
|
|
allowedIPs.forEach {
|
2018-08-09 20:45:28 +00:00
|
|
|
settingsString += "allowed_ip=\($0.trimmingCharacters(in: .whitespaces))\n"
|
2018-08-07 19:31:43 +00:00
|
|
|
}
|
2018-08-06 07:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return settingsString
|
2018-08-04 19:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 07:04:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|