aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-01-24 16:23:07 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-24 16:23:07 +0530
commit812e660491f3bbd8136f090a6a19cbf7bd074d79 (patch)
tree5c4c4e9447047c5e76e41d78ba3a23d46614ce6a /WireGuard/Shared/Model
parentmacOS: show runtime configuration in tunnel manager (diff)
downloadwireguard-apple-812e660491f3bbd8136f090a6a19cbf7bd074d79.tar.xz
wireguard-apple-812e660491f3bbd8136f090a6a19cbf7bd074d79.zip
Config file parsing: Fix bug when there are comments at the end
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared/Model')
-rw-r--r--WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift56
1 files changed, 28 insertions, 28 deletions
diff --git a/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift b/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift
index 043914a4..65676f56 100644
--- a/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift
+++ b/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift
@@ -54,38 +54,38 @@ extension TunnelConfiguration {
}
trimmedLine = trimmedLine.trimmingCharacters(in: .whitespaces)
-
- guard !trimmedLine.isEmpty else { continue }
- let lowercasedLine = line.lowercased()
-
- if let equalsIndex = line.firstIndex(of: "=") {
- // Line contains an attribute
- let keyWithCase = line[..<equalsIndex].trimmingCharacters(in: .whitespaces)
- let key = keyWithCase.lowercased()
- let value = line[line.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces)
- let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
- if let presentValue = attributes[key] {
- if keysWithMultipleEntriesAllowed.contains(key) {
- attributes[key] = presentValue + "," + value
+ let lowercasedLine = trimmedLine.lowercased()
+
+ if !trimmedLine.isEmpty {
+ if let equalsIndex = line.firstIndex(of: "=") {
+ // Line contains an attribute
+ let keyWithCase = line[..<equalsIndex].trimmingCharacters(in: .whitespaces)
+ let key = keyWithCase.lowercased()
+ let value = line[line.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces)
+ 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)
+ }
} else {
- throw ParseError.multipleEntriesForKey(keyWithCase)
+ attributes[key] = value
}
- } else {
- attributes[key] = value
- }
- 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)
+ 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)
+ }
}
+ } else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
+ throw ParseError.invalidLine(line)
}
- } else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
- throw ParseError.invalidLine(line)
}
let isLastLine = lineIndex == lines.count - 1