2018-11-10 09:45:00 +00:00
|
|
|
//
|
|
|
|
// ConfigurationParser.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/5/18.
|
2021-07-22 08:24:24 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2018-11-10 09:45:00 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-11-10 09:45:00 +00:00
|
|
|
//
|
|
|
|
// 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
|
2021-10-25 14:27:27 +00:00
|
|
|
import TunnelKitCore
|
2021-11-07 20:54:05 +00:00
|
|
|
import CTunnelKitCore
|
|
|
|
import __TunnelKitUtils
|
2018-11-10 09:45:00 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
extension OpenVPN {
|
2018-11-10 10:24:23 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/// Provides methods to parse a `Configuration` from an .ovpn configuration file.
|
|
|
|
public class ConfigurationParser {
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// XXX: parsing is very optimistic
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2021-11-07 20:54:05 +00:00
|
|
|
public struct Regex {
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
// MARK: General
|
|
|
|
|
|
|
|
static let cipher = NSRegularExpression("^cipher +[^,\\s]+")
|
|
|
|
|
2021-01-02 23:44:42 +00:00
|
|
|
static let dataCiphers = NSRegularExpression("^(data-ciphers|ncp-ciphers) +[^,\\s]+(:[^,\\s]+)*")
|
|
|
|
|
2021-01-08 18:46:16 +00:00
|
|
|
static let dataCiphersFallback = NSRegularExpression("^data-ciphers-fallback +[^,\\s]+")
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
static let auth = NSRegularExpression("^auth +[\\w\\-]+")
|
|
|
|
|
|
|
|
static let compLZO = NSRegularExpression("^comp-lzo.*")
|
|
|
|
|
|
|
|
static let compress = NSRegularExpression("^compress.*")
|
|
|
|
|
|
|
|
static let keyDirection = NSRegularExpression("^key-direction +\\d")
|
|
|
|
|
|
|
|
static let ping = NSRegularExpression("^ping +\\d+")
|
|
|
|
|
2019-09-30 18:54:29 +00:00
|
|
|
static let pingRestart = NSRegularExpression("^ping-restart +\\d+")
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
static let renegSec = NSRegularExpression("^reneg-sec +\\d+")
|
|
|
|
|
2019-09-03 20:11:53 +00:00
|
|
|
static let xorMask = NSRegularExpression("^scramble +xormask +.$")
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
static let blockBegin = NSRegularExpression("^<[\\w\\-]+>")
|
|
|
|
|
|
|
|
static let blockEnd = NSRegularExpression("^<\\/[\\w\\-]+>")
|
|
|
|
|
|
|
|
// MARK: Client
|
|
|
|
|
2020-02-29 21:47:51 +00:00
|
|
|
static let proto = NSRegularExpression("^proto +(udp[46]?|tcp[46]?)")
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
static let port = NSRegularExpression("^port +\\d+")
|
|
|
|
|
2020-02-29 21:47:51 +00:00
|
|
|
static let remote = NSRegularExpression("^remote +[^ ]+( +\\d+)?( +(udp[46]?|tcp[46]?))?")
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
static let eku = NSRegularExpression("^remote-cert-tls +server")
|
|
|
|
|
|
|
|
static let remoteRandom = NSRegularExpression("^remote-random")
|
|
|
|
|
2020-12-27 22:40:20 +00:00
|
|
|
static let mtu = NSRegularExpression("^tun-mtu +\\d+")
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Server
|
|
|
|
|
2021-11-07 20:54:05 +00:00
|
|
|
public static let authToken = NSRegularExpression("^auth-token +[a-zA-Z0-9/=+]+")
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
static let peerId = NSRegularExpression("^peer-id +[0-9]+")
|
|
|
|
|
|
|
|
// MARK: Routing
|
|
|
|
|
|
|
|
static let topology = NSRegularExpression("^topology +(net30|p2p|subnet)")
|
|
|
|
|
|
|
|
static let ifconfig = NSRegularExpression("^ifconfig +[\\d\\.]+ [\\d\\.]+")
|
|
|
|
|
|
|
|
static let ifconfig6 = NSRegularExpression("^ifconfig-ipv6 +[\\da-fA-F:]+/\\d+ [\\da-fA-F:]+")
|
|
|
|
|
|
|
|
static let route = NSRegularExpression("^route +[\\d\\.]+( +[\\d\\.]+){0,2}")
|
|
|
|
|
|
|
|
static let route6 = NSRegularExpression("^route-ipv6 +[\\da-fA-F:]+/\\d+( +[\\da-fA-F:]+){0,2}")
|
|
|
|
|
|
|
|
static let gateway = NSRegularExpression("^route-gateway +[\\d\\.]+")
|
|
|
|
|
|
|
|
static let dns = NSRegularExpression("^dhcp-option +DNS6? +[\\d\\.a-fA-F:]+")
|
|
|
|
|
|
|
|
static let domain = NSRegularExpression("^dhcp-option +DOMAIN +[^ ]+")
|
|
|
|
|
2020-12-11 16:09:15 +00:00
|
|
|
static let domainSearch = NSRegularExpression("^dhcp-option +DOMAIN-SEARCH +[^ ]+")
|
|
|
|
|
2019-10-21 19:47:45 +00:00
|
|
|
static let proxy = NSRegularExpression("^dhcp-option +PROXY_(HTTPS? +[^ ]+ +\\d+|AUTO_CONFIG_URL +[^ ]+)")
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
static let proxyBypass = NSRegularExpression("^dhcp-option +PROXY_BYPASS +.+")
|
|
|
|
|
|
|
|
static let redirectGateway = NSRegularExpression("^redirect-gateway.*")
|
|
|
|
|
|
|
|
// MARK: Unsupported
|
|
|
|
|
|
|
|
// static let fragment = NSRegularExpression("^fragment +\\d+")
|
|
|
|
static let fragment = NSRegularExpression("^fragment")
|
|
|
|
|
|
|
|
static let connectionProxy = NSRegularExpression("^\\w+-proxy")
|
|
|
|
|
|
|
|
static let externalFiles = NSRegularExpression("^(ca|cert|key|tls-auth|tls-crypt) ")
|
|
|
|
|
|
|
|
static let connection = NSRegularExpression("^<connection>")
|
|
|
|
|
|
|
|
// MARK: Continuation
|
|
|
|
|
|
|
|
static let continuation = NSRegularExpression("^push-continuation [12]")
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
private enum Topology: String {
|
|
|
|
case net30
|
|
|
|
|
|
|
|
case p2p
|
|
|
|
|
|
|
|
case subnet
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
private enum RedirectGateway: String {
|
|
|
|
case def1 // default
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
case noIPv4 = "!ipv4"
|
|
|
|
|
|
|
|
case ipv6
|
2019-04-27 11:43:09 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
case local
|
|
|
|
|
|
|
|
case autolocal
|
|
|
|
|
|
|
|
case blockLocal = "block-local"
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
case bypassDHCP = "bypass-dhcp"
|
|
|
|
|
|
|
|
case bypassDNS = "bypass-dns"
|
|
|
|
}
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/// Result of the parser.
|
|
|
|
public struct Result {
|
2018-11-10 10:24:23 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/// Original URL of the configuration file, if parsed from an URL.
|
|
|
|
public let url: URL?
|
2018-11-10 10:24:23 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/// The overall parsed `Configuration`.
|
|
|
|
public let configuration: Configuration
|
2018-11-10 09:45:00 +00:00
|
|
|
|
2019-05-19 12:04:41 +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(...)`
|
|
|
|
public let strippedLines: [String]?
|
|
|
|
|
|
|
|
/// Holds an optional `ConfigurationError` that didn't block the parser, but it would be worth taking care of.
|
|
|
|
public let warning: ConfigurationError?
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/**
|
|
|
|
Parses an .ovpn file from an URL.
|
|
|
|
|
|
|
|
- Parameter url: The URL of the configuration file.
|
|
|
|
- Parameter passphrase: The optional passphrase for encrypted data.
|
|
|
|
- Parameter returnsStripped: When `true`, stores the stripped file into `Result.strippedLines`. Defaults to `false`.
|
|
|
|
- Returns: The `Result` outcome of the parsing.
|
|
|
|
- Throws: `ConfigurationError` if the configuration file is wrong or incomplete.
|
|
|
|
*/
|
|
|
|
public static func parsed(fromURL url: URL, passphrase: String? = nil, returnsStripped: Bool = false) throws -> Result {
|
|
|
|
let lines = try String(contentsOf: url).trimmedLines()
|
2019-11-20 18:46:52 +00:00
|
|
|
return try parsed(fromLines: lines, isClient: true, passphrase: passphrase, originalURL: url, returnsStripped: returnsStripped)
|
2019-05-19 12:04:41 +00:00
|
|
|
}
|
2019-04-12 05:47:27 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
/**
|
|
|
|
Parses a configuration from an array of lines.
|
|
|
|
|
|
|
|
- Parameter lines: The array of lines holding the configuration.
|
2019-11-20 18:46:52 +00:00
|
|
|
- Parameter isClient: Enables additional checks for client configurations.
|
2019-05-19 12:04:41 +00:00
|
|
|
- Parameter passphrase: The optional passphrase for encrypted data.
|
|
|
|
- Parameter originalURL: The optional original URL of the configuration file.
|
|
|
|
- Parameter returnsStripped: When `true`, stores the stripped file into `Result.strippedLines`. Defaults to `false`.
|
|
|
|
- Returns: The `Result` outcome of the parsing.
|
|
|
|
- Throws: `ConfigurationError` if the configuration file is wrong or incomplete.
|
|
|
|
*/
|
2019-11-20 18:46:52 +00:00
|
|
|
public static func parsed(fromLines lines: [String], isClient: Bool = false, passphrase: String? = nil, originalURL: URL? = nil, returnsStripped: Bool = false) throws -> Result {
|
2019-05-19 12:04:41 +00:00
|
|
|
var optStrippedLines: [String]? = returnsStripped ? [] : nil
|
|
|
|
var optWarning: ConfigurationError?
|
|
|
|
var unsupportedError: ConfigurationError?
|
|
|
|
var currentBlockName: String?
|
|
|
|
var currentBlock: [String] = []
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2021-01-02 23:44:42 +00:00
|
|
|
var optDataCiphers: [Cipher]?
|
2021-01-08 18:46:16 +00:00
|
|
|
var optDataCiphersFallback: Cipher?
|
2019-05-19 12:04:41 +00:00
|
|
|
var optCipher: Cipher?
|
|
|
|
var optDigest: Digest?
|
|
|
|
var optCompressionFraming: CompressionFraming?
|
|
|
|
var optCompressionAlgorithm: CompressionAlgorithm?
|
|
|
|
var optCA: CryptoContainer?
|
|
|
|
var optClientCertificate: CryptoContainer?
|
|
|
|
var optClientKey: CryptoContainer?
|
|
|
|
var optKeyDirection: StaticKey.Direction?
|
|
|
|
var optTLSKeyLines: [Substring]?
|
|
|
|
var optTLSStrategy: TLSWrap.Strategy?
|
|
|
|
var optKeepAliveSeconds: TimeInterval?
|
2019-09-30 18:54:29 +00:00
|
|
|
var optKeepAliveTimeoutSeconds: TimeInterval?
|
2019-05-19 12:04:41 +00:00
|
|
|
var optRenegotiateAfterSeconds: TimeInterval?
|
2019-09-03 20:11:53 +00:00
|
|
|
var optXorMask: UInt8?
|
2019-05-19 12:04:41 +00:00
|
|
|
//
|
|
|
|
var optDefaultProto: SocketType?
|
|
|
|
var optDefaultPort: UInt16?
|
|
|
|
var optRemotes: [(String, UInt16?, SocketType?)] = [] // address, port, socket
|
|
|
|
var optChecksEKU: Bool?
|
|
|
|
var optRandomizeEndpoint: Bool?
|
2020-12-27 22:40:20 +00:00
|
|
|
var optMTU: Int?
|
2019-05-19 12:04:41 +00:00
|
|
|
//
|
|
|
|
var optAuthToken: String?
|
|
|
|
var optPeerId: UInt32?
|
|
|
|
//
|
|
|
|
var optTopology: String?
|
|
|
|
var optIfconfig4Arguments: [String]?
|
|
|
|
var optIfconfig6Arguments: [String]?
|
|
|
|
var optGateway4Arguments: [String]?
|
|
|
|
var optRoutes4: [(String, String, String?)] = [] // address, netmask, gateway
|
|
|
|
var optRoutes6: [(String, UInt8, String?)] = [] // destination, prefix, gateway
|
|
|
|
var optDNSServers: [String]?
|
2020-12-11 16:09:15 +00:00
|
|
|
var optDomain: String?
|
2019-10-25 15:21:34 +00:00
|
|
|
var optSearchDomains: [String]?
|
2019-05-19 12:04:41 +00:00
|
|
|
var optHTTPProxy: Proxy?
|
|
|
|
var optHTTPSProxy: Proxy?
|
2019-10-22 19:03:25 +00:00
|
|
|
var optProxyAutoConfigurationURL: URL?
|
2019-05-19 12:04:41 +00:00
|
|
|
var optProxyBypass: [String]?
|
|
|
|
var optRedirectGateway: Set<RedirectGateway>?
|
|
|
|
|
|
|
|
log.verbose("Configuration file:")
|
|
|
|
for line in lines {
|
|
|
|
log.verbose(line)
|
|
|
|
|
|
|
|
var isHandled = false
|
|
|
|
var strippedLine = line
|
|
|
|
defer {
|
|
|
|
if isHandled {
|
|
|
|
optStrippedLines?.append(strippedLine)
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
// MARK: Unsupported
|
|
|
|
|
|
|
|
// check blocks first
|
|
|
|
Regex.connection.enumerateComponents(in: line) { (_) in
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "<connection> blocks")
|
|
|
|
}
|
|
|
|
Regex.fragment.enumerateComponents(in: line) { (_) in
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "fragment")
|
|
|
|
}
|
|
|
|
Regex.connectionProxy.enumerateComponents(in: line) { (_) in
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "proxy: \"\(line)\"")
|
|
|
|
}
|
|
|
|
Regex.externalFiles.enumerateComponents(in: line) { (_) in
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "external file: \"\(line)\"")
|
|
|
|
}
|
|
|
|
if line.contains("mtu") || line.contains("mssfix") {
|
|
|
|
isHandled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Continuation
|
2019-03-22 10:58:30 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
var isContinuation = false
|
|
|
|
Regex.continuation.enumerateArguments(in: line) {
|
|
|
|
isContinuation = ($0.first == "2")
|
|
|
|
}
|
|
|
|
guard !isContinuation else {
|
2019-05-19 12:19:23 +00:00
|
|
|
throw OpenVPNError.continuationPushReply
|
2019-05-19 12:04:41 +00:00
|
|
|
}
|
2019-03-22 10:58:30 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Inline content
|
|
|
|
|
|
|
|
if unsupportedError == nil {
|
|
|
|
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 = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.blockEnd.enumerateComponents(in: line) {
|
2019-04-04 10:40:28 +00:00
|
|
|
isHandled = true
|
|
|
|
let tag = $0.first!
|
2019-05-19 12:04:41 +00:00
|
|
|
let from = tag.index(tag.startIndex, offsetBy: 2)
|
2019-04-04 10:40:28 +00:00
|
|
|
let to = tag.index(before: tag.endIndex)
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
let blockName = String(tag[from..<to])
|
|
|
|
guard blockName == currentBlockName else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// first is opening tag
|
|
|
|
currentBlock.removeFirst()
|
|
|
|
switch blockName {
|
|
|
|
case "ca":
|
|
|
|
optCA = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
|
|
|
case "cert":
|
|
|
|
optClientCertificate = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
|
|
|
case "key":
|
|
|
|
ConfigurationParser.normalizeEncryptedPEMBlock(block: ¤tBlock)
|
|
|
|
optClientKey = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
|
|
|
case "tls-auth":
|
|
|
|
optTLSKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
optTLSStrategy = .auth
|
|
|
|
|
|
|
|
case "tls-crypt":
|
|
|
|
optTLSKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
optTLSStrategy = .crypt
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currentBlockName = nil
|
2019-04-04 10:40:28 +00:00
|
|
|
currentBlock = []
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
if let _ = currentBlockName {
|
|
|
|
currentBlock.append(line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: General
|
|
|
|
|
|
|
|
Regex.cipher.enumerateArguments(in: line) {
|
2019-04-04 10:40:28 +00:00
|
|
|
isHandled = true
|
2019-05-19 12:04:41 +00:00
|
|
|
guard let rawValue = $0.first else {
|
2019-04-04 10:40:28 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optCipher = Cipher(rawValue: rawValue.uppercased())
|
2021-01-02 23:44:42 +00:00
|
|
|
}
|
|
|
|
Regex.dataCiphers.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let rawCiphers = rawValue.components(separatedBy: ":")
|
|
|
|
optDataCiphers = []
|
|
|
|
rawCiphers.forEach {
|
|
|
|
guard let cipher = Cipher(rawValue: $0.uppercased()) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optDataCiphers?.append(cipher)
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 18:46:16 +00:00
|
|
|
Regex.dataCiphersFallback.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optDataCiphersFallback = Cipher(rawValue: rawValue.uppercased())
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.auth.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optDigest = Digest(rawValue: rawValue.uppercased())
|
|
|
|
if optDigest == nil {
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "auth \(rawValue)")
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.compLZO.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
optCompressionFraming = .compLZO
|
|
|
|
|
2021-10-25 14:27:27 +00:00
|
|
|
if !LZOFactory.isSupported() {
|
2019-05-19 12:04:41 +00:00
|
|
|
guard let arg = $0.first else {
|
|
|
|
optWarning = optWarning ?? .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard arg == "no" else {
|
|
|
|
unsupportedError = .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let arg = $0.first
|
|
|
|
optCompressionAlgorithm = (arg == "no") ? .disabled : .LZO
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.compress.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
optCompressionFraming = .compress
|
|
|
|
|
2021-10-25 14:27:27 +00:00
|
|
|
if !LZOFactory.isSupported() {
|
2019-05-19 12:04:41 +00:00
|
|
|
guard $0.isEmpty else {
|
|
|
|
unsupportedError = .unsupportedConfiguration(option: line)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let arg = $0.first {
|
2021-07-16 16:11:37 +00:00
|
|
|
switch arg {
|
|
|
|
case "lzo":
|
|
|
|
optCompressionAlgorithm = .LZO
|
|
|
|
|
|
|
|
case "stub":
|
|
|
|
optCompressionAlgorithm = .disabled
|
|
|
|
|
|
|
|
case "stub-v2":
|
|
|
|
optCompressionFraming = .compressV2
|
|
|
|
optCompressionAlgorithm = .disabled
|
|
|
|
|
|
|
|
default:
|
|
|
|
optCompressionAlgorithm = .other
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
} else {
|
|
|
|
optCompressionAlgorithm = .disabled
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.keyDirection.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first, let value = Int(arg) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optKeyDirection = StaticKey.Direction(rawValue: value)
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.ping.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
2019-04-04 10:40:28 +00:00
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optKeepAliveSeconds = TimeInterval(arg)
|
|
|
|
}
|
2019-09-30 18:54:29 +00:00
|
|
|
Regex.pingRestart.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optKeepAliveTimeoutSeconds = TimeInterval(arg)
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.renegSec.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let arg = $0.first else {
|
2019-04-04 10:40:28 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optRenegotiateAfterSeconds = TimeInterval(arg)
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-09-03 20:11:53 +00:00
|
|
|
Regex.xorMask.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
if $0.count != 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optXorMask = Character($0[1]).asciiValue
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Client
|
|
|
|
|
|
|
|
Regex.proto.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let str = $0.first else {
|
2019-04-04 10:40:28 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optDefaultProto = SocketType(protoString: str)
|
|
|
|
if optDefaultProto == nil {
|
|
|
|
unsupportedError = ConfigurationError.unsupportedConfiguration(option: "proto \(str)")
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.port.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optDefaultPort = UInt16(str)
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
optRemotes.append((hostname, port, proto))
|
|
|
|
|
|
|
|
// replace private data
|
|
|
|
strippedLine = strippedComponents.joined(separator: " ")
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.eku.enumerateComponents(in: line) { (_) in
|
|
|
|
isHandled = true
|
|
|
|
optChecksEKU = true
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.remoteRandom.enumerateComponents(in: line) { (_) in
|
|
|
|
isHandled = true
|
|
|
|
optRandomizeEndpoint = true
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2020-12-27 22:40:20 +00:00
|
|
|
Regex.mtu.enumerateArguments(in: line) {
|
|
|
|
isHandled = true
|
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optMTU = Int(str)
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Server
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.authToken.enumerateArguments(in: line) {
|
|
|
|
optAuthToken = $0[0]
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.peerId.enumerateArguments(in: line) {
|
|
|
|
optPeerId = UInt32($0[0])
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Routing
|
|
|
|
|
|
|
|
Regex.topology.enumerateArguments(in: line) {
|
|
|
|
optTopology = $0.first
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.ifconfig.enumerateArguments(in: line) {
|
|
|
|
optIfconfig4Arguments = $0
|
2019-04-16 22:42:07 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.ifconfig6.enumerateArguments(in: line) {
|
|
|
|
optIfconfig6Arguments = $0
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.route.enumerateArguments(in: line) {
|
|
|
|
let routeEntryArguments = $0
|
|
|
|
|
|
|
|
let address = routeEntryArguments[0]
|
|
|
|
let mask = (routeEntryArguments.count > 1) ? routeEntryArguments[1] : "255.255.255.255"
|
2019-10-22 11:52:24 +00:00
|
|
|
var gateway = (routeEntryArguments.count > 2) ? routeEntryArguments[2] : nil // defaultGateway4
|
|
|
|
if gateway == "vpn_gateway" {
|
|
|
|
gateway = nil
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optRoutes4.append((address, mask, gateway))
|
2019-04-12 05:47:27 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.route6.enumerateArguments(in: line) {
|
|
|
|
let routeEntryArguments = $0
|
2019-04-13 17:03:27 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
let destinationComponents = routeEntryArguments[0].components(separatedBy: "/")
|
|
|
|
guard destinationComponents.count == 2 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let prefix = UInt8(destinationComponents[1]) else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-12 05:47:27 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
let destination = destinationComponents[0]
|
2019-10-22 11:52:24 +00:00
|
|
|
var gateway = (routeEntryArguments.count > 1) ? routeEntryArguments[1] : nil // defaultGateway6
|
|
|
|
if gateway == "vpn_gateway" {
|
|
|
|
gateway = nil
|
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
optRoutes6.append((destination, prefix, gateway))
|
2019-04-12 05:47:27 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.gateway.enumerateArguments(in: line) {
|
|
|
|
optGateway4Arguments = $0
|
2019-04-13 17:03:27 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
Regex.dns.enumerateArguments(in: line) {
|
|
|
|
guard $0.count == 2 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if optDNSServers == nil {
|
|
|
|
optDNSServers = []
|
|
|
|
}
|
|
|
|
optDNSServers?.append($0[1])
|
|
|
|
}
|
|
|
|
Regex.domain.enumerateArguments(in: line) {
|
2020-12-15 12:57:57 +00:00
|
|
|
guard $0.count == 2 else {
|
2020-12-11 16:09:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
optDomain = $0[1]
|
|
|
|
}
|
|
|
|
Regex.domainSearch.enumerateArguments(in: line) {
|
2019-05-19 12:04:41 +00:00
|
|
|
guard $0.count == 2 else {
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 15:21:34 +00:00
|
|
|
if optSearchDomains == nil {
|
|
|
|
optSearchDomains = []
|
|
|
|
}
|
|
|
|
optSearchDomains?.append($0[1])
|
2019-05-19 12:04:41 +00:00
|
|
|
}
|
|
|
|
Regex.proxy.enumerateArguments(in: line) {
|
2019-10-21 19:47:45 +00:00
|
|
|
if $0.count == 2 {
|
2019-10-22 19:03:25 +00:00
|
|
|
guard let url = URL(string: $0[1]) else {
|
2019-10-21 19:47:45 +00:00
|
|
|
unsupportedError = ConfigurationError.malformed(option: "dhcp-option PROXY_AUTO_CONFIG_URL has malformed URL")
|
2019-10-22 19:03:25 +00:00
|
|
|
return
|
2019-10-21 19:47:45 +00:00
|
|
|
}
|
2019-10-22 19:03:25 +00:00
|
|
|
optProxyAutoConfigurationURL = url
|
2019-10-21 19:47:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
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)
|
2019-10-21 19:47:45 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Regex.proxyBypass.enumerateArguments(in: line) {
|
|
|
|
guard !$0.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
optProxyBypass = $0
|
|
|
|
optProxyBypass?.removeFirst()
|
|
|
|
}
|
|
|
|
Regex.redirectGateway.enumerateArguments(in: line) {
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// redirect IPv4 by default
|
|
|
|
optRedirectGateway = [.def1]
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
for arg in $0 {
|
|
|
|
guard let opt = RedirectGateway(rawValue: arg) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
optRedirectGateway?.insert(opt)
|
2019-04-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-13 17:03:27 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
//
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
if let error = unsupportedError {
|
|
|
|
throw error
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:46:52 +00:00
|
|
|
if isClient {
|
|
|
|
guard let _ = optCA else {
|
|
|
|
throw ConfigurationError.missingConfiguration(option: "ca")
|
|
|
|
}
|
2021-01-02 23:44:42 +00:00
|
|
|
guard optCipher != nil || !(optDataCiphers?.isEmpty ?? false) else {
|
|
|
|
throw ConfigurationError.missingConfiguration(option: "cipher or data-ciphers")
|
2019-11-20 18:46:52 +00:00
|
|
|
}
|
2019-11-20 00:03:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
var sessionBuilder = ConfigurationBuilder()
|
|
|
|
|
|
|
|
// MARK: General
|
|
|
|
|
2021-01-08 18:46:16 +00:00
|
|
|
sessionBuilder.cipher = optDataCiphersFallback ?? optCipher
|
2021-01-02 23:44:42 +00:00
|
|
|
sessionBuilder.dataCiphers = optDataCiphers
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.digest = optDigest
|
|
|
|
sessionBuilder.compressionFraming = optCompressionFraming
|
|
|
|
sessionBuilder.compressionAlgorithm = optCompressionAlgorithm
|
|
|
|
sessionBuilder.ca = optCA
|
|
|
|
sessionBuilder.clientCertificate = optClientCertificate
|
|
|
|
|
|
|
|
if let clientKey = optClientKey, clientKey.isEncrypted {
|
2021-11-07 20:54:05 +00:00
|
|
|
// FIXME: remove dependency on TLSBox
|
2019-05-19 12:04:41 +00:00
|
|
|
guard let passphrase = passphrase else {
|
|
|
|
throw ConfigurationError.encryptionPassphrase
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
do {
|
|
|
|
sessionBuilder.clientKey = try clientKey.decrypted(with: passphrase)
|
|
|
|
} catch let e {
|
|
|
|
throw ConfigurationError.unableToDecrypt(error: e)
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
} else {
|
|
|
|
sessionBuilder.clientKey = optClientKey
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
if let keyLines = optTLSKeyLines, let strategy = optTLSStrategy {
|
|
|
|
let optKey: StaticKey?
|
|
|
|
switch strategy {
|
|
|
|
case .auth:
|
|
|
|
optKey = StaticKey(lines: keyLines, direction: optKeyDirection)
|
|
|
|
|
|
|
|
case .crypt:
|
|
|
|
optKey = StaticKey(lines: keyLines, direction: .client)
|
|
|
|
}
|
|
|
|
if let key = optKey {
|
|
|
|
sessionBuilder.tlsWrap = TLSWrap(strategy: strategy, key: key)
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.keepAliveInterval = optKeepAliveSeconds
|
2019-09-30 18:54:29 +00:00
|
|
|
sessionBuilder.keepAliveTimeout = optKeepAliveTimeoutSeconds
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.renegotiatesAfter = optRenegotiateAfterSeconds
|
|
|
|
|
|
|
|
// MARK: Client
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
optDefaultProto = optDefaultProto ?? .udp
|
|
|
|
optDefaultPort = optDefaultPort ?? 1194
|
|
|
|
if !optRemotes.isEmpty {
|
|
|
|
sessionBuilder.hostname = optRemotes[0].0
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
var fullRemotes: [(String, UInt16, SocketType)] = []
|
|
|
|
let hostname = optRemotes[0].0
|
|
|
|
optRemotes.forEach {
|
|
|
|
guard $0.0 == hostname else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let port = $0.1 ?? optDefaultPort else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let socketType = $0.2 ?? optDefaultProto else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fullRemotes.append((hostname, port, socketType))
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.endpointProtocols = fullRemotes.map { EndpointProtocol($0.2, $0.1) }
|
|
|
|
} else {
|
|
|
|
sessionBuilder.hostname = nil
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.checksEKU = optChecksEKU
|
|
|
|
sessionBuilder.randomizeEndpoint = optRandomizeEndpoint
|
2020-12-27 22:40:20 +00:00
|
|
|
sessionBuilder.mtu = optMTU
|
2019-09-03 20:11:53 +00:00
|
|
|
sessionBuilder.xorMask = optXorMask
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// MARK: Server
|
|
|
|
|
|
|
|
sessionBuilder.authToken = optAuthToken
|
|
|
|
sessionBuilder.peerId = optPeerId
|
|
|
|
|
|
|
|
// MARK: Routing
|
|
|
|
|
|
|
|
//
|
|
|
|
// excerpts from OpenVPN manpage
|
|
|
|
//
|
|
|
|
// "--ifconfig l rn":
|
|
|
|
//
|
|
|
|
// Set TUN/TAP adapter parameters. l is the IP address of the local VPN endpoint. For TUN devices in point-to-point mode, rn is the IP address of
|
|
|
|
// the remote VPN endpoint. For TAP devices, or TUN devices used with --topology subnet, rn is the subnet mask of the virtual network segment which
|
|
|
|
// is being created or connected to.
|
|
|
|
//
|
|
|
|
// "--topology mode":
|
|
|
|
//
|
|
|
|
// Note: Using --topology subnet changes the interpretation of the arguments of --ifconfig to mean "address netmask", no longer "local remote".
|
|
|
|
//
|
|
|
|
if let ifconfig4Arguments = optIfconfig4Arguments {
|
|
|
|
guard ifconfig4Arguments.count == 2 else {
|
|
|
|
throw ConfigurationError.malformed(option: "ifconfig takes 2 arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
let address4: String
|
|
|
|
let addressMask4: String
|
|
|
|
let defaultGateway4: String
|
|
|
|
|
|
|
|
let topology = Topology(rawValue: optTopology ?? "") ?? .net30
|
|
|
|
switch topology {
|
|
|
|
case .subnet:
|
2019-04-27 11:43:09 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
// default gateway required when topology is subnet
|
|
|
|
guard let gateway4Arguments = optGateway4Arguments, gateway4Arguments.count == 1 else {
|
|
|
|
throw ConfigurationError.malformed(option: "route-gateway takes 1 argument")
|
|
|
|
}
|
|
|
|
address4 = ifconfig4Arguments[0]
|
|
|
|
addressMask4 = ifconfig4Arguments[1]
|
|
|
|
defaultGateway4 = gateway4Arguments[0]
|
2019-05-01 15:15:41 +00:00
|
|
|
|
2019-04-27 11:43:09 +00:00
|
|
|
default:
|
2019-05-19 12:04:41 +00:00
|
|
|
address4 = ifconfig4Arguments[0]
|
|
|
|
addressMask4 = "255.255.255.255"
|
|
|
|
defaultGateway4 = ifconfig4Arguments[1]
|
|
|
|
}
|
|
|
|
let routes4 = optRoutes4.map { IPv4Settings.Route($0.0, $0.1, $0.2 ?? defaultGateway4) }
|
|
|
|
|
|
|
|
sessionBuilder.ipv4 = IPv4Settings(
|
|
|
|
address: address4,
|
|
|
|
addressMask: addressMask4,
|
|
|
|
defaultGateway: defaultGateway4,
|
|
|
|
routes: routes4
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ifconfig6Arguments = optIfconfig6Arguments {
|
|
|
|
guard ifconfig6Arguments.count == 2 else {
|
|
|
|
throw ConfigurationError.malformed(option: "ifconfig-ipv6 takes 2 arguments")
|
|
|
|
}
|
|
|
|
let address6Components = ifconfig6Arguments[0].components(separatedBy: "/")
|
|
|
|
guard address6Components.count == 2 else {
|
|
|
|
throw ConfigurationError.malformed(option: "ifconfig-ipv6 address must have a /prefix")
|
|
|
|
}
|
|
|
|
guard let addressPrefix6 = UInt8(address6Components[1]) else {
|
|
|
|
throw ConfigurationError.malformed(option: "ifconfig-ipv6 address prefix must be a 8-bit number")
|
2019-04-27 11:43:09 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
|
|
|
|
let address6 = address6Components[0]
|
|
|
|
let defaultGateway6 = ifconfig6Arguments[1]
|
|
|
|
let routes6 = optRoutes6.map { IPv6Settings.Route($0.0, $0.1, $0.2 ?? defaultGateway6) }
|
|
|
|
|
|
|
|
sessionBuilder.ipv6 = IPv6Settings(
|
|
|
|
address: address6,
|
|
|
|
addressPrefixLength: addressPrefix6,
|
|
|
|
defaultGateway: defaultGateway6,
|
|
|
|
routes: routes6
|
|
|
|
)
|
2019-04-27 11:43:09 +00:00
|
|
|
}
|
2019-05-19 12:04:41 +00:00
|
|
|
|
2020-12-15 12:57:57 +00:00
|
|
|
// prepend search domains with main domain (if set)
|
|
|
|
if let domain = optDomain {
|
|
|
|
if optSearchDomains == nil {
|
|
|
|
optSearchDomains = [domain]
|
|
|
|
} else {
|
|
|
|
optSearchDomains?.insert(domain, at: 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.dnsServers = optDNSServers
|
2019-10-25 15:21:34 +00:00
|
|
|
sessionBuilder.searchDomains = optSearchDomains
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.httpProxy = optHTTPProxy
|
|
|
|
sessionBuilder.httpsProxy = optHTTPSProxy
|
2019-10-22 19:03:25 +00:00
|
|
|
sessionBuilder.proxyAutoConfigurationURL = optProxyAutoConfigurationURL
|
2019-05-19 12:04:41 +00:00
|
|
|
sessionBuilder.proxyBypassDomains = optProxyBypass
|
|
|
|
|
|
|
|
if let flags = optRedirectGateway {
|
|
|
|
var policies: Set<RoutingPolicy> = []
|
|
|
|
for opt in flags {
|
|
|
|
switch opt {
|
|
|
|
case .def1:
|
|
|
|
policies.insert(.IPv4)
|
|
|
|
|
|
|
|
case .ipv6:
|
|
|
|
policies.insert(.IPv6)
|
|
|
|
|
|
|
|
case .blockLocal:
|
|
|
|
policies.insert(.blockLocal)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// TODO: handle [auto]local and block-*
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flags.contains(.noIPv4) {
|
|
|
|
policies.remove(.IPv4)
|
|
|
|
}
|
|
|
|
sessionBuilder.routingPolicies = [RoutingPolicy](policies)
|
2019-04-27 11:43:09 +00:00
|
|
|
}
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
return Result(
|
|
|
|
url: originalURL,
|
|
|
|
configuration: sessionBuilder.build(),
|
|
|
|
strippedLines: optStrippedLines,
|
|
|
|
warning: optWarning
|
|
|
|
)
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
private static func normalizeEncryptedPEMBlock(block: inout [String]) {
|
|
|
|
// if block.count >= 1 && block[0].contains("ENCRYPTED") {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
|
|
|
|
// XXX: restore blank line after encryption header (easier than tweaking trimmedLines)
|
|
|
|
if block.count >= 3 && block[1].contains("Proc-Type") {
|
|
|
|
block.insert("", at: 3)
|
|
|
|
// return true
|
|
|
|
}
|
|
|
|
// return false
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 10:40:28 +00:00
|
|
|
private extension String {
|
2018-11-10 09:45:00 +00:00
|
|
|
func trimmedLines() -> [String] {
|
|
|
|
return components(separatedBy: .newlines).map {
|
|
|
|
$0.trimmingCharacters(in: .whitespacesAndNewlines)
|
2021-07-17 07:26:30 +00:00
|
|
|
.replacingOccurrences(of: "\\s", with: " ", options: .regularExpression)
|
2018-11-10 09:45:00 +00:00
|
|
|
}.filter {
|
|
|
|
!$0.isEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 10:40:28 +00:00
|
|
|
|
|
|
|
private extension SocketType {
|
|
|
|
init?(protoString: String) {
|
2020-02-29 21:47:51 +00:00
|
|
|
self.init(rawValue: protoString.uppercased())
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
}
|