From 7535458339fc6659b2e144118d2cc2f1f64bbfd6 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 11 Dec 2020 17:09:15 +0100 Subject: [PATCH 1/2] Parse domain option --- .../Protocols/OpenVPN/ConfigurationParser.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift b/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift index a03fa89..9396414 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift @@ -96,6 +96,8 @@ extension OpenVPN { static let domain = NSRegularExpression("^dhcp-option +DOMAIN +[^ ]+") + static let domainSearch = NSRegularExpression("^dhcp-option +DOMAIN-SEARCH +[^ ]+") + static let proxy = NSRegularExpression("^dhcp-option +PROXY_(HTTPS? +[^ ]+ +\\d+|AUTO_CONFIG_URL +[^ ]+)") static let proxyBypass = NSRegularExpression("^dhcp-option +PROXY_BYPASS +.+") @@ -225,6 +227,7 @@ extension OpenVPN { var optRoutes4: [(String, String, String?)] = [] // address, netmask, gateway var optRoutes6: [(String, UInt8, String?)] = [] // destination, prefix, gateway var optDNSServers: [String]? + var optDomain: String? var optSearchDomains: [String]? var optHTTPProxy: Proxy? var optHTTPSProxy: Proxy? @@ -528,6 +531,17 @@ extension OpenVPN { optDNSServers?.append($0[1]) } Regex.domain.enumerateArguments(in: line) { + guard $0.count == 2, optDomain == nil else { + return + } + optDomain = $0[1] + if optSearchDomains == nil { + optSearchDomains = [optDomain!] + } else { + optSearchDomains?.insert(optDomain!, at: 0) + } + } + Regex.domainSearch.enumerateArguments(in: line) { guard $0.count == 2 else { return } From 6b8d88fef5bc9e373e13f00c46b8f95df43ce75e Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 15 Dec 2020 13:57:57 +0100 Subject: [PATCH 2/2] Consider last appearing DOMAIN option --- .../Protocols/OpenVPN/ConfigurationParser.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift b/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift index 9396414..730ed42 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/ConfigurationParser.swift @@ -531,15 +531,10 @@ extension OpenVPN { optDNSServers?.append($0[1]) } Regex.domain.enumerateArguments(in: line) { - guard $0.count == 2, optDomain == nil else { + guard $0.count == 2 else { return } optDomain = $0[1] - if optSearchDomains == nil { - optSearchDomains = [optDomain!] - } else { - optSearchDomains?.insert(optDomain!, at: 0) - } } Regex.domainSearch.enumerateArguments(in: line) { guard $0.count == 2 else { @@ -763,6 +758,15 @@ extension OpenVPN { ) } + // prepend search domains with main domain (if set) + if let domain = optDomain { + if optSearchDomains == nil { + optSearchDomains = [domain] + } else { + optSearchDomains?.insert(domain, at: 0) + } + } + sessionBuilder.dnsServers = optDNSServers sessionBuilder.searchDomains = optSearchDomains sessionBuilder.httpProxy = optHTTPProxy