2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// ConnectionProfile.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
|
|
|
|
import NetworkExtension
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public enum Context: String, Codable {
|
2018-10-26 16:36:41 +00:00
|
|
|
case provider
|
|
|
|
|
|
|
|
case host
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public protocol ConnectionProfile: class, EndpointDataSource, CustomStringConvertible {
|
2018-10-26 16:36:41 +00:00
|
|
|
var context: Context { get }
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
var id: String { get }
|
|
|
|
|
|
|
|
var username: String? { get set }
|
|
|
|
|
2018-10-18 20:51:37 +00:00
|
|
|
var requiresCredentials: Bool { get }
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration
|
2018-11-02 13:44:29 +00:00
|
|
|
|
|
|
|
func with(newId: String) -> ConnectionProfile
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension ConnectionProfile {
|
2018-10-11 07:13:19 +00:00
|
|
|
var passwordKey: String? {
|
|
|
|
guard let username = username else {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-26 16:36:41 +00:00
|
|
|
return "\(Bundle.main.bundleIdentifier!).\(context.rawValue).\(id).\(username)"
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func password(in keychain: Keychain) -> String? {
|
|
|
|
guard let key = passwordKey else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return try? keychain.password(for: key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setPassword(_ password: String?, in keychain: Keychain) throws {
|
|
|
|
guard let key = passwordKey else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let password = password else {
|
|
|
|
keychain.removePassword(for: key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try keychain.set(password: password, for: key, label: key)
|
|
|
|
}
|
2018-11-02 14:43:44 +00:00
|
|
|
|
|
|
|
func removePassword(in keychain: Keychain) {
|
|
|
|
guard let key = passwordKey else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keychain.removePassword(for: key)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-11-04 14:56:05 +00:00
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public extension ConnectionProfile {
|
2018-11-04 14:56:05 +00:00
|
|
|
var description: String {
|
2019-01-04 13:19:56 +00:00
|
|
|
return "(\(context):\(id))"
|
2018-11-04 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|