diff options
author | Roopesh Chander <roop@roopc.net> | 2019-01-24 16:23:07 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2019-01-24 16:23:07 +0530 |
commit | 812e660491f3bbd8136f090a6a19cbf7bd074d79 (patch) | |
tree | 5c4c4e9447047c5e76e41d78ba3a23d46614ce6a /WireGuard | |
parent | macOS: show runtime configuration in tunnel manager (diff) | |
download | wireguard-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')
-rw-r--r-- | WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift b/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift index 043914a..65676f5 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 |