Split DNS domain and search domains (#313)

This commit is contained in:
Davide De Rosa 2023-03-17 20:22:21 +01:00 committed by GitHub
parent 11ae9e4141
commit e0c0cc137f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 7 deletions

View File

@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- OpenVPN: Deal with remote options properly. [#297](https://github.com/passepartoutvpn/tunnelkit/pull/297) - OpenVPN: Deal with remote options properly. [#297](https://github.com/passepartoutvpn/tunnelkit/pull/297)
- OpenVPN: Routes from configuration file are ignored. [#278](https://github.com/passepartoutvpn/tunnelkit/issues/278) - OpenVPN: Routes from configuration file are ignored. [#278](https://github.com/passepartoutvpn/tunnelkit/issues/278)
- OpenVPN: Split DNS domain and search domains. [#313](https://github.com/passepartoutvpn/tunnelkit/pulls/313)
- IPv6 endpoints are parsed improperly. [#293](https://github.com/passepartoutvpn/tunnelkit/issues/293) - IPv6 endpoints are parsed improperly. [#293](https://github.com/passepartoutvpn/tunnelkit/issues/293)
- Fix abandoned MockVPN. [#285](https://github.com/passepartoutvpn/tunnelkit/pull/285) - Fix abandoned MockVPN. [#285](https://github.com/passepartoutvpn/tunnelkit/pull/285)

View File

@ -128,6 +128,14 @@ extension NetworkSettingsBuilder {
return servers return servers
} }
private var dnsDomain: String? {
var domain = localOptions.dnsDomain
if pullDNS, let remoteDomain = remoteOptions.dnsDomain {
domain = remoteDomain
}
return domain
}
private var allDNSSearchDomains: [String] { private var allDNSSearchDomains: [String] {
var searchDomains = localOptions.searchDomains ?? [] var searchDomains = localOptions.searchDomains ?? []
if pullDNS, let remoteSearchDomains = remoteOptions.searchDomains { if pullDNS, let remoteSearchDomains = remoteOptions.searchDomains {
@ -275,10 +283,14 @@ extension NetworkSettingsBuilder {
dnsSettings?.matchDomains = [""] dnsSettings?.matchDomains = [""]
} }
if let domain = dnsDomain {
log.info("DNS: Using domain: \(domain)")
dnsSettings?.domainName = domain
}
let searchDomains = allDNSSearchDomains let searchDomains = allDNSSearchDomains
if !searchDomains.isEmpty { if !searchDomains.isEmpty {
log.info("DNS: Using search domains \(searchDomains)") log.info("DNS: Using search domains: \(searchDomains)")
dnsSettings?.domainName = searchDomains.first
dnsSettings?.searchDomains = searchDomains dnsSettings?.searchDomains = searchDomains
if !isGateway { if !isGateway {
dnsSettings?.matchDomains = dnsSettings?.searchDomains dnsSettings?.matchDomains = dnsSettings?.searchDomains

View File

@ -275,6 +275,9 @@ extension OpenVPN {
/// The server name if `dnsProtocol = .tls`. /// The server name if `dnsProtocol = .tls`.
public var dnsTLSServerName: String? public var dnsTLSServerName: String?
/// The main domain name.
public var dnsDomain: String?
/// The search domain. /// The search domain.
@available(*, deprecated, message: "Use searchDomains instead") @available(*, deprecated, message: "Use searchDomains instead")
public var searchDomain: String? { public var searchDomain: String? {
@ -287,7 +290,7 @@ extension OpenVPN {
} }
} }
/// The search domains. The first one is interpreted as the main domain name. /// The search domains.
public var searchDomains: [String]? public var searchDomains: [String]?
/// The Proxy Auto-Configuration (PAC) url. /// The Proxy Auto-Configuration (PAC) url.
@ -370,6 +373,7 @@ extension OpenVPN {
dnsServers: dnsServers, dnsServers: dnsServers,
dnsHTTPSURL: dnsHTTPSURL, dnsHTTPSURL: dnsHTTPSURL,
dnsTLSServerName: dnsTLSServerName, dnsTLSServerName: dnsTLSServerName,
dnsDomain: dnsDomain,
searchDomains: searchDomains, searchDomains: searchDomains,
isProxyEnabled: isProxyEnabled, isProxyEnabled: isProxyEnabled,
httpProxy: httpProxy, httpProxy: httpProxy,
@ -496,6 +500,9 @@ extension OpenVPN {
/// - Seealso: `ConfigurationBuilder.dnsTLSServerName` /// - Seealso: `ConfigurationBuilder.dnsTLSServerName`
public let dnsTLSServerName: String? public let dnsTLSServerName: String?
/// - Seealso: `ConfigurationBuilder.dnsDomain`
public let dnsDomain: String?
/// - Seealso: `ConfigurationBuilder.searchDomains` /// - Seealso: `ConfigurationBuilder.searchDomains`
public let searchDomains: [String]? public let searchDomains: [String]?
@ -619,6 +626,7 @@ extension OpenVPN.Configuration {
builder.dnsServers = dnsServers builder.dnsServers = dnsServers
builder.dnsHTTPSURL = dnsHTTPSURL builder.dnsHTTPSURL = dnsHTTPSURL
builder.dnsTLSServerName = dnsTLSServerName builder.dnsTLSServerName = dnsTLSServerName
builder.dnsDomain = dnsDomain
builder.searchDomains = searchDomains builder.searchDomains = searchDomains
builder.isProxyEnabled = isProxyEnabled builder.isProxyEnabled = isProxyEnabled
builder.httpProxy = httpProxy builder.httpProxy = httpProxy
@ -755,6 +763,9 @@ extension OpenVPN.Configuration {
log.info("\tDNS: not configured") log.info("\tDNS: not configured")
} }
} }
if let dnsDomain = dnsDomain, !dnsDomain.isEmpty {
log.info("\tDNS domain: \(dnsDomain.maskedDescription)")
}
if let searchDomains = searchDomains, !searchDomains.isEmpty { if let searchDomains = searchDomains, !searchDomains.isEmpty {
log.info("\tSearch domains: \(searchDomains.maskedDescription)") log.info("\tSearch domains: \(searchDomains.maskedDescription)")
} }

View File

@ -111,6 +111,8 @@ extension OpenVPN {
static let domain = NSRegularExpression("^dhcp-option +DOMAIN +[^ ]+") 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 proxy = NSRegularExpression("^dhcp-option +PROXY_(HTTPS? +[^ ]+ +\\d+|AUTO_CONFIG_URL +[^ ]+)")
static let proxyBypass = NSRegularExpression("^dhcp-option +PROXY_BYPASS +.+") static let proxyBypass = NSRegularExpression("^dhcp-option +PROXY_BYPASS +.+")
@ -288,6 +290,7 @@ extension OpenVPN {
var optRoutes4: [(String, String, String?)]? // address, netmask, gateway var optRoutes4: [(String, String, String?)]? // address, netmask, gateway
var optRoutes6: [(String, UInt8, String?)]? // destination, prefix, gateway var optRoutes6: [(String, UInt8, String?)]? // destination, prefix, gateway
var optDNSServers: [String]? var optDNSServers: [String]?
var optDomain: String?
var optSearchDomains: [String]? var optSearchDomains: [String]?
var optHTTPProxy: Proxy? var optHTTPProxy: Proxy?
var optHTTPSProxy: Proxy? var optHTTPSProxy: Proxy?
@ -654,6 +657,12 @@ extension OpenVPN {
optDNSServers?.append($0[1]) optDNSServers?.append($0[1])
} }
Regex.domain.enumerateSpacedArguments(in: line) { Regex.domain.enumerateSpacedArguments(in: line) {
guard $0.count == 2 else {
return
}
optDomain = $0[1]
}
Regex.domainSearch.enumerateSpacedArguments(in: line) {
guard $0.count == 2 else { guard $0.count == 2 else {
return return
} }
@ -931,6 +940,7 @@ extension OpenVPN {
} }
sessionBuilder.dnsServers = optDNSServers sessionBuilder.dnsServers = optDNSServers
sessionBuilder.dnsDomain = optDomain
sessionBuilder.searchDomains = optSearchDomains sessionBuilder.searchDomains = optSearchDomains
sessionBuilder.httpProxy = optHTTPProxy sessionBuilder.httpProxy = optHTTPProxy
sessionBuilder.httpsProxy = optHTTPSProxy sessionBuilder.httpsProxy = optHTTPSProxy

View File

@ -64,10 +64,12 @@ class ConfigurationParserTests: XCTestCase {
let lines = [ let lines = [
"dhcp-option DNS 8.8.8.8", "dhcp-option DNS 8.8.8.8",
"dhcp-option DNS6 ffff::1", "dhcp-option DNS6 ffff::1",
"dhcp-option DOMAIN fake-main.net", "dhcp-option DOMAIN first-domain.net",
"dhcp-option DOMAIN main.net", "dhcp-option DOMAIN second-domain.org",
"dhcp-option DOMAIN one.com", "dhcp-option DOMAIN-SEARCH fake-main.net",
"dhcp-option DOMAIN two.com", "dhcp-option DOMAIN-SEARCH main.net",
"dhcp-option DOMAIN-SEARCH one.com",
"dhcp-option DOMAIN-SEARCH two.com",
"dhcp-option PROXY_HTTP 1.2.3.4 8081", "dhcp-option PROXY_HTTP 1.2.3.4 8081",
"dhcp-option PROXY_HTTPS 7.8.9.10 8082", "dhcp-option PROXY_HTTPS 7.8.9.10 8082",
"dhcp-option PROXY_AUTO_CONFIG_URL https://pac/", "dhcp-option PROXY_AUTO_CONFIG_URL https://pac/",
@ -77,6 +79,7 @@ class ConfigurationParserTests: XCTestCase {
let parsed = try! OpenVPN.ConfigurationParser.parsed(fromLines: lines).configuration let parsed = try! OpenVPN.ConfigurationParser.parsed(fromLines: lines).configuration
XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"]) XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"])
XCTAssertEqual(parsed.dnsDomain, "second-domain.org")
XCTAssertEqual(parsed.searchDomains, ["fake-main.net", "main.net", "one.com", "two.com"]) XCTAssertEqual(parsed.searchDomains, ["fake-main.net", "main.net", "one.com", "two.com"])
XCTAssertEqual(parsed.httpProxy?.address, "1.2.3.4") XCTAssertEqual(parsed.httpProxy?.address, "1.2.3.4")
XCTAssertEqual(parsed.httpProxy?.port, 8081) XCTAssertEqual(parsed.httpProxy?.port, 8081)