2018-10-24 13:48:05 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 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 {
|
2019-01-08 10:14:17 +00:00
|
|
|
case invalidLine(String.SubSequence)
|
2018-10-24 13:48:05 +00:00
|
|
|
case noInterface
|
|
|
|
case multipleInterfaces
|
2019-01-08 10:14:17 +00:00
|
|
|
case interfaceHasNoPrivateKey
|
|
|
|
case interfaceHasInvalidPrivateKey(String)
|
|
|
|
case interfaceHasInvalidListenPort(String)
|
|
|
|
case interfaceHasInvalidAddress(String)
|
|
|
|
case interfaceHasInvalidDNS(String)
|
|
|
|
case interfaceHasInvalidMTU(String)
|
|
|
|
case interfaceHasUnrecognizedKey(String)
|
|
|
|
case peerHasNoPublicKey
|
|
|
|
case peerHasInvalidPublicKey(String)
|
|
|
|
case peerHasInvalidPreSharedKey(String)
|
|
|
|
case peerHasInvalidAllowedIP(String)
|
|
|
|
case peerHasInvalidEndpoint(String)
|
|
|
|
case peerHasInvalidPersistentKeepAlive(String)
|
2019-01-23 23:00:46 +00:00
|
|
|
case peerHasInvalidTransferBytes(String)
|
|
|
|
case peerHasInvalidLastHandshakeTime(String)
|
2019-01-08 10:14:17 +00:00
|
|
|
case peerHasUnrecognizedKey(String)
|
2018-11-06 02:47:03 +00:00
|
|
|
case multiplePeersWithSamePublicKey
|
2019-01-08 13:58:38 +00:00
|
|
|
case multipleEntriesForKey(String)
|
2018-10-24 13:48:05 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-01-08 18:06:27 +00:00
|
|
|
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
|
|
|
|
2019-06-28 10:26:39 +00:00
|
|
|
let lines = wgQuickConfig.split { $0.isNewline }
|
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
|
|
|
|
2019-03-11 19:59:31 +00:00
|
|
|
trimmedLine = trimmedLine.trimmingCharacters(in: .whitespacesAndNewlines)
|
2019-01-24 10:53:07 +00:00
|
|
|
let lowercasedLine = trimmedLine.lowercased()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-01-24 10:53:07 +00:00
|
|
|
if !trimmedLine.isEmpty {
|
2019-02-05 11:15:32 +00:00
|
|
|
if let equalsIndex = trimmedLine.firstIndex(of: "=") {
|
2019-01-24 10:53:07 +00:00
|
|
|
// Line contains an attribute
|
2019-03-11 19:59:31 +00:00
|
|
|
let keyWithCase = trimmedLine[..<equalsIndex].trimmingCharacters(in: .whitespacesAndNewlines)
|
2019-01-24 10:53:07 +00:00
|
|
|
let key = keyWithCase.lowercased()
|
2019-03-11 19:59:31 +00:00
|
|
|
let value = trimmedLine[trimmedLine.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespacesAndNewlines)
|
2019-01-24 10:53:07 +00:00
|
|
|
let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
|
|
|
|
if let presentValue = attributes[key] {
|
|
|
|
if keysWithMultipleEntriesAllowed.contains(key) {
|
|
|
|
attributes[key] = presentValue + "," + value
|
|
|
|
} else {
|
|
|
|
throw ParseError.multipleEntriesForKey(keyWithCase)
|
|
|
|
}
|
2019-01-08 13:58:38 +00:00
|
|
|
} else {
|
2019-01-24 10:53:07 +00:00
|
|
|
attributes[key] = value
|
2019-01-08 18:06:27 +00:00
|
|
|
}
|
2019-01-24 10:53:07 +00:00
|
|
|
let interfaceSectionKeys: Set<String> = ["privatekey", "listenport", "address", "dns", "mtu"]
|
|
|
|
let peerSectionKeys: Set<String> = ["publickey", "presharedkey", "allowedips", "endpoint", "persistentkeepalive"]
|
|
|
|
if parserState == .inInterfaceSection {
|
|
|
|
guard interfaceSectionKeys.contains(key) else {
|
|
|
|
throw ParseError.interfaceHasUnrecognizedKey(keyWithCase)
|
|
|
|
}
|
|
|
|
} else if parserState == .inPeerSection {
|
|
|
|
guard peerSectionKeys.contains(key) else {
|
|
|
|
throw ParseError.peerHasUnrecognizedKey(keyWithCase)
|
|
|
|
}
|
2019-01-08 10:14:17 +00:00
|
|
|
}
|
2019-01-24 10:53:07 +00:00
|
|
|
} else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
|
|
|
|
throw ParseError.invalidLine(line)
|
2019-01-08 10:14:17 +00:00
|
|
|
}
|
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 {
|
2019-01-08 10:14:17 +00:00
|
|
|
let interface = try TunnelConfiguration.collate(interfaceAttributes: attributes)
|
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 {
|
2019-01-08 10:14:17 +00:00
|
|
|
let peer = try TunnelConfiguration.collate(peerAttributes: attributes)
|
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 }
|
2020-11-26 16:23:50 +00:00
|
|
|
let peerPublicKeysSet = Set<PublicKey>(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"
|
2020-11-26 16:23:50 +00:00
|
|
|
output.append("PrivateKey = \(interface.privateKey.base64Key)\n")
|
2018-12-21 21:16:09 +00:00
|
|
|
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")
|
2020-11-26 16:23:50 +00:00
|
|
|
output.append("PublicKey = \(peer.publicKey.base64Key)\n")
|
|
|
|
if let preSharedKey = peer.preSharedKey?.base64Key {
|
2019-02-07 23:44:14 +00:00
|
|
|
output.append("PresharedKey = \(preSharedKey)\n")
|
2018-12-21 21:16:09 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-01-08 10:14:17 +00:00
|
|
|
private static func collate(interfaceAttributes attributes: [String: String]) throws -> InterfaceConfiguration {
|
|
|
|
guard let privateKeyString = attributes["privatekey"] else {
|
|
|
|
throw ParseError.interfaceHasNoPrivateKey
|
|
|
|
}
|
2020-11-26 16:23:50 +00:00
|
|
|
guard let privateKey = PrivateKey(base64Key: privateKeyString) else {
|
2019-01-08 10:14:17 +00:00
|
|
|
throw ParseError.interfaceHasInvalidPrivateKey(privateKeyString)
|
|
|
|
}
|
2018-12-21 23:28:18 +00:00
|
|
|
var interface = InterfaceConfiguration(privateKey: privateKey)
|
2018-12-12 21:33:14 +00:00
|
|
|
if let listenPortString = attributes["listenport"] {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let listenPort = UInt16(listenPortString) else {
|
|
|
|
throw ParseError.interfaceHasInvalidListenPort(listenPortString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
interface.listenPort = listenPort
|
|
|
|
}
|
|
|
|
if let addressesString = attributes["address"] {
|
2018-12-13 03:09:52 +00:00
|
|
|
var addresses = [IPAddressRange]()
|
2019-03-11 19:59:31 +00:00
|
|
|
for addressString in addressesString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let address = IPAddressRange(from: addressString) else {
|
|
|
|
throw ParseError.interfaceHasInvalidAddress(addressString)
|
|
|
|
}
|
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]()
|
2019-03-11 19:59:31 +00:00
|
|
|
for dnsServerString in dnsString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let dnsServer = DNSServer(from: dnsServerString) else {
|
|
|
|
throw ParseError.interfaceHasInvalidDNS(dnsServerString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
dnsServers.append(dnsServer)
|
|
|
|
}
|
|
|
|
interface.dns = dnsServers
|
|
|
|
}
|
|
|
|
if let mtuString = attributes["mtu"] {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let mtu = UInt16(mtuString) else {
|
|
|
|
throw ParseError.interfaceHasInvalidMTU(mtuString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
interface.mtu = mtu
|
|
|
|
}
|
|
|
|
return interface
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-01-08 10:14:17 +00:00
|
|
|
private static func collate(peerAttributes attributes: [String: String]) throws -> PeerConfiguration {
|
|
|
|
guard let publicKeyString = attributes["publickey"] else {
|
|
|
|
throw ParseError.peerHasNoPublicKey
|
|
|
|
}
|
2020-11-26 16:23:50 +00:00
|
|
|
guard let publicKey = PublicKey(base64Key: publicKeyString) else {
|
2019-01-08 10:14:17 +00:00
|
|
|
throw ParseError.peerHasInvalidPublicKey(publicKeyString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
var peer = PeerConfiguration(publicKey: publicKey)
|
|
|
|
if let preSharedKeyString = attributes["presharedkey"] {
|
2020-11-26 16:23:50 +00:00
|
|
|
guard let preSharedKey = PreSharedKey(base64Key: preSharedKeyString) else {
|
2019-01-08 10:14:17 +00:00
|
|
|
throw ParseError.peerHasInvalidPreSharedKey(preSharedKeyString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
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) {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let allowedIP = IPAddressRange(from: allowedIPString) else {
|
|
|
|
throw ParseError.peerHasInvalidAllowedIP(allowedIPString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
allowedIPs.append(allowedIP)
|
|
|
|
}
|
|
|
|
peer.allowedIPs = allowedIPs
|
|
|
|
}
|
|
|
|
if let endpointString = attributes["endpoint"] {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let endpoint = Endpoint(from: endpointString) else {
|
|
|
|
throw ParseError.peerHasInvalidEndpoint(endpointString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
peer.endpoint = endpoint
|
|
|
|
}
|
|
|
|
if let persistentKeepAliveString = attributes["persistentkeepalive"] {
|
2019-01-08 10:14:17 +00:00
|
|
|
guard let persistentKeepAlive = UInt16(persistentKeepAliveString) else {
|
|
|
|
throw ParseError.peerHasInvalidPersistentKeepAlive(persistentKeepAliveString)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
peer.persistentKeepAlive = persistentKeepAlive
|
|
|
|
}
|
|
|
|
return peer
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-10-24 13:48:05 +00:00
|
|
|
}
|