2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// ConnectionService.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/3/18.
|
|
|
|
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
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
|
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
protocol ConnectionServiceDelegate: class {
|
2018-11-02 13:20:40 +00:00
|
|
|
func connectionService(didAdd profile: ConnectionProfile)
|
|
|
|
|
2018-11-02 14:49:14 +00:00
|
|
|
func connectionService(didRename oldProfile: ConnectionProfile, to newProfile: ConnectionProfile)
|
2018-11-02 13:20:40 +00:00
|
|
|
|
|
|
|
func connectionService(didRemoveProfileWithKey key: ConnectionService.ProfileKey)
|
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
func connectionService(willDeactivate profile: ConnectionProfile)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
func connectionService(didActivate profile: ConnectionProfile)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectionService: Codable {
|
|
|
|
enum CodingKeys: String, CodingKey {
|
2018-10-25 18:22:05 +00:00
|
|
|
case build
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
case appGroup
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
case baseConfiguration
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
case activeProfileKey
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
case preferences
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
struct ProfileKey: RawRepresentable, Hashable, Codable {
|
|
|
|
let context: Context
|
|
|
|
|
|
|
|
let id: String
|
|
|
|
|
|
|
|
init(_ context: Context, _ id: String) {
|
|
|
|
self.context = context
|
|
|
|
self.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
init(_ profile: ConnectionProfile) {
|
2018-10-26 16:36:41 +00:00
|
|
|
context = profile.context
|
2018-10-26 08:00:28 +00:00
|
|
|
id = profile.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: RawRepresentable
|
|
|
|
|
|
|
|
var rawValue: String {
|
|
|
|
return "\(context).\(id)"
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(rawValue: String) {
|
|
|
|
let comps = rawValue.components(separatedBy: ".")
|
|
|
|
guard comps.count == 2 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let context = Context(rawValue: comps[0]) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.context = context
|
|
|
|
id = comps[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 12:25:33 +00:00
|
|
|
var directory: String? = nil
|
|
|
|
|
|
|
|
var rootURL: URL {
|
|
|
|
return FileManager.default.userURL(for: .documentDirectory, appending: directory)
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
private var providersURL: URL {
|
2018-11-01 12:25:33 +00:00
|
|
|
return rootURL.appendingPathComponent(AppConstants.Store.providersDirectory)
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var hostsURL: URL {
|
2018-11-01 12:25:33 +00:00
|
|
|
return rootURL.appendingPathComponent(AppConstants.Store.hostsDirectory)
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 18:22:05 +00:00
|
|
|
private var build: Int
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
private let appGroup: String
|
|
|
|
|
|
|
|
private let defaults: UserDefaults
|
|
|
|
|
|
|
|
private let keychain: Keychain
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
var baseConfiguration: TunnelKitProvider.Configuration
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private var cache: [ProfileKey: ConnectionProfile]
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private var pendingRemoval: Set<ProfileKey>
|
|
|
|
|
|
|
|
private(set) var activeProfileKey: ProfileKey? {
|
2018-10-11 07:13:19 +00:00
|
|
|
willSet {
|
|
|
|
if let oldProfile = activeProfile {
|
2018-11-03 22:40:07 +00:00
|
|
|
delegate?.connectionService(willDeactivate: oldProfile)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if let newProfile = activeProfile {
|
|
|
|
delegate?.connectionService(didActivate: newProfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeProfile: ConnectionProfile? {
|
2018-10-26 08:00:28 +00:00
|
|
|
guard let id = activeProfileKey else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
var hit = cache[id]
|
|
|
|
if let placeholder = hit as? PlaceholderConnectionProfile {
|
|
|
|
hit = profile(withContext: placeholder.context, id: placeholder.id)
|
|
|
|
cache[id] = hit
|
|
|
|
}
|
|
|
|
return hit
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let preferences: EditablePreferences
|
|
|
|
|
|
|
|
weak var delegate: ConnectionServiceDelegate?
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
init(withAppGroup appGroup: String, baseConfiguration: TunnelKitProvider.Configuration) {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let defaults = UserDefaults(suiteName: appGroup) else {
|
|
|
|
fatalError("No entitlements for group '\(appGroup)'")
|
|
|
|
}
|
2018-10-25 18:22:05 +00:00
|
|
|
build = GroupConstants.App.buildNumber
|
2018-10-11 07:13:19 +00:00
|
|
|
self.appGroup = appGroup
|
|
|
|
self.defaults = defaults
|
|
|
|
keychain = Keychain(group: appGroup)
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
self.baseConfiguration = baseConfiguration
|
2018-10-26 08:00:28 +00:00
|
|
|
activeProfileKey = nil
|
2018-10-11 07:13:19 +00:00
|
|
|
preferences = EditablePreferences()
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
cache = [:]
|
|
|
|
pendingRemoval = []
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Codable
|
|
|
|
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
let appGroup = try container.decode(String.self, forKey: .appGroup)
|
|
|
|
guard let defaults = UserDefaults(suiteName: appGroup) else {
|
|
|
|
fatalError("No entitlements for group '\(appGroup)'")
|
|
|
|
}
|
2018-10-25 18:22:05 +00:00
|
|
|
build = try container.decode(Int.self, forKey: .build)
|
2018-10-11 07:13:19 +00:00
|
|
|
self.appGroup = appGroup
|
|
|
|
self.defaults = defaults
|
|
|
|
keychain = Keychain(group: appGroup)
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
baseConfiguration = try container.decode(TunnelKitProvider.Configuration.self, forKey: .baseConfiguration)
|
2018-10-26 08:00:28 +00:00
|
|
|
activeProfileKey = try container.decodeIfPresent(ProfileKey.self, forKey: .activeProfileKey)
|
2018-10-11 07:13:19 +00:00
|
|
|
preferences = try container.decode(EditablePreferences.self, forKey: .preferences)
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
cache = [:]
|
|
|
|
pendingRemoval = []
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encode(to encoder: Encoder) throws {
|
2018-10-25 18:22:05 +00:00
|
|
|
build = GroupConstants.App.buildNumber
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
2018-10-25 18:22:05 +00:00
|
|
|
try container.encode(build, forKey: .build)
|
2018-10-11 07:13:19 +00:00
|
|
|
try container.encode(appGroup, forKey: .appGroup)
|
2018-10-25 16:42:11 +00:00
|
|
|
try container.encode(baseConfiguration, forKey: .baseConfiguration)
|
2018-10-26 08:00:28 +00:00
|
|
|
try container.encodeIfPresent(activeProfileKey, forKey: .activeProfileKey)
|
2018-10-11 07:13:19 +00:00
|
|
|
try container.encode(preferences, forKey: .preferences)
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
// MARK: Serialization
|
|
|
|
|
|
|
|
func loadProfiles() {
|
|
|
|
let fm = FileManager.default
|
|
|
|
try? fm.createDirectory(at: providersURL, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
try? fm.createDirectory(at: hostsURL, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let files = try fm.contentsOfDirectory(at: providersURL, includingPropertiesForKeys: nil, options: [])
|
|
|
|
// log.debug("Found \(files.count) provider files: \(files)")
|
|
|
|
for entry in files {
|
|
|
|
guard let id = ConnectionService.profileId(fromURL: entry) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let key = ProfileKey(.provider, id)
|
|
|
|
cache[key] = PlaceholderConnectionProfile(key)
|
|
|
|
}
|
|
|
|
} catch let e {
|
2018-10-27 08:55:50 +00:00
|
|
|
log.error("Could not list provider contents: \(e) (\(providersURL))")
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
let files = try fm.contentsOfDirectory(at: hostsURL, includingPropertiesForKeys: nil, options: [])
|
|
|
|
// log.debug("Found \(files.count) host files: \(files)")
|
|
|
|
for entry in files {
|
|
|
|
guard let id = ConnectionService.profileId(fromURL: entry) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let key = ProfileKey(.host, id)
|
|
|
|
cache[key] = PlaceholderConnectionProfile(key)
|
|
|
|
}
|
|
|
|
} catch let e {
|
2018-10-27 08:55:50 +00:00
|
|
|
log.error("Could not list host contents: \(e) (\(hostsURL))")
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-11-02 14:49:14 +00:00
|
|
|
func saveProfiles() {
|
2018-10-26 08:00:28 +00:00
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
|
|
|
let fm = FileManager.default
|
|
|
|
try? fm.createDirectory(at: providersURL, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
try? fm.createDirectory(at: hostsURL, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
|
|
|
|
for key in pendingRemoval {
|
2018-11-01 12:19:35 +00:00
|
|
|
let url = profileURL(key)
|
2018-10-26 08:00:28 +00:00
|
|
|
try? fm.removeItem(at: url)
|
2018-10-27 10:39:22 +00:00
|
|
|
if let cfg = configurationURL(for: key) {
|
|
|
|
try? fm.removeItem(at: cfg)
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
for entry in cache.values {
|
|
|
|
if let profile = entry as? ProviderConnectionProfile {
|
|
|
|
do {
|
2018-11-01 12:19:35 +00:00
|
|
|
let url = profileURL(ProfileKey(.provider, entry.id))
|
2018-10-26 08:00:28 +00:00
|
|
|
let data = try encoder.encode(profile)
|
|
|
|
try data.write(to: url)
|
|
|
|
log.debug("Saved provider '\(profile.id)'")
|
|
|
|
} catch let e {
|
2018-10-27 08:55:50 +00:00
|
|
|
log.error("Could not save provider '\(profile.id)': \(e)")
|
2018-10-26 08:00:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if let profile = entry as? HostConnectionProfile {
|
|
|
|
do {
|
2018-11-01 12:19:35 +00:00
|
|
|
let url = profileURL(ProfileKey(.host, entry.id))
|
2018-10-26 08:00:28 +00:00
|
|
|
let data = try encoder.encode(profile)
|
|
|
|
try data.write(to: url)
|
|
|
|
log.debug("Saved host '\(profile.id)'")
|
|
|
|
} catch let e {
|
2018-10-27 08:55:50 +00:00
|
|
|
log.error("Could not save host '\(profile.id)': \(e)")
|
2018-10-26 08:00:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if let placeholder = entry as? PlaceholderConnectionProfile {
|
|
|
|
log.debug("Skipped \(placeholder.context) '\(placeholder.id)'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 16:36:41 +00:00
|
|
|
func profile(withContext context: Context, id: String) -> ConnectionProfile? {
|
2018-10-26 08:00:28 +00:00
|
|
|
let key = ProfileKey(context, id)
|
|
|
|
var profile = cache[key]
|
|
|
|
if let _ = profile as? PlaceholderConnectionProfile {
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
do {
|
2018-11-01 12:19:35 +00:00
|
|
|
let data = try profileData(key)
|
2018-10-26 08:00:28 +00:00
|
|
|
switch context {
|
|
|
|
case .provider:
|
|
|
|
profile = try decoder.decode(ProviderConnectionProfile.self, from: data)
|
|
|
|
|
|
|
|
case .host:
|
|
|
|
profile = try decoder.decode(HostConnectionProfile.self, from: data)
|
|
|
|
}
|
|
|
|
cache[key] = profile
|
|
|
|
} catch let e {
|
2018-10-27 08:55:50 +00:00
|
|
|
log.error("Could not decode profile JSON: \(e)")
|
2018-10-26 08:00:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return profile
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 16:36:41 +00:00
|
|
|
func ids(forContext context: Context) -> [String] {
|
2018-10-26 08:00:28 +00:00
|
|
|
return cache.keys.filter { $0.context == context }.map { $0.id }
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 12:19:35 +00:00
|
|
|
func contextURL(_ key: ProfileKey) -> URL {
|
|
|
|
switch key.context {
|
|
|
|
case .provider:
|
|
|
|
return providersURL
|
|
|
|
|
|
|
|
case .host:
|
|
|
|
return hostsURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func profileURL(_ key: ProfileKey) -> URL {
|
|
|
|
return contextURL(key).appendingPathComponent(key.id).appendingPathExtension("json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func profileData(_ key: ProfileKey) throws -> Data {
|
|
|
|
return try Data(contentsOf: profileURL(key))
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private static func profileId(fromURL url: URL) -> String? {
|
2018-10-27 08:40:13 +00:00
|
|
|
guard url.pathExtension == "json" else {
|
2018-10-26 08:00:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-27 08:40:13 +00:00
|
|
|
return url.deletingPathExtension().lastPathComponent
|
2018-10-26 08:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Profiles
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func addProfile(_ profile: ConnectionProfile, credentials: Credentials?) -> Bool {
|
2018-10-26 08:00:28 +00:00
|
|
|
guard cache.index(forKey: ProfileKey(profile)) == nil else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
addOrReplaceProfile(profile, credentials: credentials)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func addOrReplaceProfile(_ profile: ConnectionProfile, credentials: Credentials?) {
|
2018-10-26 08:00:28 +00:00
|
|
|
let key = ProfileKey(profile)
|
|
|
|
cache[key] = profile
|
|
|
|
pendingRemoval.remove(key)
|
2018-10-11 07:13:19 +00:00
|
|
|
try? setCredentials(credentials, for: profile)
|
2018-10-26 08:00:28 +00:00
|
|
|
if cache.count == 1 {
|
|
|
|
activeProfileKey = key
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
|
2018-11-02 13:20:40 +00:00
|
|
|
delegate?.connectionService(didAdd: profile)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-11-02 14:49:14 +00:00
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
@discardableResult func renameProfile(_ key: ProfileKey, to newId: String) -> ConnectionProfile? {
|
2018-11-02 14:49:14 +00:00
|
|
|
precondition(newId != key.id)
|
|
|
|
|
|
|
|
// WARNING: can be a placeholder
|
|
|
|
guard let oldProfile = cache[key] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let fm = FileManager.default
|
|
|
|
let temporaryDelegate = delegate
|
|
|
|
delegate = nil
|
|
|
|
|
|
|
|
// 1. add renamed profile
|
|
|
|
let newProfile = oldProfile.with(newId: newId)
|
|
|
|
let newKey = ProfileKey(newProfile)
|
|
|
|
let sameCredentials = credentials(for: oldProfile)
|
|
|
|
addOrReplaceProfile(newProfile, credentials: sameCredentials)
|
|
|
|
|
|
|
|
// 2. rename .ovpn (if present)
|
|
|
|
if let cfgFrom = configurationURL(for: key) {
|
|
|
|
let cfgTo = targetConfigurationURL(for: newKey)
|
|
|
|
try? fm.removeItem(at: cfgTo)
|
|
|
|
try? fm.moveItem(at: cfgFrom, to: cfgTo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. remove old entry
|
|
|
|
removeProfile(key)
|
|
|
|
|
|
|
|
// 4. replace active key (if active)
|
|
|
|
if key == activeProfileKey {
|
|
|
|
activeProfileKey = newKey
|
|
|
|
}
|
|
|
|
|
|
|
|
delegate = temporaryDelegate
|
|
|
|
delegate?.connectionService(didRename: oldProfile, to: newProfile)
|
|
|
|
|
|
|
|
return newProfile
|
|
|
|
}
|
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
@discardableResult func renameProfile(_ profile: ConnectionProfile, to id: String) -> ConnectionProfile? {
|
2018-11-02 14:49:14 +00:00
|
|
|
return renameProfile(ProfileKey(profile), to: id)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
func removeProfile(_ key: ProfileKey) {
|
2018-11-02 14:43:44 +00:00
|
|
|
guard let profile = cache[key] else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-02 14:43:44 +00:00
|
|
|
cache.removeValue(forKey: key)
|
|
|
|
removeCredentials(for: profile)
|
2018-10-26 08:00:28 +00:00
|
|
|
pendingRemoval.insert(key)
|
|
|
|
if cache.isEmpty {
|
|
|
|
activeProfileKey = nil
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2018-11-02 13:20:40 +00:00
|
|
|
|
|
|
|
delegate?.connectionService(didRemoveProfileWithKey: key)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
func containsProfile(_ key: ProfileKey) -> Bool {
|
|
|
|
return cache.index(forKey: key) != nil
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
func containsProfile(_ profile: ConnectionProfile) -> Bool {
|
|
|
|
return containsProfile(ProfileKey(profile))
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func hasActiveProfile() -> Bool {
|
2018-10-26 08:00:28 +00:00
|
|
|
return activeProfileKey != nil
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
func isActiveProfile(_ key: ProfileKey) -> Bool {
|
|
|
|
return key == activeProfileKey
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func isActiveProfile(_ profile: ConnectionProfile) -> Bool {
|
2018-10-26 08:00:28 +00:00
|
|
|
return isActiveProfile(ProfileKey(profile))
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func activateProfile(_ profile: ConnectionProfile) {
|
2018-10-26 08:00:28 +00:00
|
|
|
activeProfileKey = ProfileKey(profile)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Credentials
|
|
|
|
|
2018-10-18 20:51:37 +00:00
|
|
|
func needsCredentials(for profile: ConnectionProfile) -> Bool {
|
|
|
|
guard profile.requiresCredentials else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-10-18 20:51:37 +00:00
|
|
|
guard let creds = credentials(for: profile) else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return creds.isEmpty
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func credentials(for profile: ConnectionProfile) -> Credentials? {
|
|
|
|
guard let username = profile.username, let key = profile.passwordKey else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let password = try? keychain.password(for: key) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return Credentials(username, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCredentials(_ credentials: Credentials?, for profile: ConnectionProfile) throws {
|
|
|
|
profile.username = credentials?.username
|
|
|
|
try profile.setPassword(credentials?.password, in: keychain)
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:43:44 +00:00
|
|
|
func removeCredentials(for profile: ConnectionProfile) {
|
|
|
|
profile.removePassword(in: keychain)
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
// MARK: VPN
|
|
|
|
|
|
|
|
func vpnConfiguration() throws -> NetworkExtensionVPNConfiguration {
|
|
|
|
guard let profile = activeProfile else {
|
|
|
|
throw ApplicationError.missingProfile
|
|
|
|
}
|
2018-10-19 15:47:35 +00:00
|
|
|
let creds = credentials(for: profile)
|
|
|
|
if profile.requiresCredentials {
|
|
|
|
guard creds != nil else {
|
|
|
|
throw ApplicationError.missingCredentials
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:42:11 +00:00
|
|
|
let cfg = try profile.generate(from: baseConfiguration, preferences: preferences)
|
2018-10-11 07:13:19 +00:00
|
|
|
let protocolConfiguration = try cfg.generatedTunnelProtocol(
|
|
|
|
withBundleIdentifier: GroupConstants.App.tunnelIdentifier,
|
|
|
|
appGroup: appGroup,
|
|
|
|
hostname: profile.mainAddress,
|
2018-10-19 15:47:35 +00:00
|
|
|
credentials: creds
|
2018-10-11 07:13:19 +00:00
|
|
|
)
|
|
|
|
protocolConfiguration.disconnectOnSleep = preferences.disconnectsOnSleep
|
|
|
|
|
|
|
|
log.verbose("Configuration:")
|
|
|
|
log.verbose(protocolConfiguration)
|
|
|
|
|
|
|
|
var rules: [NEOnDemandRule] = []
|
|
|
|
#if os(iOS)
|
|
|
|
if preferences.trustsMobileNetwork {
|
|
|
|
let rule = policyRule()
|
|
|
|
rule.interfaceTypeMatch = .cellular
|
|
|
|
rules.append(rule)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
let reallyTrustedWifis = Array(preferences.trustedWifis.filter { $1 }.keys)
|
|
|
|
if !reallyTrustedWifis.isEmpty {
|
|
|
|
let rule = policyRule()
|
|
|
|
rule.interfaceTypeMatch = .wiFi
|
|
|
|
rule.ssidMatch = reallyTrustedWifis
|
|
|
|
rules.append(rule)
|
|
|
|
}
|
|
|
|
rules.append(NEOnDemandRuleConnect())
|
|
|
|
|
|
|
|
return NetworkExtensionVPNConfiguration(protocolConfiguration: protocolConfiguration, onDemandRules: rules)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func policyRule() -> NEOnDemandRule {
|
|
|
|
switch preferences.trustPolicy {
|
|
|
|
case .ignore:
|
|
|
|
return NEOnDemandRuleIgnore()
|
|
|
|
|
|
|
|
case .disconnect:
|
|
|
|
return NEOnDemandRuleDisconnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var vpnLog: String {
|
2018-10-25 16:42:11 +00:00
|
|
|
return baseConfiguration.existingLog(in: appGroup) ?? ""
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 21:53:20 +00:00
|
|
|
var vpnLastError: TunnelKitProvider.ProviderError? {
|
2018-10-25 19:08:49 +00:00
|
|
|
return baseConfiguration.lastError(in: appGroup)
|
2018-10-21 21:53:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 22:14:40 +00:00
|
|
|
func clearVpnLastError() {
|
2018-10-25 19:08:49 +00:00
|
|
|
baseConfiguration.clearLastError(in: appGroup)
|
2018-10-21 22:14:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
// func eraseVpnLog() {
|
|
|
|
// defaults.removeObject(forKey: Keys.vpnLog)
|
|
|
|
// }
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
private class PlaceholderConnectionProfile: ConnectionProfile {
|
2018-10-26 16:36:41 +00:00
|
|
|
let context: Context
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
let id: String
|
|
|
|
|
2018-11-02 13:44:29 +00:00
|
|
|
var username: String? = nil
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
var requiresCredentials: Bool = false
|
|
|
|
|
|
|
|
func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration {
|
|
|
|
fatalError("Generating configuration from a PlaceholderConnectionProfile")
|
|
|
|
}
|
|
|
|
|
2018-11-02 13:44:29 +00:00
|
|
|
func with(newId: String) -> ConnectionProfile {
|
|
|
|
return PlaceholderConnectionProfile(ConnectionService.ProfileKey(context, newId))
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
var mainAddress: String = ""
|
|
|
|
|
|
|
|
var addresses: [String] = []
|
|
|
|
|
|
|
|
var protocols: [TunnelKitProvider.EndpointProtocol] = []
|
|
|
|
|
|
|
|
var canCustomizeEndpoint: Bool = false
|
|
|
|
|
|
|
|
var customAddress: String?
|
|
|
|
|
|
|
|
var customProtocol: TunnelKitProvider.EndpointProtocol?
|
|
|
|
|
|
|
|
init(_ key: ConnectionService.ProfileKey) {
|
|
|
|
self.context = key.context
|
|
|
|
self.id = key.id
|
|
|
|
}
|
|
|
|
}
|