2018-10-24 13:48:05 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-10-24 13:48:05 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
extension TunnelConfiguration {
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
enum ParserState {
|
|
|
|
case inInterfaceSection
|
|
|
|
case inPeerSection
|
|
|
|
case notInASection
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
enum ParseError: Error {
|
|
|
|
case invalidLine(_ line: String.SubSequence)
|
|
|
|
case noInterface
|
|
|
|
case invalidInterface
|
|
|
|
case multipleInterfaces
|
2018-11-06 02:47:03 +00:00
|
|
|
case multiplePeersWithSamePublicKey
|
2018-10-24 13:48:05 +00:00
|
|
|
case invalidPeer
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 23:28:18 +00:00
|
|
|
//swiftlint:disable:next function_body_length cyclomatic_complexity
|
|
|
|
convenience init(fromWgQuickConfig wgQuickConfig: String, called name: String? = nil) throws {
|
2018-11-03 18:35:25 +00:00
|
|
|
var interfaceConfiguration: InterfaceConfiguration?
|
2018-12-12 21:33:14 +00:00
|
|
|
var peerConfigurations = [PeerConfiguration]()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
let lines = wgQuickConfig.split(separator: "\n")
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
var parserState = ParserState.notInASection
|
|
|
|
var attributes = [String: String]()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
for (lineIndex, line) in lines.enumerated() {
|
|
|
|
var trimmedLine: String
|
|
|
|
if let commentRange = line.range(of: "#") {
|
|
|
|
trimmedLine = String(line[..<commentRange.lowerBound])
|
|
|
|
} else {
|
|
|
|
trimmedLine = String(line)
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
trimmedLine = trimmedLine.trimmingCharacters(in: .whitespaces)
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-20 17:22:37 +00:00
|
|
|
guard !trimmedLine.isEmpty else { continue }
|
2018-10-24 13:48:05 +00:00
|
|
|
let lowercasedLine = line.lowercased()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
if let equalsIndex = line.firstIndex(of: "=") {
|
|
|
|
// Line contains an attribute
|
2018-11-19 07:04:53 +00:00
|
|
|
let key = line[..<equalsIndex].trimmingCharacters(in: .whitespaces).lowercased()
|
2018-10-24 13:48:05 +00:00
|
|
|
let value = line[line.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces)
|
2018-11-19 07:04:53 +00:00
|
|
|
let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
|
2018-11-05 18:22:16 +00:00
|
|
|
if let presentValue = attributes[key], keysWithMultipleEntriesAllowed.contains(key) {
|
|
|
|
attributes[key] = presentValue + "," + value
|
|
|
|
} else {
|
|
|
|
attributes[key] = value
|
|
|
|
}
|
2018-12-13 03:09:52 +00:00
|
|
|
} else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
|
|
|
|
throw ParseError.invalidLine(line)
|
2018-10-24 13:48:05 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
let isLastLine = lineIndex == lines.count - 1
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
if isLastLine || lowercasedLine == "[interface]" || lowercasedLine == "[peer]" {
|
2018-10-24 13:48:05 +00:00
|
|
|
// Previous section has ended; process the attributes collected so far
|
2018-12-12 18:28:27 +00:00
|
|
|
if parserState == .inInterfaceSection {
|
2018-12-21 23:28:18 +00:00
|
|
|
guard let interface = TunnelConfiguration.collate(interfaceAttributes: attributes) else { throw ParseError.invalidInterface }
|
2018-12-12 18:28:27 +00:00
|
|
|
guard interfaceConfiguration == nil else { throw ParseError.multipleInterfaces }
|
2018-10-24 13:48:05 +00:00
|
|
|
interfaceConfiguration = interface
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if parserState == .inPeerSection {
|
2018-12-21 21:16:09 +00:00
|
|
|
guard let peer = TunnelConfiguration.collate(peerAttributes: attributes) else { throw ParseError.invalidPeer }
|
2018-10-24 13:48:05 +00:00
|
|
|
peerConfigurations.append(peer)
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
if lowercasedLine == "[interface]" {
|
2018-10-24 13:48:05 +00:00
|
|
|
parserState = .inInterfaceSection
|
|
|
|
attributes.removeAll()
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if lowercasedLine == "[peer]" {
|
2018-10-24 13:48:05 +00:00
|
|
|
parserState = .inPeerSection
|
|
|
|
attributes.removeAll()
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-11-06 02:47:03 +00:00
|
|
|
let peerPublicKeysArray = peerConfigurations.map { $0.publicKey }
|
|
|
|
let peerPublicKeysSet = Set<Data>(peerPublicKeysArray)
|
2018-12-12 18:28:27 +00:00
|
|
|
if peerPublicKeysArray.count != peerPublicKeysSet.count {
|
2018-11-06 02:47:03 +00:00
|
|
|
throw ParseError.multiplePeersWithSamePublicKey
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
if let interfaceConfiguration = interfaceConfiguration {
|
2018-12-21 23:28:18 +00:00
|
|
|
self.init(name: name, interface: interfaceConfiguration, peers: peerConfigurations)
|
2018-10-24 13:48:05 +00:00
|
|
|
} else {
|
|
|
|
throw ParseError.noInterface
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
func asWgQuickConfig() -> String {
|
|
|
|
var output = "[Interface]\n"
|
|
|
|
output.append("PrivateKey = \(interface.privateKey.base64EncodedString())\n")
|
|
|
|
if let listenPort = interface.listenPort {
|
|
|
|
output.append("ListenPort = \(listenPort)\n")
|
|
|
|
}
|
|
|
|
if !interface.addresses.isEmpty {
|
|
|
|
let addressString = interface.addresses.map { $0.stringRepresentation }.joined(separator: ", ")
|
|
|
|
output.append("Address = \(addressString)\n")
|
|
|
|
}
|
|
|
|
if !interface.dns.isEmpty {
|
|
|
|
let dnsString = interface.dns.map { $0.stringRepresentation }.joined(separator: ", ")
|
|
|
|
output.append("DNS = \(dnsString)\n")
|
|
|
|
}
|
|
|
|
if let mtu = interface.mtu {
|
|
|
|
output.append("MTU = \(mtu)\n")
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
for peer in peers {
|
|
|
|
output.append("\n[Peer]\n")
|
|
|
|
output.append("PublicKey = \(peer.publicKey.base64EncodedString())\n")
|
|
|
|
if let preSharedKey = peer.preSharedKey {
|
|
|
|
output.append("PresharedKey = \(preSharedKey.base64EncodedString())\n")
|
|
|
|
}
|
|
|
|
if !peer.allowedIPs.isEmpty {
|
|
|
|
let allowedIPsString = peer.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ")
|
|
|
|
output.append("AllowedIPs = \(allowedIPsString)\n")
|
|
|
|
}
|
|
|
|
if let endpoint = peer.endpoint {
|
|
|
|
output.append("Endpoint = \(endpoint.stringRepresentation)\n")
|
|
|
|
}
|
|
|
|
if let persistentKeepAlive = peer.persistentKeepAlive {
|
|
|
|
output.append("PersistentKeepalive = \(persistentKeepAlive)\n")
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 21:16:09 +00:00
|
|
|
return output
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
//swiftlint:disable:next cyclomatic_complexity
|
2018-12-21 23:28:18 +00:00
|
|
|
private static func collate(interfaceAttributes attributes: [String: String]) -> InterfaceConfiguration? {
|
2018-12-12 21:33:14 +00:00
|
|
|
// required wg fields
|
|
|
|
guard let privateKeyString = attributes["privatekey"] else { return nil }
|
|
|
|
guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == TunnelConfiguration.keyLength else { return nil }
|
2018-12-21 23:28:18 +00:00
|
|
|
var interface = InterfaceConfiguration(privateKey: privateKey)
|
2018-12-12 21:33:14 +00:00
|
|
|
// other wg fields
|
|
|
|
if let listenPortString = attributes["listenport"] {
|
|
|
|
guard let listenPort = UInt16(listenPortString) else { return nil }
|
|
|
|
interface.listenPort = listenPort
|
|
|
|
}
|
|
|
|
// wg-quick fields
|
|
|
|
if let addressesString = attributes["address"] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var addresses = [IPAddressRange]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for addressString in addressesString.splitToArray(trimmingCharacters: .whitespaces) {
|
|
|
|
guard let address = IPAddressRange(from: addressString) else { return nil }
|
2018-12-12 21:33:14 +00:00
|
|
|
addresses.append(address)
|
|
|
|
}
|
|
|
|
interface.addresses = addresses
|
|
|
|
}
|
|
|
|
if let dnsString = attributes["dns"] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var dnsServers = [DNSServer]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for dnsServerString in dnsString.splitToArray(trimmingCharacters: .whitespaces) {
|
|
|
|
guard let dnsServer = DNSServer(from: dnsServerString) else { return nil }
|
2018-12-12 21:33:14 +00:00
|
|
|
dnsServers.append(dnsServer)
|
|
|
|
}
|
|
|
|
interface.dns = dnsServers
|
|
|
|
}
|
|
|
|
if let mtuString = attributes["mtu"] {
|
|
|
|
guard let mtu = UInt16(mtuString) else { return nil }
|
|
|
|
interface.mtu = mtu
|
|
|
|
}
|
|
|
|
return interface
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
//swiftlint:disable:next cyclomatic_complexity
|
2018-12-12 21:33:14 +00:00
|
|
|
private static func collate(peerAttributes attributes: [String: String]) -> PeerConfiguration? {
|
|
|
|
// required wg fields
|
|
|
|
guard let publicKeyString = attributes["publickey"] else { return nil }
|
|
|
|
guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == TunnelConfiguration.keyLength else { return nil }
|
|
|
|
var peer = PeerConfiguration(publicKey: publicKey)
|
|
|
|
// wg fields
|
|
|
|
if let preSharedKeyString = attributes["presharedkey"] {
|
|
|
|
guard let preSharedKey = Data(base64Encoded: preSharedKeyString), preSharedKey.count == TunnelConfiguration.keyLength else { return nil }
|
|
|
|
peer.preSharedKey = preSharedKey
|
|
|
|
}
|
|
|
|
if let allowedIPsString = attributes["allowedips"] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var allowedIPs = [IPAddressRange]()
|
2018-12-20 16:46:26 +00:00
|
|
|
for allowedIPString in allowedIPsString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
|
|
|
guard let allowedIP = IPAddressRange(from: allowedIPString) else { return nil }
|
2018-12-12 21:33:14 +00:00
|
|
|
allowedIPs.append(allowedIP)
|
|
|
|
}
|
|
|
|
peer.allowedIPs = allowedIPs
|
|
|
|
}
|
|
|
|
if let endpointString = attributes["endpoint"] {
|
|
|
|
guard let endpoint = Endpoint(from: endpointString) else { return nil }
|
|
|
|
peer.endpoint = endpoint
|
|
|
|
}
|
|
|
|
if let persistentKeepAliveString = attributes["persistentkeepalive"] {
|
|
|
|
guard let persistentKeepAlive = UInt16(persistentKeepAliveString) else { return nil }
|
|
|
|
peer.persistentKeepAlive = persistentKeepAlive
|
|
|
|
}
|
|
|
|
return peer
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
}
|