Parse search domain from dhcp-option DOMAIN

This commit is contained in:
Davide De Rosa 2019-04-03 12:50:43 +02:00
parent 4dd8ff3485
commit 370e68aa3f
3 changed files with 23 additions and 3 deletions

View File

@ -535,7 +535,13 @@ extension TunnelKitProvider: SessionProxyDelegate {
ipv6Settings?.excludedRoutes = [] ipv6Settings?.excludedRoutes = []
} }
let dnsSettings = NEDNSSettings(servers: cfg.sessionConfiguration.dnsServers ?? reply.options.dnsServers) let dnsServers = cfg.sessionConfiguration.dnsServers ?? reply.options.dnsServers
let searchDomain = cfg.sessionConfiguration.searchDomain ?? reply.options.searchDomain
let dnsSettings = NEDNSSettings(servers: dnsServers)
dnsSettings.domainName = searchDomain
if let searchDomain = searchDomain {
dnsSettings.searchDomains = [searchDomain]
}
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: remoteAddress) let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: remoteAddress)
newSettings.ipv4Settings = ipv4Settings newSettings.ipv4Settings = ipv4Settings

View File

@ -87,6 +87,8 @@ public struct OptionsBundle {
static let dns = NSRegularExpression("^dhcp-option +DNS6? +[\\d\\.a-fA-F:]+") static let dns = NSRegularExpression("^dhcp-option +DNS6? +[\\d\\.a-fA-F:]+")
static let domain = NSRegularExpression("^dhcp-option +DOMAIN +[^ ]+")
// MARK: Unsupported // MARK: Unsupported
// static let fragment = NSRegularExpression("^fragment +\\d+") // static let fragment = NSRegularExpression("^fragment +\\d+")
@ -181,6 +183,9 @@ public struct OptionsBundle {
/// The DNS servers. /// The DNS servers.
public let dnsServers: [String] public let dnsServers: [String]
/// The search domain.
public let searchDomain: String?
/** /**
Parses options from an array of lines. Parses options from an array of lines.
@ -225,6 +230,7 @@ public struct OptionsBundle {
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 optSearchDomain: String?
log.verbose("Configuration file:") log.verbose("Configuration file:")
for line in lines { for line in lines {
@ -495,6 +501,12 @@ public struct OptionsBundle {
} }
optDNSServers.append($0[1]) optDNSServers.append($0[1])
} }
Regex.domain.enumerateArguments(in: line) {
guard $0.count == 2 else {
return
}
optSearchDomain = $0[1]
}
// //
@ -654,6 +666,7 @@ public struct OptionsBundle {
} }
dnsServers = optDNSServers dnsServers = optDNSServers
searchDomain = optSearchDomain
} }
private static func normalizeEncryptedPEMBlock(block: inout [String]) { private static func normalizeEncryptedPEMBlock(block: inout [String]) {

View File

@ -51,11 +51,12 @@ class OptionsBundleTests: XCTestCase {
} }
func testDHCPOption() throws { func testDHCPOption() throws {
let lines = base + ["dhcp-option DNS 8.8.8.8", "dhcp-option DNS6 ffff::1"] let lines = base + ["dhcp-option DNS 8.8.8.8", "dhcp-option DNS6 ffff::1", "dhcp-option DOMAIN example.com"]
XCTAssertNoThrow(try OptionsBundle(from: lines)) XCTAssertNoThrow(try OptionsBundle(from: lines))
let parsed = try! OptionsBundle(from: lines) let parsed = try! OptionsBundle(from: lines)
XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"]) XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"])
XCTAssertEqual(parsed.searchDomain, "example.com")
} }
func testConnectionBlock() throws { func testConnectionBlock() throws {