2018-08-23 08:19:25 +00:00
|
|
|
//
|
|
|
|
// ConnectionStrategy.swift
|
2018-08-23 10:07:55 +00:00
|
|
|
// TunnelKit
|
2018-08-23 08:19:25 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/18/18.
|
2020-01-11 08:26:41 +00:00
|
|
|
// Copyright (c) 2020 Davide De Rosa. All rights reserved.
|
2018-08-27 18:30:09 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-08-27 18:30:09 +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/>.
|
|
|
|
//
|
|
|
|
// This file incorporates work covered by the following copyright and
|
|
|
|
// permission notice:
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018-Present Private Internet Access
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
2018-08-23 08:19:25 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import NetworkExtension
|
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
class ConnectionStrategy {
|
2020-04-14 20:53:15 +00:00
|
|
|
struct Endpoint: CustomStringConvertible {
|
|
|
|
let record: DNSRecord
|
|
|
|
|
|
|
|
let proto: EndpointProtocol
|
|
|
|
|
|
|
|
var isValid: Bool {
|
|
|
|
if record.isIPv6 {
|
|
|
|
return proto.socketType != .udp4 && proto.socketType != .tcp4
|
|
|
|
} else {
|
|
|
|
return proto.socketType != .udp6 && proto.socketType != .tcp6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: CustomStringConvertible
|
2018-08-23 08:19:25 +00:00
|
|
|
|
2020-04-14 20:53:15 +00:00
|
|
|
var description: String {
|
|
|
|
return "\(record.address.maskedDescription):\(proto)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private let hostname: String?
|
2018-08-23 08:19:25 +00:00
|
|
|
|
2018-11-10 09:17:36 +00:00
|
|
|
private let endpointProtocols: [EndpointProtocol]
|
2018-08-23 08:19:25 +00:00
|
|
|
|
2020-04-14 20:53:15 +00:00
|
|
|
private var endpoints: [Endpoint]
|
|
|
|
|
|
|
|
private var currentEndpointIndex: Int
|
|
|
|
|
|
|
|
private let resolvedAddresses: [String]
|
2018-08-23 08:19:25 +00:00
|
|
|
|
2019-05-23 19:59:15 +00:00
|
|
|
init(configuration: OpenVPNTunnelProvider.Configuration) {
|
2019-04-09 17:40:17 +00:00
|
|
|
hostname = configuration.sessionConfiguration.hostname
|
2019-04-04 09:06:00 +00:00
|
|
|
guard var endpointProtocols = configuration.sessionConfiguration.endpointProtocols else {
|
2019-04-09 17:40:17 +00:00
|
|
|
fatalError("No endpoints provided")
|
2019-04-04 09:06:00 +00:00
|
|
|
}
|
2019-03-25 09:21:21 +00:00
|
|
|
if configuration.sessionConfiguration.randomizeEndpoint ?? false {
|
2019-04-04 09:06:00 +00:00
|
|
|
endpointProtocols.shuffle()
|
2019-03-25 09:21:21 +00:00
|
|
|
}
|
2019-04-04 09:06:00 +00:00
|
|
|
self.endpointProtocols = endpointProtocols
|
2020-04-14 20:53:15 +00:00
|
|
|
|
|
|
|
currentEndpointIndex = 0
|
|
|
|
if let resolvedAddresses = configuration.resolvedAddresses {
|
|
|
|
if configuration.prefersResolvedAddresses {
|
|
|
|
endpoints = ConnectionStrategy.unrolledEndpoints(
|
|
|
|
records: resolvedAddresses.map { DNSRecord(address: $0, isIPv6: false) },
|
|
|
|
protos: endpointProtocols
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
endpoints = []
|
|
|
|
}
|
|
|
|
self.resolvedAddresses = resolvedAddresses
|
|
|
|
} else {
|
|
|
|
guard hostname != nil else {
|
|
|
|
fatalError("Either configuration.hostname or resolvedRecords required")
|
|
|
|
}
|
|
|
|
endpoints = []
|
|
|
|
resolvedAddresses = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func unrolledEndpoints(ipv4Addresses: [String], protos: [EndpointProtocol]) -> [Endpoint] {
|
|
|
|
return unrolledEndpoints(records: ipv4Addresses.map { DNSRecord(address: $0, isIPv6: false) }, protos: protos)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func unrolledEndpoints(records: [DNSRecord], protos: [EndpointProtocol]) -> [Endpoint] {
|
|
|
|
guard !records.isEmpty else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
var endpoints: [Endpoint] = []
|
|
|
|
for r in records {
|
|
|
|
for p in protos {
|
|
|
|
let endpoint = Endpoint(record: r, proto: p)
|
|
|
|
guard endpoint.isValid else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
endpoints.append(endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.debug("Unrolled endpoints: \(endpoints.maskedDescription)")
|
|
|
|
return endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasEndpoint() -> Bool {
|
|
|
|
return currentEndpointIndex < endpoints.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func currentEndpoint() -> Endpoint {
|
|
|
|
guard hasEndpoint() else {
|
|
|
|
fatalError("Endpoint index out of bounds (\(currentEndpointIndex) >= \(endpoints.count))")
|
|
|
|
}
|
|
|
|
return endpoints[currentEndpointIndex]
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:53:15 +00:00
|
|
|
@discardableResult
|
|
|
|
func tryNextEndpoint() -> Bool {
|
|
|
|
guard hasEndpoint() else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
currentEndpointIndex += 1
|
|
|
|
guard currentEndpointIndex < endpoints.count else {
|
|
|
|
log.debug("Exhausted endpoints")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
log.debug("Try next endpoint: \(currentEndpoint().maskedDescription)")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:19:25 +00:00
|
|
|
func createSocket(
|
|
|
|
from provider: NEProvider,
|
|
|
|
timeout: Int,
|
|
|
|
queue: DispatchQueue,
|
|
|
|
completionHandler: @escaping (GenericSocket?, Error?) -> Void) {
|
2020-04-14 20:53:15 +00:00
|
|
|
|
|
|
|
if hasEndpoint() {
|
|
|
|
let endpoint = currentEndpoint()
|
|
|
|
log.debug("Pick current endpoint: \(endpoint.maskedDescription)")
|
|
|
|
let socket = provider.createSocket(to: endpoint)
|
2018-08-23 08:19:25 +00:00
|
|
|
completionHandler(socket, nil)
|
|
|
|
return
|
|
|
|
}
|
2020-04-14 20:53:15 +00:00
|
|
|
log.debug("No endpoints available, will resort to DNS resolution")
|
|
|
|
|
2019-04-09 17:40:17 +00:00
|
|
|
guard let hostname = hostname else {
|
|
|
|
log.error("DNS resolution unavailable: no hostname provided!")
|
2019-05-23 19:59:15 +00:00
|
|
|
completionHandler(nil, OpenVPNTunnelProvider.ProviderError.dnsFailure)
|
2019-04-09 17:40:17 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-24 16:25:04 +00:00
|
|
|
log.debug("DNS resolve hostname: \(hostname.maskedDescription)")
|
2020-04-14 20:53:15 +00:00
|
|
|
DNSResolver.resolve(hostname, timeout: timeout, queue: queue) { (records, error) in
|
|
|
|
self.currentEndpointIndex = 0
|
|
|
|
if let records = records, !records.isEmpty {
|
|
|
|
log.debug("DNS resolved addresses: \(records.map { $0.address }.maskedDescription)")
|
|
|
|
self.endpoints = ConnectionStrategy.unrolledEndpoints(records: records, protos: self.endpointProtocols)
|
2018-08-23 08:19:25 +00:00
|
|
|
} else {
|
|
|
|
log.error("DNS resolution failed!")
|
2020-04-14 20:53:15 +00:00
|
|
|
log.debug("Fall back to resolved addresses: \(self.resolvedAddresses.maskedDescription)")
|
|
|
|
self.endpoints = ConnectionStrategy.unrolledEndpoints(ipv4Addresses: self.resolvedAddresses, protos: self.endpointProtocols)
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
2020-04-14 20:53:15 +00:00
|
|
|
|
|
|
|
guard self.hasEndpoint() else {
|
|
|
|
log.error("No endpoints available")
|
2019-05-23 19:59:15 +00:00
|
|
|
completionHandler(nil, OpenVPNTunnelProvider.ProviderError.dnsFailure)
|
2018-08-23 08:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:53:15 +00:00
|
|
|
let targetEndpoint = self.currentEndpoint()
|
|
|
|
log.debug("Pick current endpoint: \(targetEndpoint.maskedDescription)")
|
|
|
|
let socket = provider.createSocket(to: targetEndpoint)
|
2018-08-23 08:19:25 +00:00
|
|
|
completionHandler(socket, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NEProvider {
|
2020-04-14 20:53:15 +00:00
|
|
|
func createSocket(to endpoint: ConnectionStrategy.Endpoint) -> GenericSocket {
|
|
|
|
let ep = NWHostEndpoint(hostname: endpoint.record.address, port: "\(endpoint.proto.port)")
|
|
|
|
switch endpoint.proto.socketType {
|
2020-02-29 21:47:51 +00:00
|
|
|
case .udp, .udp4, .udp6:
|
2020-04-14 20:53:15 +00:00
|
|
|
let impl = createUDPSession(to: ep, from: nil)
|
2018-08-24 10:44:17 +00:00
|
|
|
return NEUDPSocket(impl: impl)
|
2018-08-23 08:19:25 +00:00
|
|
|
|
2020-02-29 21:47:51 +00:00
|
|
|
case .tcp, .tcp4, .tcp6:
|
2020-04-14 20:53:15 +00:00
|
|
|
let impl = createTCPConnection(to: ep, enableTLS: false, tlsParameters: nil, delegate: nil)
|
2018-08-24 10:44:17 +00:00
|
|
|
return NETCPSocket(impl: impl)
|
2018-08-23 08:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|