2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// HostConnectionProfile.m
|
|
|
|
// 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 HostConnectionProfile: ConnectionProfile, Codable, Equatable {
|
|
|
|
public var title: String
|
2018-10-25 21:08:43 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public let hostname: String
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var parameters: TunnelKitProvider.Configuration
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public init(title: String, hostname: String) {
|
2018-10-11 07:13:19 +00:00
|
|
|
self.title = title
|
|
|
|
self.hostname = hostname
|
2019-04-04 09:21:51 +00:00
|
|
|
let sessionConfiguration = SessionProxy.ConfigurationBuilder().build()
|
2018-10-25 18:08:22 +00:00
|
|
|
parameters = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionConfiguration).build()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: ConnectionProfile
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public let context: Context = .host
|
2018-10-26 16:36:41 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var id: String {
|
2018-10-25 21:08:43 +00:00
|
|
|
return title
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
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 false
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration {
|
2019-04-04 09:21:51 +00:00
|
|
|
guard let endpointProtocols = parameters.sessionConfiguration.endpointProtocols, !endpointProtocols.isEmpty else {
|
|
|
|
preconditionFailure("No endpointProtocols")
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
// XXX: copy paste, error prone
|
|
|
|
var builder = parameters.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
|
|
|
|
2019-04-09 22:33:27 +00:00
|
|
|
// forcibly override hostname with profile hostname (never nil)
|
2019-04-28 21:10:58 +00:00
|
|
|
var sessionBuilder = builder.sessionConfiguration.builder()
|
|
|
|
sessionBuilder.hostname = hostname
|
|
|
|
builder.sessionConfiguration = sessionBuilder.build()
|
2019-04-09 22:33:27 +00:00
|
|
|
|
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
|
|
|
let profile = HostConnectionProfile(title: newId, hostname: hostname)
|
|
|
|
profile.username = username
|
|
|
|
profile.parameters = parameters
|
|
|
|
return profile
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension HostConnectionProfile {
|
2018-10-11 07:13:19 +00:00
|
|
|
static func ==(lhs: HostConnectionProfile, rhs: HostConnectionProfile) -> Bool {
|
|
|
|
return lhs.id == rhs.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension HostConnectionProfile {
|
2019-04-09 18:36:06 +00:00
|
|
|
var mainAddress: String? {
|
2018-10-11 07:13:19 +00:00
|
|
|
return hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
var addresses: [String] {
|
|
|
|
return [hostname]
|
|
|
|
}
|
|
|
|
|
2018-11-10 09:29:51 +00:00
|
|
|
var protocols: [EndpointProtocol] {
|
2019-04-04 09:21:51 +00:00
|
|
|
return parameters.sessionConfiguration.endpointProtocols ?? []
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var canCustomizeEndpoint: Bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var customAddress: String? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 09:29:51 +00:00
|
|
|
var customProtocol: EndpointProtocol? {
|
2018-10-11 07:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|