2018-11-10 09:45:00 +00:00
|
|
|
//
|
|
|
|
// ConfigurationParser.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/5/18.
|
2019-03-09 10:44:18 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2018-11-10 09:45:00 +00:00
|
|
|
//
|
|
|
|
// https://github.com/keeshux
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SwiftyBeaver
|
2019-03-18 22:33:36 +00:00
|
|
|
import __TunnelKitNative
|
2018-11-10 09:45:00 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/// Provides methods to parse a `SessionProxy.Configuration` from an .ovpn configuration file.
|
2018-11-10 09:45:00 +00:00
|
|
|
public class ConfigurationParser {
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// Error raised by the parser, with details about the line that triggered it.
|
2018-11-10 09:45:00 +00:00
|
|
|
public enum ParsingError: Error {
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// The file misses a required option.
|
2018-11-10 09:45:00 +00:00
|
|
|
case missingConfiguration(option: String)
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/// The file includes an unsupported option.
|
2018-11-10 09:45:00 +00:00
|
|
|
case unsupportedConfiguration(option: String)
|
2019-03-25 17:30:52 +00:00
|
|
|
|
|
|
|
/// Passphrase required to decrypt private keys.
|
|
|
|
case encryptionPassphrase
|
2019-03-25 18:24:35 +00:00
|
|
|
|
|
|
|
/// Encryption passphrase is incorrect or key is corrupt.
|
|
|
|
case unableToDecrypt(error: Error)
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// Result of the parser.
|
2018-11-10 09:45:00 +00:00
|
|
|
public struct ParsingResult {
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// Original URL of the configuration file, if parsed from an URL.
|
2018-11-10 09:45:00 +00:00
|
|
|
public let url: URL?
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// The main endpoint hostname.
|
2018-11-10 09:45:00 +00:00
|
|
|
public let hostname: String
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/// The list of `EndpointProtocol` to which the client can connect to.
|
2018-11-10 09:45:00 +00:00
|
|
|
public let protocols: [EndpointProtocol]
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// The overall parsed `SessionProxy.Configuration`.
|
2018-11-10 09:45:00 +00:00
|
|
|
public let configuration: SessionProxy.Configuration
|
2018-11-10 10:24:23 +00:00
|
|
|
|
|
|
|
/// The lines of the configuration file stripped of any sensitive data. Lines that
|
|
|
|
/// the parser does not recognize are discarded in the first place.
|
|
|
|
///
|
|
|
|
/// - Seealso: `ConfigurationParser.parsed(...)`
|
2018-11-10 09:45:00 +00:00
|
|
|
public let strippedLines: [String]?
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/// Holds an optional `ParsingError` that didn't block the parser, but it would be worth taking care of.
|
2018-11-10 09:45:00 +00:00
|
|
|
public let warning: ParsingError?
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Regex {
|
|
|
|
static let proto = NSRegularExpression("^proto +(udp6?|tcp6?)")
|
|
|
|
|
|
|
|
static let port = NSRegularExpression("^port +\\d+")
|
|
|
|
|
|
|
|
static let remote = NSRegularExpression("^remote +[^ ]+( +\\d+)?( +(udp6?|tcp6?))?")
|
|
|
|
|
|
|
|
static let cipher = NSRegularExpression("^cipher +[\\w\\-]+")
|
|
|
|
|
|
|
|
static let auth = NSRegularExpression("^auth +[\\w\\-]+")
|
|
|
|
|
|
|
|
static let compLZO = NSRegularExpression("^comp-lzo.*")
|
|
|
|
|
|
|
|
static let compress = NSRegularExpression("^compress.*")
|
|
|
|
|
|
|
|
static let ping = NSRegularExpression("^ping +\\d+")
|
|
|
|
|
|
|
|
static let renegSec = NSRegularExpression("^reneg-sec +\\d+")
|
|
|
|
|
|
|
|
static let keyDirection = NSRegularExpression("^key-direction +\\d")
|
|
|
|
|
2019-02-23 10:51:37 +00:00
|
|
|
static let eku = NSRegularExpression("^remote-cert-tls +server")
|
|
|
|
|
2018-11-10 09:45:00 +00:00
|
|
|
static let blockBegin = NSRegularExpression("^<[\\w\\-]+>")
|
|
|
|
|
|
|
|
static let blockEnd = NSRegularExpression("^<\\/[\\w\\-]+>")
|
|
|
|
|
2019-02-23 10:51:37 +00:00
|
|
|
static let dns = NSRegularExpression("^dhcp-option +DNS6? +[\\d\\.a-fA-F:]+")
|
2019-01-05 21:26:20 +00:00
|
|
|
|
2019-03-25 09:19:19 +00:00
|
|
|
static let remoteRandom = NSRegularExpression("^remote-random")
|
|
|
|
|
2018-11-10 09:45:00 +00:00
|
|
|
// unsupported
|
|
|
|
|
|
|
|
// static let fragment = NSRegularExpression("^fragment +\\d+")
|
|
|
|
static let fragment = NSRegularExpression("^fragment")
|
|
|
|
|
|
|
|
static let proxy = NSRegularExpression("^\\w+-proxy")
|
|
|
|
|
|
|
|
static let externalFiles = NSRegularExpression("^(ca|cert|key|tls-auth|tls-crypt) ")
|
2019-03-04 16:23:51 +00:00
|
|
|
|
|
|
|
static let connection = NSRegularExpression("^<connection>")
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/**
|
|
|
|
Parses an .ovpn file from an URL.
|
|
|
|
|
|
|
|
- Parameter url: The URL of the configuration file.
|
2019-03-25 14:51:43 +00:00
|
|
|
- Parameter passphrase: The optional passphrase for encrypted data.
|
2018-11-10 10:24:23 +00:00
|
|
|
- Parameter returnsStripped: When `true`, stores the stripped file into `ParsingResult.strippedLines`. Defaults to `false`.
|
|
|
|
- Returns: The `ParsingResult` outcome of the parsing.
|
|
|
|
- Throws: `ParsingError` if the configuration file is wrong or incomplete.
|
|
|
|
*/
|
2019-03-25 14:51:43 +00:00
|
|
|
public static func parsed(fromURL url: URL, passphrase: String? = nil, returnsStripped: Bool = false) throws -> ParsingResult {
|
2018-11-10 09:45:00 +00:00
|
|
|
let lines = try String(contentsOf: url).trimmedLines()
|
2019-03-25 14:51:43 +00:00
|
|
|
return try parsed(fromLines: lines, passphrase: passphrase, originalURL: url, returnsStripped: returnsStripped)
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 10:24:23 +00:00
|
|
|
/**
|
|
|
|
Parses an .ovpn file as an array of lines.
|
|
|
|
|
|
|
|
- Parameter lines: The array of lines holding the configuration.
|
2019-03-25 14:51:43 +00:00
|
|
|
- Parameter passphrase: The optional passphrase for encrypted data.
|
2019-03-25 14:46:15 +00:00
|
|
|
- Parameter originalURL: The optional original URL of the configuration file.
|
2018-11-10 10:24:23 +00:00
|
|
|
- Parameter returnsStripped: When `true`, stores the stripped file into `ParsingResult.strippedLines`. Defaults to `false`.
|
|
|
|
- Returns: The `ParsingResult` outcome of the parsing.
|
|
|
|
- Throws: `ParsingError` if the configuration file is wrong or incomplete.
|
|
|
|
*/
|
2019-03-25 14:51:43 +00:00
|
|
|
public static func parsed(fromLines lines: [String], passphrase: String? = nil, originalURL: URL? = nil, returnsStripped: Bool = false) throws -> ParsingResult {
|
2018-11-10 09:45:00 +00:00
|
|
|
var strippedLines: [String]? = returnsStripped ? [] : nil
|
|
|
|
var warning: ParsingError? = nil
|
|
|
|
|
|
|
|
var defaultProto: SocketType?
|
|
|
|
var defaultPort: UInt16?
|
|
|
|
var remotes: [(String, UInt16?, SocketType?)] = []
|
|
|
|
|
|
|
|
var cipher: SessionProxy.Cipher?
|
|
|
|
var digest: SessionProxy.Digest?
|
|
|
|
var compressionFraming: SessionProxy.CompressionFraming = .disabled
|
2019-03-19 11:04:42 +00:00
|
|
|
var compressionAlgorithm: SessionProxy.CompressionAlgorithm = .disabled
|
2018-11-10 09:45:00 +00:00
|
|
|
var optCA: CryptoContainer?
|
|
|
|
var clientCertificate: CryptoContainer?
|
|
|
|
var clientKey: CryptoContainer?
|
2019-02-23 10:51:37 +00:00
|
|
|
var checksEKU = false
|
2018-11-10 09:45:00 +00:00
|
|
|
var keepAliveSeconds: TimeInterval?
|
|
|
|
var renegotiateAfterSeconds: TimeInterval?
|
|
|
|
var keyDirection: StaticKey.Direction?
|
|
|
|
var tlsStrategy: SessionProxy.TLSWrap.Strategy?
|
|
|
|
var tlsKeyLines: [Substring]?
|
|
|
|
var tlsWrap: SessionProxy.TLSWrap?
|
2019-01-05 21:26:20 +00:00
|
|
|
var dnsServers: [String]?
|
2019-03-25 09:19:19 +00:00
|
|
|
var randomizeEndpoint = false
|
2018-11-10 09:45:00 +00:00
|
|
|
|
|
|
|
var currentBlockName: String?
|
|
|
|
var currentBlock: [String] = []
|
|
|
|
var unsupportedError: ParsingError? = nil
|
|
|
|
|
|
|
|
log.verbose("Configuration file:")
|
|
|
|
for line in lines {
|
|
|
|
log.verbose(line)
|
|
|
|
|
|
|
|
var isHandled = false
|
|
|
|
var strippedLine = line
|
|
|
|
defer {
|
|
|
|
if isHandled {
|
|
|
|
strippedLines?.append(strippedLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:23:51 +00:00
|
|
|
// check blocks first
|
|
|
|
Regex.connection.enumerateComponents(in: line) { (_) in
|
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "<connection> blocks")
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:23:51 +00:00
|
|
|
if unsupportedError == nil {
|
2019-04-02 17:07:48 +00:00
|
|
|
if currentBlockName == nil {
|
|
|
|
Regex.blockBegin.enumerateComponents(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
let tag = $0.first!
|
|
|
|
let from = tag.index(after: tag.startIndex)
|
|
|
|
let to = tag.index(before: tag.endIndex)
|
|
|
|
|
|
|
|
currentBlockName = String(tag[from..<to])
|
|
|
|
currentBlock = []
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
2019-03-04 16:23:51 +00:00
|
|
|
Regex.blockEnd.enumerateComponents(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
let tag = $0.first!
|
|
|
|
let from = tag.index(tag.startIndex, offsetBy: 2)
|
|
|
|
let to = tag.index(before: tag.endIndex)
|
|
|
|
|
|
|
|
let blockName = String(tag[from..<to])
|
|
|
|
guard blockName == currentBlockName else {
|
|
|
|
return
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
|
2019-03-04 16:23:51 +00:00
|
|
|
// first is opening tag
|
|
|
|
currentBlock.removeFirst()
|
|
|
|
switch blockName {
|
|
|
|
case "ca":
|
|
|
|
optCA = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
|
|
|
case "cert":
|
|
|
|
clientCertificate = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
|
|
|
case "key":
|
2019-03-25 14:51:43 +00:00
|
|
|
let isEncrypted = normalizeEncryptedPEMBlock(block: ¤tBlock)
|
2019-03-04 16:23:51 +00:00
|
|
|
let container = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
2019-03-25 14:51:43 +00:00
|
|
|
if isEncrypted {
|
|
|
|
guard let passphrase = passphrase else {
|
2019-03-25 17:30:52 +00:00
|
|
|
unsupportedError = ParsingError.encryptionPassphrase
|
2019-03-25 14:51:43 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
clientKey = try container.decrypted(with: passphrase)
|
|
|
|
} catch let e {
|
2019-03-25 18:24:35 +00:00
|
|
|
unsupportedError = ParsingError.unableToDecrypt(error: e)
|
2019-03-25 14:51:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clientKey = container
|
2019-03-04 16:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "tls-auth":
|
|
|
|
tlsKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
tlsStrategy = .auth
|
|
|
|
|
|
|
|
case "tls-crypt":
|
|
|
|
tlsKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
tlsStrategy = .crypt
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
2019-03-04 16:23:51 +00:00
|
|
|
currentBlockName = nil
|
|
|
|
currentBlock = []
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let _ = currentBlockName {
|
|
|
|
currentBlock.append(line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-23 10:51:37 +00:00
|
|
|
Regex.eku.enumerateComponents(in: line) { (_) in
|
|
|
|
checksEKU = true
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
Regex.proto.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultProto = SocketType(protoString: str)
|
|
|
|
if defaultProto == nil {
|
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "proto \(str)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.port.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultPort = UInt16(str)
|
|
|
|
}
|
|
|
|
Regex.remote.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let hostname = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var port: UInt16?
|
|
|
|
var proto: SocketType?
|
|
|
|
var strippedComponents = ["remote", "<hostname>"]
|
|
|
|
if $0.count > 1 {
|
|
|
|
port = UInt16($0[1])
|
|
|
|
strippedComponents.append($0[1])
|
|
|
|
}
|
|
|
|
if $0.count > 2 {
|
|
|
|
proto = SocketType(protoString: $0[2])
|
|
|
|
strippedComponents.append($0[2])
|
|
|
|
}
|
|
|
|
remotes.append((hostname, port, proto))
|
|
|
|
|
|
|
|
// replace private data
|
|
|
|
strippedLine = strippedComponents.joined(separator: " ")
|
|
|
|
}
|
|
|
|
Regex.cipher.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cipher = SessionProxy.Cipher(rawValue: rawValue.uppercased())
|
|
|
|
if cipher == nil {
|
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "cipher \(rawValue)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.auth.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
digest = SessionProxy.Digest(rawValue: rawValue.uppercased())
|
|
|
|
if digest == nil {
|
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "auth \(rawValue)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.compLZO.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
compressionFraming = .compLZO
|
|
|
|
|
2019-03-19 11:04:42 +00:00
|
|
|
if !LZOIsSupported() {
|
|
|
|
guard let arg = $0.first else {
|
|
|
|
warning = warning ?? .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard arg == "no" else {
|
|
|
|
unsupportedError = .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let arg = $0.first
|
|
|
|
compressionAlgorithm = (arg == "no") ? .disabled : .LZO
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.compress.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
compressionFraming = .compress
|
|
|
|
|
2019-03-19 22:08:38 +00:00
|
|
|
if !LZOIsSupported() {
|
|
|
|
guard $0.isEmpty else {
|
|
|
|
unsupportedError = .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let arg = $0.first {
|
|
|
|
compressionAlgorithm = (arg == "lzo") ? .LZO : .other
|
|
|
|
} else {
|
|
|
|
compressionAlgorithm = .disabled
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.keyDirection.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first, let value = Int(arg) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyDirection = StaticKey.Direction(rawValue: value)
|
|
|
|
}
|
|
|
|
Regex.ping.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keepAliveSeconds = TimeInterval(arg)
|
|
|
|
}
|
|
|
|
Regex.renegSec.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
renegotiateAfterSeconds = TimeInterval(arg)
|
|
|
|
}
|
2019-02-23 10:51:37 +00:00
|
|
|
Regex.dns.enumerateArguments(in: line) {
|
2019-01-05 21:26:20 +00:00
|
|
|
isHandled = true
|
|
|
|
guard $0.count == 2 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dnsServers == nil {
|
|
|
|
dnsServers = []
|
|
|
|
}
|
|
|
|
dnsServers?.append($0[1])
|
|
|
|
}
|
2019-03-25 09:19:19 +00:00
|
|
|
Regex.remoteRandom.enumerateComponents(in: line) { (_) in
|
|
|
|
randomizeEndpoint = true
|
|
|
|
}
|
2019-03-04 16:23:51 +00:00
|
|
|
Regex.fragment.enumerateComponents(in: line) { (_) in
|
2018-11-10 09:45:00 +00:00
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "fragment")
|
|
|
|
}
|
2019-03-04 16:23:51 +00:00
|
|
|
Regex.proxy.enumerateComponents(in: line) { (_) in
|
2018-11-10 09:45:00 +00:00
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "proxy: \"\(line)\"")
|
|
|
|
}
|
2019-03-04 16:23:51 +00:00
|
|
|
Regex.externalFiles.enumerateComponents(in: line) { (_) in
|
2018-11-10 09:45:00 +00:00
|
|
|
unsupportedError = ParsingError.unsupportedConfiguration(option: "external file: \"\(line)\"")
|
|
|
|
}
|
|
|
|
if line.contains("mtu") || line.contains("mssfix") {
|
|
|
|
isHandled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if let error = unsupportedError {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let ca = optCA else {
|
|
|
|
throw ParsingError.missingConfiguration(option: "ca")
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: only reads first remote
|
|
|
|
// hostnames = remotes.map { $0.0 }
|
|
|
|
guard !remotes.isEmpty else {
|
|
|
|
throw ParsingError.missingConfiguration(option: "remote")
|
|
|
|
}
|
|
|
|
let hostname = remotes[0].0
|
|
|
|
|
|
|
|
defaultProto = defaultProto ?? .udp
|
|
|
|
defaultPort = defaultPort ?? 1194
|
|
|
|
|
|
|
|
// XXX: reads endpoints from remotes with matching hostname
|
|
|
|
var endpointProtocols: [EndpointProtocol] = []
|
|
|
|
remotes.forEach {
|
|
|
|
guard $0.0 == hostname else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let port = $0.1 ?? defaultPort else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let socketType = $0.2 ?? defaultProto else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endpointProtocols.append(EndpointProtocol(socketType, port))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!endpointProtocols.isEmpty, "Must define an endpoint protocol")
|
|
|
|
|
|
|
|
if let keyLines = tlsKeyLines, let strategy = tlsStrategy {
|
|
|
|
let optKey: StaticKey?
|
|
|
|
switch strategy {
|
|
|
|
case .auth:
|
|
|
|
optKey = StaticKey(lines: keyLines, direction: keyDirection)
|
|
|
|
|
|
|
|
case .crypt:
|
|
|
|
optKey = StaticKey(lines: keyLines, direction: .client)
|
|
|
|
}
|
|
|
|
if let key = optKey {
|
|
|
|
tlsWrap = SessionProxy.TLSWrap(strategy: strategy, key: key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sessionBuilder = SessionProxy.ConfigurationBuilder(ca: ca)
|
|
|
|
sessionBuilder.cipher = cipher ?? .aes128cbc
|
|
|
|
sessionBuilder.digest = digest ?? .sha1
|
|
|
|
sessionBuilder.compressionFraming = compressionFraming
|
2019-03-19 11:04:42 +00:00
|
|
|
sessionBuilder.compressionAlgorithm = compressionAlgorithm
|
2018-11-10 09:45:00 +00:00
|
|
|
sessionBuilder.tlsWrap = tlsWrap
|
|
|
|
sessionBuilder.clientCertificate = clientCertificate
|
|
|
|
sessionBuilder.clientKey = clientKey
|
2019-02-23 10:51:37 +00:00
|
|
|
sessionBuilder.checksEKU = checksEKU
|
2018-11-10 09:45:00 +00:00
|
|
|
sessionBuilder.keepAliveInterval = keepAliveSeconds
|
|
|
|
sessionBuilder.renegotiatesAfter = renegotiateAfterSeconds
|
2019-01-05 21:26:20 +00:00
|
|
|
sessionBuilder.dnsServers = dnsServers
|
2019-03-25 09:19:19 +00:00
|
|
|
sessionBuilder.randomizeEndpoint = randomizeEndpoint
|
2018-11-10 09:45:00 +00:00
|
|
|
|
|
|
|
return ParsingResult(
|
|
|
|
url: originalURL,
|
|
|
|
hostname: hostname,
|
|
|
|
protocols: endpointProtocols,
|
|
|
|
configuration: sessionBuilder.build(),
|
|
|
|
strippedLines: strippedLines,
|
|
|
|
warning: warning
|
|
|
|
)
|
|
|
|
}
|
2019-03-25 14:51:43 +00:00
|
|
|
|
|
|
|
private static func normalizeEncryptedPEMBlock(block: inout [String]) -> Bool {
|
2019-04-02 09:33:54 +00:00
|
|
|
if block.count >= 1 && block[0].contains("ENCRYPTED") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:51:43 +00:00
|
|
|
// XXX: restore blank line after encryption header (easier than tweaking trimmedLines)
|
2019-04-01 22:37:33 +00:00
|
|
|
if block.count >= 3 && block[1].contains("Proc-Type") {
|
2019-03-25 14:51:43 +00:00
|
|
|
block.insert("", at: 3)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension SocketType {
|
|
|
|
init?(protoString: String) {
|
|
|
|
var str = protoString
|
|
|
|
if str.hasSuffix("6") {
|
|
|
|
str.removeLast()
|
|
|
|
}
|
|
|
|
self.init(rawValue: str.uppercased())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension String {
|
|
|
|
func trimmedLines() -> [String] {
|
|
|
|
return components(separatedBy: .newlines).map {
|
|
|
|
$0.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
}.filter {
|
|
|
|
!$0.isEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|