Parse DNS servers from configuration
This commit is contained in:
parent
13c41d80e7
commit
c244b29a8f
|
@ -91,6 +91,8 @@ public class ConfigurationParser {
|
||||||
|
|
||||||
static let blockEnd = NSRegularExpression("^<\\/[\\w\\-]+>")
|
static let blockEnd = NSRegularExpression("^<\\/[\\w\\-]+>")
|
||||||
|
|
||||||
|
static let dnsRegexp = NSRegularExpression("dhcp-option DNS6? [\\d\\.a-fA-F:]+")
|
||||||
|
|
||||||
// unsupported
|
// unsupported
|
||||||
|
|
||||||
// static let fragment = NSRegularExpression("^fragment +\\d+")
|
// static let fragment = NSRegularExpression("^fragment +\\d+")
|
||||||
|
@ -143,6 +145,7 @@ public class ConfigurationParser {
|
||||||
var tlsStrategy: SessionProxy.TLSWrap.Strategy?
|
var tlsStrategy: SessionProxy.TLSWrap.Strategy?
|
||||||
var tlsKeyLines: [Substring]?
|
var tlsKeyLines: [Substring]?
|
||||||
var tlsWrap: SessionProxy.TLSWrap?
|
var tlsWrap: SessionProxy.TLSWrap?
|
||||||
|
var dnsServers: [String]?
|
||||||
|
|
||||||
var currentBlockName: String?
|
var currentBlockName: String?
|
||||||
var currentBlock: [String] = []
|
var currentBlock: [String] = []
|
||||||
|
@ -316,6 +319,16 @@ public class ConfigurationParser {
|
||||||
}
|
}
|
||||||
renegotiateAfterSeconds = TimeInterval(arg)
|
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
|
Regex.fragment.enumerateArguments(in: line) { (_) in
|
||||||
unsupportedError = ParsingError.unsupportedConfiguration(option: "fragment")
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "fragment")
|
||||||
}
|
}
|
||||||
|
@ -388,6 +401,7 @@ public class ConfigurationParser {
|
||||||
sessionBuilder.clientKey = clientKey
|
sessionBuilder.clientKey = clientKey
|
||||||
sessionBuilder.keepAliveInterval = keepAliveSeconds
|
sessionBuilder.keepAliveInterval = keepAliveSeconds
|
||||||
sessionBuilder.renegotiatesAfter = renegotiateAfterSeconds
|
sessionBuilder.renegotiatesAfter = renegotiateAfterSeconds
|
||||||
|
sessionBuilder.dnsServers = dnsServers
|
||||||
|
|
||||||
return ParsingResult(
|
return ParsingResult(
|
||||||
url: originalURL,
|
url: originalURL,
|
||||||
|
|
|
@ -27,6 +27,8 @@ import XCTest
|
||||||
import TunnelKit
|
import TunnelKit
|
||||||
|
|
||||||
class ConfigurationParserTests: XCTestCase {
|
class ConfigurationParserTests: XCTestCase {
|
||||||
|
let base: [String] = ["<ca>", "</ca>", "remote 1.2.3.4"]
|
||||||
|
|
||||||
override func setUp() {
|
override func setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||||
|
@ -55,8 +57,6 @@ class ConfigurationParserTests: XCTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCompression() throws {
|
func testCompression() throws {
|
||||||
let base: [String] = ["<ca>", "</ca>", "remote 1.2.3.4"]
|
|
||||||
|
|
||||||
XCTAssertNotNil(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo"]).warning)
|
XCTAssertNotNil(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo"]).warning)
|
||||||
XCTAssertNoThrow(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo no"]))
|
XCTAssertNoThrow(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo no"]))
|
||||||
XCTAssertThrowsError(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo yes"]))
|
XCTAssertThrowsError(try ConfigurationParser.parsed(fromLines: base + ["comp-lzo yes"]))
|
||||||
|
@ -65,6 +65,14 @@ class ConfigurationParserTests: XCTestCase {
|
||||||
XCTAssertThrowsError(try ConfigurationParser.parsed(fromLines: base + ["compress lzo"]))
|
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 {
|
private func url(withName name: String) -> URL {
|
||||||
return Bundle(for: ConfigurationParserTests.self).url(forResource: name, withExtension: "ovpn")!
|
return Bundle(for: ConfigurationParserTests.self).url(forResource: name, withExtension: "ovpn")!
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue