2019-03-09 07:42:58 +00:00
|
|
|
//
|
2019-03-18 16:06:19 +00:00
|
|
|
// IntentDispatcher.swift
|
|
|
|
// Passepartout
|
2019-03-09 07:42:58 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/8/19.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2019-03-09 07:42:58 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// 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 Intents
|
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public extension Notification.Name {
|
2019-03-09 07:58:01 +00:00
|
|
|
static let IntentDidUpdateService = Notification.Name("IntentDidUpdateService")
|
|
|
|
}
|
|
|
|
|
2019-03-09 07:42:58 +00:00
|
|
|
@available(iOS 12, *)
|
2019-03-18 16:06:19 +00:00
|
|
|
public class IntentDispatcher {
|
2019-03-09 07:42:58 +00:00
|
|
|
private class Groups {
|
|
|
|
static let vpn = "VPN"
|
|
|
|
|
|
|
|
static let trust = "Trust"
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateConnection(with profile: ConnectionProfile) {
|
2019-03-09 07:42:58 +00:00
|
|
|
let profileKey = ProfileKey(profile)
|
2019-03-09 10:59:05 +00:00
|
|
|
let genericIntent: INIntent
|
2019-03-09 07:42:58 +00:00
|
|
|
|
2019-03-09 10:59:05 +00:00
|
|
|
if let provider = profile as? ProviderConnectionProfile, let pool = provider.pool {
|
|
|
|
let intent = MoveToLocationIntent()
|
|
|
|
intent.providerId = profile.id
|
|
|
|
intent.poolId = pool.id
|
|
|
|
intent.poolName = pool.name
|
|
|
|
genericIntent = intent
|
|
|
|
} else {
|
|
|
|
let intent = ConnectVPNIntent()
|
|
|
|
intent.context = profileKey.context.rawValue
|
|
|
|
intent.profileId = profileKey.id
|
|
|
|
genericIntent = intent
|
|
|
|
}
|
2019-03-09 07:42:58 +00:00
|
|
|
|
2019-03-09 10:59:05 +00:00
|
|
|
let interaction = INInteraction(intent: genericIntent, response: nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
interaction.groupIdentifier = profileKey.rawValue
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateEnableVPN() {
|
2019-03-10 13:03:39 +00:00
|
|
|
let intent = EnableVPNIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.vpn
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateDisableVPN() {
|
2019-03-09 07:42:58 +00:00
|
|
|
let intent = DisableVPNIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.vpn
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateTrustCurrentNetwork() {
|
2019-03-09 07:42:58 +00:00
|
|
|
let intent = TrustCurrentNetworkIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateUntrustCurrentNetwork() {
|
2019-03-09 07:42:58 +00:00
|
|
|
let intent = UntrustCurrentNetworkIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateTrustCellularNetwork() {
|
2019-03-09 07:42:58 +00:00
|
|
|
let intent = TrustCellularNetworkIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func donateUntrustCellularNetwork() {
|
2019-03-09 07:42:58 +00:00
|
|
|
let intent = UntrustCellularNetworkIntent()
|
|
|
|
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-03-18 16:06:19 +00:00
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleInteraction(_ interaction: INInteraction, completionHandler: ((Error?) -> Void)?) {
|
|
|
|
handleIntent(interaction.intent, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleIntent(_ intent: INIntent, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-18 16:06:19 +00:00
|
|
|
if let custom = intent as? ConnectVPNIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleConnectVPN(custom, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let custom = intent as? EnableVPNIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleEnableVPN(custom, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let custom = intent as? DisableVPNIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleDisableVPN(custom, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let custom = intent as? MoveToLocationIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleMoveToLocation(custom, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let _ = intent as? TrustCurrentNetworkIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleCurrentNetwork(trust: true, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let _ = intent as? UntrustCurrentNetworkIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleCurrentNetwork(trust: false, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let _ = intent as? TrustCellularNetworkIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleCellularNetwork(trust: true, interaction: interaction, completionHandler: completionHandler)
|
2019-03-18 16:06:19 +00:00
|
|
|
} else if let _ = intent as? UntrustCellularNetworkIntent {
|
2019-03-18 16:08:54 +00:00
|
|
|
handleCellularNetwork(trust: false, interaction: interaction, completionHandler: completionHandler)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleConnectVPN(_ intent: ConnectVPNIntent, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-09 07:42:58 +00:00
|
|
|
guard let contextValue = intent.context, let context = Context(rawValue: contextValue), let id = intent.profileId else {
|
2019-03-18 16:06:19 +00:00
|
|
|
if let interactionIdentifier = interaction?.identifier {
|
|
|
|
INInteraction.delete(with: [interactionIdentifier], completion: nil)
|
|
|
|
}
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = missing data, programming error
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let profileKey = ProfileKey(context, id)
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("Connect to profile: \(profileKey)")
|
2019-03-09 07:42:58 +00:00
|
|
|
|
|
|
|
let service = TransientStore.shared.service
|
|
|
|
let vpn = VPN.shared
|
|
|
|
guard !(service.isActiveProfile(profileKey) && (vpn.status == .connected)) else {
|
|
|
|
log.info("Profile is already active and connected")
|
2019-03-18 16:08:54 +00:00
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let profile = service.profile(withContext: context, id: id) else {
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = no profile
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
service.activateProfile(profile)
|
2019-03-18 16:08:54 +00:00
|
|
|
refreshVPN(service: service, doReconnect: true, completionHandler: completionHandler)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleMoveToLocation(_ intent: MoveToLocationIntent, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-09 07:42:58 +00:00
|
|
|
guard let providerId = intent.providerId, let poolId = intent.poolId else {
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = no provider/pool
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let service = TransientStore.shared.service
|
|
|
|
guard let providerProfile = service.profile(withContext: .provider, id: providerId) as? ProviderConnectionProfile else {
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = no provider
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("Move to provider location: \(providerId) @ [\(poolId)]")
|
2019-03-09 07:42:58 +00:00
|
|
|
|
|
|
|
let vpn = VPN.shared
|
|
|
|
guard !(service.isActiveProfile(providerProfile) && (providerProfile.poolId == poolId) && (vpn.status == .connected)) else {
|
|
|
|
log.info("Profile is already active and connected to \(poolId)")
|
2019-03-18 16:08:54 +00:00
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
providerProfile.poolId = poolId
|
|
|
|
service.activateProfile(providerProfile)
|
2019-03-18 16:08:54 +00:00
|
|
|
refreshVPN(service: service, doReconnect: true, completionHandler: completionHandler)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleEnableVPN(_ intent: EnableVPNIntent, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-10 13:03:39 +00:00
|
|
|
let service = TransientStore.shared.service
|
|
|
|
log.info("Enabling VPN...")
|
2019-03-18 16:08:54 +00:00
|
|
|
refreshVPN(service: service, doReconnect: true, completionHandler: completionHandler)
|
2019-03-10 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleDisableVPN(_ intent: DisableVPNIntent, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("Disabling VPN...")
|
2019-03-19 16:57:51 +00:00
|
|
|
|
|
|
|
let vpn = VPN.shared
|
|
|
|
vpn.prepare {
|
|
|
|
vpn.disconnect { (error) in
|
|
|
|
notifyServiceUpdate()
|
|
|
|
completionHandler?(error)
|
|
|
|
}
|
2019-03-10 12:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleCurrentNetwork(trust: Bool, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-09 07:42:58 +00:00
|
|
|
guard let currentWifi = Utils.currentWifiNetworkName() else {
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = not connected to wifi
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let service = TransientStore.shared.service
|
|
|
|
service.preferences.trustedWifis[currentWifi] = trust
|
|
|
|
TransientStore.shared.serialize(withProfiles: false)
|
|
|
|
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("\(trust ? "Trusted" : "Untrusted") Wi-Fi: \(currentWifi)")
|
2019-03-18 16:08:54 +00:00
|
|
|
refreshVPN(service: service, doReconnect: false, completionHandler: completionHandler)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
public static func handleCellularNetwork(trust: Bool, interaction: INInteraction?, completionHandler: ((Error?) -> Void)?) {
|
2019-03-09 07:42:58 +00:00
|
|
|
guard Utils.hasCellularData() else {
|
2019-03-18 16:08:54 +00:00
|
|
|
// FIXME: error = has no mobile data
|
|
|
|
completionHandler?(nil)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let service = TransientStore.shared.service
|
|
|
|
service.preferences.trustsMobileNetwork = trust
|
|
|
|
TransientStore.shared.serialize(withProfiles: false)
|
|
|
|
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("\(trust ? "Trusted" : "Untrusted") cellular network")
|
2019-03-18 16:08:54 +00:00
|
|
|
refreshVPN(service: service, doReconnect: false, completionHandler: completionHandler)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:08:54 +00:00
|
|
|
private static func refreshVPN(service: ConnectionService, doReconnect: Bool, completionHandler: ((Error?) -> Void)?) {
|
2019-03-09 07:42:58 +00:00
|
|
|
let configuration: VPNConfiguration
|
|
|
|
do {
|
|
|
|
configuration = try service.vpnConfiguration()
|
|
|
|
} catch let e {
|
|
|
|
log.error("Unable to build VPN configuration: \(e)")
|
2019-03-18 16:08:24 +00:00
|
|
|
notifyServiceUpdate()
|
2019-03-18 16:08:54 +00:00
|
|
|
completionHandler?(e)
|
2019-03-09 07:42:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let vpn = VPN.shared
|
2019-03-10 12:55:22 +00:00
|
|
|
if doReconnect {
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("Reconnecting VPN: \(configuration)")
|
2019-03-09 07:58:01 +00:00
|
|
|
vpn.reconnect(configuration: configuration) { (error) in
|
2019-03-18 16:08:24 +00:00
|
|
|
notifyServiceUpdate()
|
2019-03-18 16:08:54 +00:00
|
|
|
completionHandler?(error)
|
2019-03-09 07:58:01 +00:00
|
|
|
}
|
2019-03-10 12:55:22 +00:00
|
|
|
} else {
|
2019-03-10 12:58:22 +00:00
|
|
|
log.info("Reinstalling VPN: \(configuration)")
|
2019-03-09 07:58:01 +00:00
|
|
|
vpn.install(configuration: configuration) { (error) in
|
2019-03-18 16:08:24 +00:00
|
|
|
notifyServiceUpdate()
|
2019-03-18 16:08:54 +00:00
|
|
|
completionHandler?(error)
|
2019-03-09 07:58:01 +00:00
|
|
|
}
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2019-03-18 16:06:19 +00:00
|
|
|
public static func forgetProfile(withKey profileKey: ProfileKey) {
|
2019-03-09 07:42:58 +00:00
|
|
|
INInteraction.delete(with: profileKey.rawValue) { (error) in
|
|
|
|
if let error = error {
|
|
|
|
log.error("Unable to forget interactions: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.debug("Removed profile \(profileKey) interactions")
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 07:58:01 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2019-03-18 16:08:24 +00:00
|
|
|
private static func notifyServiceUpdate() {
|
2019-03-09 07:58:01 +00:00
|
|
|
NotificationCenter.default.post(name: .IntentDidUpdateService, object: nil)
|
|
|
|
}
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension INInteraction {
|
|
|
|
func donateAndLog() {
|
|
|
|
donate { (error) in
|
|
|
|
if let error = error {
|
|
|
|
log.error("Unable to donate interaction: \(error)")
|
|
|
|
}
|
|
|
|
log.debug("Donated \(self.intent)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|