wg-quick parser: trim \r as well

The influx of Windows users has already begun to infect our nice
project.

Reported-by: Cosku Bas <cosku.bas@gmail.com>
This commit is contained in:
Jason A. Donenfeld 2019-03-11 13:59:31 -06:00
parent ce291d54a1
commit 1d768d4e9d
1 changed files with 5 additions and 5 deletions

View File

@ -52,15 +52,15 @@ extension TunnelConfiguration {
trimmedLine = String(line) trimmedLine = String(line)
} }
trimmedLine = trimmedLine.trimmingCharacters(in: .whitespaces) trimmedLine = trimmedLine.trimmingCharacters(in: .whitespacesAndNewlines)
let lowercasedLine = trimmedLine.lowercased() let lowercasedLine = trimmedLine.lowercased()
if !trimmedLine.isEmpty { if !trimmedLine.isEmpty {
if let equalsIndex = trimmedLine.firstIndex(of: "=") { if let equalsIndex = trimmedLine.firstIndex(of: "=") {
// Line contains an attribute // Line contains an attribute
let keyWithCase = trimmedLine[..<equalsIndex].trimmingCharacters(in: .whitespaces) let keyWithCase = trimmedLine[..<equalsIndex].trimmingCharacters(in: .whitespacesAndNewlines)
let key = keyWithCase.lowercased() let key = keyWithCase.lowercased()
let value = trimmedLine[trimmedLine.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces) let value = trimmedLine[trimmedLine.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespacesAndNewlines)
let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"] let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
if let presentValue = attributes[key] { if let presentValue = attributes[key] {
if keysWithMultipleEntriesAllowed.contains(key) { if keysWithMultipleEntriesAllowed.contains(key) {
@ -182,7 +182,7 @@ extension TunnelConfiguration {
} }
if let addressesString = attributes["address"] { if let addressesString = attributes["address"] {
var addresses = [IPAddressRange]() var addresses = [IPAddressRange]()
for addressString in addressesString.splitToArray(trimmingCharacters: .whitespaces) { for addressString in addressesString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
guard let address = IPAddressRange(from: addressString) else { guard let address = IPAddressRange(from: addressString) else {
throw ParseError.interfaceHasInvalidAddress(addressString) throw ParseError.interfaceHasInvalidAddress(addressString)
} }
@ -192,7 +192,7 @@ extension TunnelConfiguration {
} }
if let dnsString = attributes["dns"] { if let dnsString = attributes["dns"] {
var dnsServers = [DNSServer]() var dnsServers = [DNSServer]()
for dnsServerString in dnsString.splitToArray(trimmingCharacters: .whitespaces) { for dnsServerString in dnsString.splitToArray(trimmingCharacters: .whitespacesAndNewlines) {
guard let dnsServer = DNSServer(from: dnsServerString) else { guard let dnsServer = DNSServer(from: dnsServerString) else {
throw ParseError.interfaceHasInvalidDNS(dnsServerString) throw ParseError.interfaceHasInvalidDNS(dnsServerString)
} }