2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// TunnelKitProvider+FileConfiguration.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/5/18.
|
|
|
|
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/keeshux
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import TunnelKit
|
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
extension TunnelKitProvider.Configuration {
|
|
|
|
private struct Regex {
|
2018-10-17 16:37:02 +00:00
|
|
|
static let proto = Utils.regex("^proto +(udp6?|tcp6?)")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let port = Utils.regex("^port +\\d+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let remote = Utils.regex("^remote +[^ ]+( +\\d+)?( +(udp6?|tcp6?))?")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let cipher = Utils.regex("^cipher +[\\w\\-]+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let auth = Utils.regex("^auth +[\\w\\-]+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let compLZO = Utils.regex("^comp-lzo")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let compress = Utils.regex("^compress")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let ping = Utils.regex("^ping +\\d+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let renegSec = Utils.regex("^reneg-sec +\\d+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let fragment = Utils.regex("^fragment +\\d+")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-23 22:24:23 +00:00
|
|
|
static let proxy = Utils.regex("^\\w+-proxy")
|
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let keyDirection = Utils.regex("^key-direction +\\d")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-24 08:21:40 +00:00
|
|
|
static let externalFiles = Utils.regex("^(ca|cert|key|tls-auth|tls-crypt) ")
|
2018-10-23 16:59:53 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let blockBegin = Utils.regex("^<[\\w\\-]+>")
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-17 16:37:02 +00:00
|
|
|
static let blockEnd = Utils.regex("^<\\/[\\w\\-]+>")
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:28 +00:00
|
|
|
static func parsed(from url: URL, stripped: UnsafeMutablePointer<[String]>? = nil) throws -> (String, TunnelKitProvider.Configuration) {
|
2018-10-21 07:35:46 +00:00
|
|
|
let lines = try String(contentsOf: url).trimmedLines()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
var defaultProto: TunnelKitProvider.SocketType?
|
|
|
|
var defaultPort: UInt16?
|
|
|
|
var remotes: [(String, UInt16?, TunnelKitProvider.SocketType?)] = []
|
|
|
|
|
|
|
|
var cipher: SessionProxy.Cipher?
|
|
|
|
var digest: SessionProxy.Digest?
|
|
|
|
var compressionFraming: SessionProxy.CompressionFraming = .disabled
|
|
|
|
var optCA: CryptoContainer?
|
|
|
|
var clientCertificate: CryptoContainer?
|
|
|
|
var clientKey: CryptoContainer?
|
2018-10-25 18:08:22 +00:00
|
|
|
var keepAliveSeconds: TimeInterval?
|
|
|
|
var renegotiateAfterSeconds: TimeInterval?
|
2018-10-18 21:40:38 +00:00
|
|
|
var keyDirection: StaticKey.Direction?
|
|
|
|
var tlsStrategy: SessionProxy.TLSWrap.Strategy?
|
|
|
|
var tlsKeyLines: [Substring]?
|
|
|
|
var tlsWrap: SessionProxy.TLSWrap?
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
var currentBlockName: String?
|
|
|
|
var currentBlock: [String] = []
|
|
|
|
var unsupportedError: ApplicationError? = nil
|
|
|
|
|
|
|
|
log.verbose("Configuration file:")
|
|
|
|
for line in lines {
|
|
|
|
log.verbose(line)
|
|
|
|
|
2018-10-26 22:17:28 +00:00
|
|
|
var isHandled = false
|
|
|
|
var strippedLine = line
|
|
|
|
defer {
|
|
|
|
if isHandled {
|
|
|
|
stripped?.pointee.append(strippedLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
Regex.blockBegin.enumerateComponents(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
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) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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":
|
|
|
|
clientKey = CryptoContainer(pem: currentBlock.joined(separator: "\n"))
|
|
|
|
|
2018-10-18 21:40:38 +00:00
|
|
|
case "tls-auth":
|
|
|
|
tlsKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
tlsStrategy = .auth
|
|
|
|
|
|
|
|
case "tls-crypt":
|
|
|
|
tlsKeyLines = currentBlock.map { Substring($0) }
|
|
|
|
tlsStrategy = .crypt
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currentBlockName = nil
|
|
|
|
currentBlock = []
|
|
|
|
}
|
|
|
|
if let _ = currentBlockName {
|
|
|
|
currentBlock.append(line)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-26 22:17:28 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
Regex.proto.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultProto = TunnelKitProvider.SocketType(protoString: str)
|
2018-10-18 08:23:46 +00:00
|
|
|
if defaultProto == nil {
|
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "proto \(str)")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.port.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let str = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultPort = UInt16(str)
|
|
|
|
}
|
|
|
|
Regex.remote.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let hostname = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var port: UInt16?
|
|
|
|
var proto: TunnelKitProvider.SocketType?
|
2018-10-26 22:17:28 +00:00
|
|
|
var strippedComponents = ["remote", "<hostname>"]
|
2018-10-11 07:13:19 +00:00
|
|
|
if $0.count > 1 {
|
|
|
|
port = UInt16($0[1])
|
2018-10-26 22:17:28 +00:00
|
|
|
strippedComponents.append($0[1])
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
if $0.count > 2 {
|
|
|
|
proto = TunnelKitProvider.SocketType(protoString: $0[2])
|
2018-10-26 22:17:28 +00:00
|
|
|
strippedComponents.append($0[2])
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
remotes.append((hostname, port, proto))
|
2018-10-26 22:17:28 +00:00
|
|
|
|
|
|
|
// replace private data
|
|
|
|
strippedLine = strippedComponents.joined(separator: " ")
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.cipher.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cipher = SessionProxy.Cipher(rawValue: rawValue.uppercased())
|
2018-10-18 08:23:46 +00:00
|
|
|
if cipher == nil {
|
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "cipher \(rawValue)")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.auth.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let rawValue = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
digest = SessionProxy.Digest(rawValue: rawValue.uppercased())
|
2018-10-18 08:23:46 +00:00
|
|
|
if digest == nil {
|
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "auth \(rawValue)")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.compLZO.enumerateComponents(in: line) { _ in
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
compressionFraming = .compLZO
|
|
|
|
}
|
|
|
|
Regex.compress.enumerateComponents(in: line) { _ in
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
compressionFraming = .compress
|
|
|
|
}
|
2018-10-18 21:40:38 +00:00
|
|
|
Regex.keyDirection.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-18 21:40:38 +00:00
|
|
|
guard let arg = $0.first, let value = Int(arg) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyDirection = StaticKey.Direction(rawValue: value)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
Regex.ping.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
2018-10-25 18:08:22 +00:00
|
|
|
keepAliveSeconds = TimeInterval(arg)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.renegSec.enumerateArguments(in: line) {
|
2018-10-26 22:17:28 +00:00
|
|
|
isHandled = true
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let arg = $0.first else {
|
|
|
|
return
|
|
|
|
}
|
2018-10-25 18:08:22 +00:00
|
|
|
renegotiateAfterSeconds = TimeInterval(arg)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
Regex.fragment.enumerateArguments(in: line) { (_) in
|
2018-10-16 16:03:13 +00:00
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "fragment")
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-10-23 22:24:23 +00:00
|
|
|
Regex.proxy.enumerateArguments(in: line) { (_) in
|
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "proxy: \"\(line)\"")
|
|
|
|
}
|
2018-10-23 16:59:53 +00:00
|
|
|
Regex.externalFiles.enumerateArguments(in: line) { (_) in
|
|
|
|
unsupportedError = ApplicationError.unsupportedConfiguration(option: "external file: \"\(line)\"")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
if let error = unsupportedError {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let ca = optCA else {
|
|
|
|
throw ApplicationError.missingCA
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: only reads first remote
|
|
|
|
// hostnames = remotes.map { $0.0 }
|
|
|
|
guard !remotes.isEmpty else {
|
|
|
|
throw ApplicationError.emptyRemotes
|
|
|
|
}
|
|
|
|
let hostname = remotes[0].0
|
|
|
|
|
|
|
|
defaultProto = defaultProto ?? .udp
|
|
|
|
defaultPort = defaultPort ?? 1194
|
|
|
|
|
|
|
|
// XXX: reads endpoints from remotes with matching hostname
|
|
|
|
var endpointProtocols: [TunnelKitProvider.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(TunnelKitProvider.EndpointProtocol(socketType, port))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!endpointProtocols.isEmpty, "Must define an endpoint protocol")
|
|
|
|
|
2018-10-18 21:40:38 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 18:08:22 +00:00
|
|
|
var sessionBuilder = SessionProxy.ConfigurationBuilder(ca: ca)
|
|
|
|
sessionBuilder.cipher = cipher ?? .aes128cbc
|
|
|
|
sessionBuilder.digest = digest ?? .sha1
|
|
|
|
sessionBuilder.compressionFraming = compressionFraming
|
|
|
|
sessionBuilder.tlsWrap = tlsWrap
|
|
|
|
sessionBuilder.clientCertificate = clientCertificate
|
|
|
|
sessionBuilder.clientKey = clientKey
|
|
|
|
sessionBuilder.keepAliveInterval = keepAliveSeconds
|
|
|
|
sessionBuilder.renegotiatesAfter = renegotiateAfterSeconds
|
|
|
|
var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
|
2018-10-11 07:13:19 +00:00
|
|
|
builder.endpointProtocols = endpointProtocols
|
|
|
|
|
|
|
|
return (hostname, builder.build())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension TunnelKitProvider.SocketType {
|
|
|
|
init?(protoString: String) {
|
|
|
|
var str = protoString
|
|
|
|
if str.hasSuffix("6") {
|
|
|
|
str.removeLast()
|
|
|
|
}
|
|
|
|
self.init(rawValue: str.uppercased())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NSRegularExpression {
|
|
|
|
func enumerateComponents(in string: String, using block: ([String]) -> Void) {
|
|
|
|
enumerateMatches(in: string, options: [], range: NSMakeRange(0, string.count)) { (result, flags, stop) in
|
|
|
|
guard let range = result?.range else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let match = (string as NSString).substring(with: range)
|
|
|
|
let tokens = match.components(separatedBy: " ")
|
|
|
|
block(tokens)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func enumerateArguments(in string: String, using block: ([String]) -> Void) {
|
|
|
|
enumerateMatches(in: string, options: [], range: NSMakeRange(0, string.count)) { (result, flags, stop) in
|
|
|
|
guard let range = result?.range else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let match = (string as NSString).substring(with: range)
|
|
|
|
var tokens = match.components(separatedBy: " ")
|
|
|
|
tokens.removeFirst()
|
|
|
|
block(tokens)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|