Parse dhcp-option PROXY_HTTP* into Configuration

This commit is contained in:
Davide De Rosa 2019-04-12 07:47:27 +02:00
parent 4af8305f7a
commit 5fb70b5bab
3 changed files with 91 additions and 5 deletions

View File

@ -92,12 +92,14 @@ public class ConfigurationParser {
static let domain = NSRegularExpression("^dhcp-option +DOMAIN +[^ ]+")
static let proxy = NSRegularExpression("^dhcp-option +PROXY_(HTTPS?|BYPASS) +[^ ]+ +\\d+")
// MARK: Unsupported
// static let fragment = NSRegularExpression("^fragment +\\d+")
static let fragment = NSRegularExpression("^fragment")
static let proxy = NSRegularExpression("^\\w+-proxy")
static let connectionProxy = NSRegularExpression("^\\w+-proxy")
static let externalFiles = NSRegularExpression("^(ca|cert|key|tls-auth|tls-crypt) ")
@ -193,6 +195,8 @@ public class ConfigurationParser {
var optRoutes6: [(String, UInt8, String?)] = [] // destination, prefix, gateway
var optDNSServers: [String] = []
var optSearchDomain: String?
var optHTTPProxy: Proxy?
var optHTTPSProxy: Proxy?
log.verbose("Configuration file:")
for line in lines {
@ -215,7 +219,7 @@ public class ConfigurationParser {
Regex.fragment.enumerateComponents(in: line) { (_) in
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "fragment")
}
Regex.proxy.enumerateComponents(in: line) { (_) in
Regex.connectionProxy.enumerateComponents(in: line) { (_) in
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "proxy: \"\(line)\"")
}
Regex.externalFiles.enumerateComponents(in: line) { (_) in
@ -469,6 +473,21 @@ public class ConfigurationParser {
}
optSearchDomain = $0[1]
}
Regex.proxy.enumerateArguments(in: line) {
guard $0.count == 3, let port = UInt16($0[2]) else {
return
}
switch $0[0] {
case "PROXY_HTTPS":
optHTTPSProxy = Proxy($0[1], port)
case "PROXY_HTTP":
optHTTPProxy = Proxy($0[1], port)
default:
break
}
}
//
@ -631,6 +650,8 @@ public class ConfigurationParser {
sessionBuilder.dnsServers = optDNSServers
sessionBuilder.searchDomain = optSearchDomain
sessionBuilder.httpProxy = optHTTPProxy
sessionBuilder.httpsProxy = optHTTPSProxy
//

View File

@ -215,6 +215,12 @@ extension SessionProxy {
/// The search domain.
public var searchDomain: String?
/// The HTTP proxy.
public var httpProxy: Proxy?
/// The HTTPS proxy.
public var httpsProxy: Proxy?
/// :nodoc:
public init() {
}
@ -246,7 +252,9 @@ extension SessionProxy {
ipv4: ipv4,
ipv6: ipv6,
dnsServers: dnsServers,
searchDomain: searchDomain
searchDomain: searchDomain,
httpProxy: httpProxy,
httpsProxy: httpsProxy
)
}
@ -334,6 +342,12 @@ extension SessionProxy {
/// - Seealso: `SessionProxy.ConfigurationBuilder.searchDomain`
public let searchDomain: String?
/// - Seealso: `SessionProxy.ConfigurationBuilder.httpProxy`
public var httpProxy: Proxy?
/// - Seealso: `SessionProxy.ConfigurationBuilder.httpsProxy`
public var httpsProxy: Proxy?
// MARK: Shortcuts
/// :nodoc:
@ -384,6 +398,8 @@ extension SessionProxy.Configuration {
builder.ipv6 = ipv6
builder.dnsServers = dnsServers
builder.searchDomain = searchDomain
builder.httpProxy = httpProxy
builder.httpsProxy = httpsProxy
return builder
}
}
@ -486,6 +502,45 @@ public struct IPv6Settings: Codable, CustomStringConvertible {
}
}
/// Encapsulate a proxy setting.
public struct Proxy: Codable, RawRepresentable, CustomStringConvertible {
/// The proxy address.
public let address: String
/// The proxy port.
public let port: UInt16
/// :nodoc:
public init(_ address: String, _ port: UInt16) {
self.address = address
self.port = port
}
// MARK: RawRepresentable
/// :nodoc:
public var rawValue: String {
return "\(address):\(port)"
}
/// :nodoc:
public init?(rawValue: String) {
let comps = rawValue.components(separatedBy: ":")
guard comps.count == 2, let port = UInt16(comps[1]) else {
return nil
}
self.init(comps[0], port)
}
// MARK: CustomStringConvertible
/// :nodoc:
public var description: String {
return rawValue
}
}
/// :nodoc:
extension EndpointProtocol: Codable {
public init(from decoder: Decoder) throws {

View File

@ -53,12 +53,22 @@ class ConfigurationParserTests: XCTestCase {
}
func testDHCPOption() throws {
let lines = base + ["dhcp-option DNS 8.8.8.8", "dhcp-option DNS6 ffff::1", "dhcp-option DOMAIN example.com"]
let lines = base + [
"dhcp-option DNS 8.8.8.8",
"dhcp-option DNS6 ffff::1",
"dhcp-option DOMAIN example.com",
"dhcp-option PROXY_HTTP 1.2.3.4 8081",
"dhcp-option PROXY_HTTPS 7.8.9.10 8082"
]
XCTAssertNoThrow(try ConfigurationParser.parsed(fromLines: lines))
let parsed = try! ConfigurationParser.parsed(fromLines: lines).configuration
XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"])
XCTAssertEqual(parsed.searchDomain, "example.com")
XCTAssertEqual(parsed.httpProxy?.address, "1.2.3.4")
XCTAssertEqual(parsed.httpProxy?.port, 8081)
XCTAssertEqual(parsed.httpsProxy?.address, "7.8.9.10")
XCTAssertEqual(parsed.httpsProxy?.port, 8082)
}
func testConnectionBlock() throws {