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) + let lowercasedLine = trimmedLine.lowercased() - guard !trimmedLine.isEmpty else { continue } - let lowercasedLine = line.lowercased() - - if let equalsIndex = line.firstIndex(of: "=") { - // Line contains an attribute - let keyWithCase = line[.. = ["address", "allowedips", "dns"] - if let presentValue = attributes[key] { - if keysWithMultipleEntriesAllowed.contains(key) { - attributes[key] = presentValue + "," + value + if !trimmedLine.isEmpty { + if let equalsIndex = line.firstIndex(of: "=") { + // Line contains an attribute + let keyWithCase = line[.. = ["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 = ["privatekey", "listenport", "address", "dns", "mtu"] + let peerSectionKeys: Set = ["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) } - let interfaceSectionKeys: Set = ["privatekey", "listenport", "address", "dns", "mtu"] - let peerSectionKeys: Set = ["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) } let isLastLine = lineIndex == lines.count - 1