From 22625e8cc4792a71174af5fc69f0f63ba4b808d8 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 24 Jan 2019 00:00:46 +0100 Subject: Tunnel: support getting runtime configuration Signed-off-by: Jason A. Donenfeld --- .../Tunnel/TunnelConfiguration+UapiConfig.swift | 207 +++++++++++++++++++++ WireGuard/WireGuard/Tunnel/TunnelsManager.swift | 17 ++ 2 files changed, 224 insertions(+) create mode 100644 WireGuard/WireGuard/Tunnel/TunnelConfiguration+UapiConfig.swift (limited to 'WireGuard/WireGuard/Tunnel') diff --git a/WireGuard/WireGuard/Tunnel/TunnelConfiguration+UapiConfig.swift b/WireGuard/WireGuard/Tunnel/TunnelConfiguration+UapiConfig.swift new file mode 100644 index 0000000..63a8570 --- /dev/null +++ b/WireGuard/WireGuard/Tunnel/TunnelConfiguration+UapiConfig.swift @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved. + +import Foundation + +extension TunnelConfiguration { + //swiftlint:disable:next function_body_length cyclomatic_complexity + convenience init(fromUapiConfig uapiConfig: String, basedOn base: TunnelConfiguration? = nil) throws { + var interfaceConfiguration: InterfaceConfiguration? + var peerConfigurations = [PeerConfiguration]() + + var lines = uapiConfig.split(separator: "\n") + lines.append("") + + var parserState = ParserState.inInterfaceSection + var attributes = [String: String]() + + for line in lines { + var key = "" + var value = "" + + if !line.isEmpty { + guard let equalsIndex = line.firstIndex(of: "=") else { throw ParseError.invalidLine(line) } + key = String(line[.. = ["private_key", "listen_port", "fwmark"] + let peerSectionKeys: Set = ["public_key", "preshared_key", "allowed_ip", "endpoint", "persistent_keepalive_interval", "last_handshake_time_sec", "last_handshake_time_nsec", "rx_bytes", "tx_bytes", "protocol_version"] + + if parserState == .inInterfaceSection { + guard interfaceSectionKeys.contains(key) else { + throw ParseError.interfaceHasUnrecognizedKey(key) + } + } + if parserState == .inPeerSection { + guard peerSectionKeys.contains(key) else { + throw ParseError.peerHasUnrecognizedKey(key) + } + } + } + + let peerPublicKeysArray = peerConfigurations.map { $0.publicKey } + let peerPublicKeysSet = Set(peerPublicKeysArray) + if peerPublicKeysArray.count != peerPublicKeysSet.count { + throw ParseError.multiplePeersWithSamePublicKey + } + + interfaceConfiguration?.addresses = base?.interface.addresses ?? [] + interfaceConfiguration?.dns = base?.interface.dns ?? [] + interfaceConfiguration?.mtu = base?.interface.mtu + + if let interfaceConfiguration = interfaceConfiguration { + self.init(name: base?.name, interface: interfaceConfiguration, peers: peerConfigurations) + } else { + throw ParseError.noInterface + } + } + + private static func collate(interfaceAttributes attributes: [String: String]) throws -> InterfaceConfiguration { + guard let privateKeyString = attributes["private_key"] else { + throw ParseError.interfaceHasNoPrivateKey + } + guard let privateKey = Data(hexEncoded: privateKeyString), privateKey.count == TunnelConfiguration.keyLength else { + throw ParseError.interfaceHasInvalidPrivateKey(privateKeyString) + } + var interface = InterfaceConfiguration(privateKey: privateKey) + if let listenPortString = attributes["listen_port"] { + guard let listenPort = UInt16(listenPortString) else { + throw ParseError.interfaceHasInvalidListenPort(listenPortString) + } + if listenPort != 0 { + interface.listenPort = listenPort + } + } + return interface + } + + //swiftlint:disable:next cyclomatic_complexity + private static func collate(peerAttributes attributes: [String: String]) throws -> PeerConfiguration { + guard let publicKeyString = attributes["public_key"] else { + throw ParseError.peerHasNoPublicKey + } + guard let publicKey = Data(hexEncoded: publicKeyString), publicKey.count == TunnelConfiguration.keyLength else { + throw ParseError.peerHasInvalidPublicKey(publicKeyString) + } + var peer = PeerConfiguration(publicKey: publicKey) + if let preSharedKeyString = attributes["preshared_key"] { + guard let preSharedKey = Data(hexEncoded: preSharedKeyString), preSharedKey.count == TunnelConfiguration.keyLength else { + throw ParseError.peerHasInvalidPreSharedKey(preSharedKeyString) + } + // TODO(zx2c4): does the compiler optimize this away? + var accumulator: UInt8 = 0 + for index in 0.. Void)) { + guard status != .inactive, let session = tunnelProvider.connection as? NETunnelProviderSession else { + completionHandler(tunnelConfiguration) + return + } + guard nil != (try? session.sendProviderMessage(Data(bytes: [ 0 ]), responseHandler: { + guard self.status != .inactive, let data = $0, let base = self.tunnelConfiguration, let settings = String(data: data, encoding: .utf8) else { + completionHandler(self.tunnelConfiguration) + return + } + completionHandler((try? TunnelConfiguration(fromUapiConfig: settings, basedOn: base)) ?? self.tunnelConfiguration) + })) else { + completionHandler(tunnelConfiguration) + return + } + } + func refreshStatus() { let status = TunnelStatus(from: tunnelProvider.connection.status) self.status = status -- cgit v1.2.3-59-g8ed1b