2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// ProviderConnectionProfile.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/2/18.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
|
|
|
|
public let name: Infrastructure.Name
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var infrastructure: Infrastructure {
|
2018-10-11 07:13:19 +00:00
|
|
|
return InfrastructureFactory.shared.get(name)
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var poolId: String {
|
2018-10-11 07:13:19 +00:00
|
|
|
didSet {
|
|
|
|
validateEndpoint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var pool: Pool? {
|
2018-10-18 23:01:11 +00:00
|
|
|
return infrastructure.pool(for: poolId) ?? infrastructure.pool(for: infrastructure.defaults.pool)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var presetId: String {
|
2018-10-11 07:13:19 +00:00
|
|
|
didSet {
|
|
|
|
validateEndpoint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var preset: InfrastructurePreset? {
|
2018-10-11 07:13:19 +00:00
|
|
|
return infrastructure.preset(for: presetId)
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var manualAddress: String?
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var manualProtocol: EndpointProtocol?
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var usesProviderEndpoint: Bool {
|
2018-10-11 07:13:19 +00:00
|
|
|
return (manualAddress != nil) || (manualProtocol != nil)
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public init(name: Infrastructure.Name) {
|
2018-10-11 07:13:19 +00:00
|
|
|
self.name = name
|
|
|
|
poolId = ""
|
|
|
|
presetId = ""
|
|
|
|
|
|
|
|
username = nil
|
|
|
|
|
|
|
|
poolId = infrastructure.defaults.pool
|
|
|
|
presetId = infrastructure.defaults.preset
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func sortedPools() -> [Pool] {
|
2018-10-29 16:44:34 +00:00
|
|
|
return infrastructure.pools.sorted()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func validateEndpoint() {
|
|
|
|
guard let pool = pool, let preset = preset else {
|
|
|
|
manualAddress = nil
|
|
|
|
manualProtocol = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let address = manualAddress, !pool.hasAddress(address) {
|
|
|
|
manualAddress = nil
|
|
|
|
}
|
|
|
|
if let proto = manualProtocol, !preset.hasProtocol(proto) {
|
|
|
|
manualProtocol = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: ConnectionProfile
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public let context: Context = .provider
|
2018-10-26 16:36:41 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var id: String {
|
2018-10-11 07:13:19 +00:00
|
|
|
return name.rawValue
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var username: String?
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var requiresCredentials: Bool {
|
2018-10-18 20:51:37 +00:00
|
|
|
return true
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-10-18 20:51:37 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let pool = pool else {
|
2018-10-18 23:01:11 +00:00
|
|
|
preconditionFailure("Nil pool?")
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
guard let preset = preset else {
|
2018-10-18 23:01:11 +00:00
|
|
|
preconditionFailure("Nil preset?")
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assert(!pool.numericAddresses.isEmpty)
|
|
|
|
|
|
|
|
// XXX: copy paste, error prone
|
|
|
|
var builder = preset.configuration.builder()
|
|
|
|
builder.mtu = configuration.mtu
|
|
|
|
builder.shouldDebug = configuration.shouldDebug
|
|
|
|
builder.debugLogFormat = configuration.debugLogFormat
|
2019-03-21 20:31:13 +00:00
|
|
|
builder.masksPrivateData = configuration.masksPrivateData
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
if let address = manualAddress {
|
|
|
|
builder.prefersResolvedAddresses = true
|
|
|
|
builder.resolvedAddresses = [address]
|
|
|
|
} else {
|
|
|
|
builder.prefersResolvedAddresses = !preferences.resolvesHostname
|
2019-01-05 20:55:59 +00:00
|
|
|
builder.resolvedAddresses = pool.addresses()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let proto = manualProtocol {
|
|
|
|
builder.endpointProtocols = [proto]
|
|
|
|
} else {
|
|
|
|
builder.endpointProtocols = preset.configuration.endpointProtocols
|
|
|
|
// builder.endpointProtocols = [
|
2018-11-10 09:29:51 +00:00
|
|
|
// EndpointProtocol(.udp, 8080),
|
|
|
|
// EndpointProtocol(.tcp, 443)
|
2018-10-11 07:13:19 +00:00
|
|
|
// ]
|
|
|
|
}
|
|
|
|
return builder.build()
|
|
|
|
}
|
2018-11-02 13:44:29 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func with(newId: String) -> ConnectionProfile {
|
2018-11-02 13:44:29 +00:00
|
|
|
fatalError("Cannot rename a ProviderConnectionProfile")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension ProviderConnectionProfile {
|
2018-10-11 07:13:19 +00:00
|
|
|
static func ==(lhs: ProviderConnectionProfile, rhs: ProviderConnectionProfile) -> Bool {
|
|
|
|
return lhs.id == rhs.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension ProviderConnectionProfile {
|
2018-10-11 07:13:19 +00:00
|
|
|
var mainAddress: String {
|
|
|
|
assert(pool != nil, "Getting provider main address but no pool set")
|
|
|
|
return pool?.hostname ?? ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var addresses: [String] {
|
2019-01-05 20:55:59 +00:00
|
|
|
return pool?.addresses() ?? []
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 09:29:51 +00:00
|
|
|
var protocols: [EndpointProtocol] {
|
2018-10-11 07:13:19 +00:00
|
|
|
return preset?.configuration.endpointProtocols ?? []
|
|
|
|
}
|
|
|
|
|
|
|
|
var canCustomizeEndpoint: Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var customAddress: String? {
|
|
|
|
return manualAddress
|
|
|
|
}
|
|
|
|
|
2018-11-10 09:29:51 +00:00
|
|
|
var customProtocol: EndpointProtocol? {
|
2018-10-11 07:13:19 +00:00
|
|
|
return manualProtocol
|
|
|
|
}
|
|
|
|
}
|