Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #56 from keeshux/override-dns-servers
Browse files Browse the repository at this point in the history
Override DNS servers
  • Loading branch information
keeshux authored Feb 23, 2019
2 parents 650f0d5 + e12240f commit 2373dc8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Override DNS servers client side. [#56](https://github.com/keeshux/tunnelkit/pull/56)

### Fixed

- Compiling errors in demo target.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ extension TunnelKitProvider {
tlsWrap: nil,
keepAliveInterval: nil,
renegotiatesAfter: nil,
usesPIAPatches: nil
usesPIAPatches: nil,
dnsServers: nil
),
shouldDebug: false,
debugLogKey: nil,
Expand Down
2 changes: 1 addition & 1 deletion TunnelKit/Sources/AppExtension/TunnelKitProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ extension TunnelKitProvider: SessionProxyDelegate {
ipv6Settings?.excludedRoutes = []
}

let dnsSettings = NEDNSSettings(servers: reply.dnsServers)
let dnsSettings = NEDNSSettings(servers: cfg.sessionConfiguration.dnsServers ?? reply.dnsServers)

let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: remoteAddress)
newSettings.ipv4Settings = ipv4Settings
Expand Down
14 changes: 14 additions & 0 deletions TunnelKit/Sources/Core/ConfigurationParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public class ConfigurationParser {

static let blockEnd = NSRegularExpression("^<\\/[\\w\\-]+>")

static let dnsRegexp = NSRegularExpression("dhcp-option DNS6? [\\d\\.a-fA-F:]+")

// unsupported

// static let fragment = NSRegularExpression("^fragment +\\d+")
Expand Down Expand Up @@ -143,6 +145,7 @@ public class ConfigurationParser {
var tlsStrategy: SessionProxy.TLSWrap.Strategy?
var tlsKeyLines: [Substring]?
var tlsWrap: SessionProxy.TLSWrap?
var dnsServers: [String]?

var currentBlockName: String?
var currentBlock: [String] = []
Expand Down Expand Up @@ -316,6 +319,16 @@ public class ConfigurationParser {
}
renegotiateAfterSeconds = TimeInterval(arg)
}
Regex.dnsRegexp.enumerateArguments(in: line) {
isHandled = true
guard $0.count == 2 else {
return
}
if dnsServers == nil {
dnsServers = []
}
dnsServers?.append($0[1])
}
Regex.fragment.enumerateArguments(in: line) { (_) in
unsupportedError = ParsingError.unsupportedConfiguration(option: "fragment")
}
Expand Down Expand Up @@ -388,6 +401,7 @@ public class ConfigurationParser {
sessionBuilder.clientKey = clientKey
sessionBuilder.keepAliveInterval = keepAliveSeconds
sessionBuilder.renegotiatesAfter = renegotiateAfterSeconds
sessionBuilder.dnsServers = dnsServers

return ParsingResult(
url: originalURL,
Expand Down
14 changes: 12 additions & 2 deletions TunnelKit/Sources/Core/SessionProxy+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ extension SessionProxy {
/// Server is patched for the PIA VPN provider.
public var usesPIAPatches: Bool?

/// Optionally override the server DNS entries.
public var dnsServers: [String]?

/// :nodoc:
public init(ca: CryptoContainer) {
cipher = .aes128cbc
Expand All @@ -177,6 +180,7 @@ extension SessionProxy {
keepAliveInterval = nil
renegotiatesAfter = nil
usesPIAPatches = false
dnsServers = nil
}

/**
Expand All @@ -195,7 +199,8 @@ extension SessionProxy {
tlsWrap: tlsWrap,
keepAliveInterval: keepAliveInterval,
renegotiatesAfter: renegotiatesAfter,
usesPIAPatches: usesPIAPatches
usesPIAPatches: usesPIAPatches,
dnsServers: dnsServers
)
}
}
Expand Down Expand Up @@ -233,6 +238,9 @@ extension SessionProxy {
/// - Seealso: `SessionProxy.ConfigurationBuilder.usesPIAPatches`
public let usesPIAPatches: Bool?

/// - Seealso: `SessionProxy.ConfigurationBuilder.dnsServers`
public let dnsServers: [String]?

/**
Returns a `SessionProxy.ConfigurationBuilder` to use this configuration as a starting point for a new one.

Expand All @@ -249,6 +257,7 @@ extension SessionProxy {
builder.keepAliveInterval = keepAliveInterval
builder.renegotiatesAfter = renegotiatesAfter
builder.usesPIAPatches = usesPIAPatches
builder.dnsServers = dnsServers
return builder
}

Expand All @@ -265,7 +274,8 @@ extension SessionProxy {
(lhs.compressionFraming == rhs.compressionFraming) &&
(lhs.keepAliveInterval == rhs.keepAliveInterval) &&
(lhs.renegotiatesAfter == rhs.renegotiatesAfter) &&
(lhs.usesPIAPatches == rhs.usesPIAPatches)
(lhs.usesPIAPatches == rhs.usesPIAPatches) &&
(lhs.dnsServers == rhs.dnsServers)
}
}
}
12 changes: 10 additions & 2 deletions TunnelKitTests/ConfigurationParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import XCTest
import TunnelKit

class ConfigurationParserTests: XCTestCase {
let base: [String] = ["<ca>", "</ca>", "remote 1.2.3.4"]

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
Expand Down Expand Up @@ -55,8 +57,6 @@ class ConfigurationParserTests: XCTestCase {
}

func testCompression() throws {
let base: [String] = ["<ca>", "</ca>", "remote 1.2.3.4"]

XCTAssertNotNil(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo"]).warning)
XCTAssertNoThrow(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo no"]))
XCTAssertThrowsError(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo yes"]))
Expand All @@ -65,6 +65,14 @@ class ConfigurationParserTests: XCTestCase {
XCTAssertThrowsError(try ConfigurationParser.parsed(fromLines: base + ["compress lzo"]))
}

func testDHCPOption() throws {
let lines = base + ["dhcp-option DNS 8.8.8.8", "dhcp-option DNS6 ffff::1"]
XCTAssertNoThrow(try ConfigurationParser.parsed(fromLines: lines))

let parsed = try! ConfigurationParser.parsed(fromLines: lines)
XCTAssertEqual(parsed.configuration.dnsServers, ["8.8.8.8", "ffff::1"])
}

private func url(withName name: String) -> URL {
return Bundle(for: ConfigurationParserTests.self).url(forResource: name, withExtension: "ovpn")!
}
Expand Down

0 comments on commit 2373dc8

Please sign in to comment.