Merge pull request #40 from passepartoutvpn/custom-intents

Custom intents
This commit is contained in:
Davide De Rosa 2019-03-09 11:31:04 +01:00 committed by GitHub
commit 72f36dd28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 970 additions and 1 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- Custom intents, have a look at Siri suggestions for Passepartout. [#40](https://github.com/passepartoutvpn/passepartout-ios/pull/40)
### Fixed
- Incorrect VPN status after renaming. [#37](https://github.com/passepartoutvpn/passepartout-ios/issues/37)

View File

@ -153,3 +153,15 @@ extension UISplitViewController {
return nil
}
}
extension AppDelegate {
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
guard let interaction = userActivity.interaction else {
return false
}
if #available(iOS 12, *) {
InteractionsHandler.handleInteraction(interaction)
}
return true
}
}

View File

@ -43,6 +43,16 @@
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
<key>NSUserActivityTypes</key>
<array>
<string>ConnectVPNIntent</string>
<string>DisableVPNIntent</string>
<string>MoveToLocationIntent</string>
<string>TrustCellularNetworkIntent</string>
<string>TrustCurrentNetworkIntent</string>
<string>UntrustCellularNetworkIntent</string>
<string>UntrustCurrentNetworkIntent</string>
</array>
<key>UIFileSharingEnabled</key>
<true/>
<key>UILaunchStoryboardName</key>

View File

@ -0,0 +1,585 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>INEnums</key>
<array/>
<key>INIntentDefinitionModelVersion</key>
<string>1.0</string>
<key>INIntentDefinitionSystemVersion</key>
<string>17G3025</string>
<key>INIntentDefinitionToolsBuildVersion</key>
<string>10B61</string>
<key>INIntentDefinitionToolsVersion</key>
<string>10.1</string>
<key>INIntents</key>
<array>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>eXXb2z</string>
<key>INIntentLastParameterTag</key>
<integer>5</integer>
<key>INIntentName</key>
<string>ConnectVPN</string>
<key>INIntentParameterCombinations</key>
<dict>
<key>context,profileId</key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string>On-demand VPN service</string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>uMFvJW</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string>Connect to ${profileId}</string>
<key>INIntentParameterCombinationTitleID</key>
<string>U6o81V</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array>
<dict>
<key>INIntentParameterDisplayPriority</key>
<integer>1</integer>
<key>INIntentParameterName</key>
<string>context</string>
<key>INIntentParameterSupportsMultipleValues</key>
<false/>
<key>INIntentParameterTag</key>
<integer>5</integer>
<key>INIntentParameterType</key>
<string>String</string>
</dict>
<dict>
<key>INIntentParameterDisplayPriority</key>
<integer>2</integer>
<key>INIntentParameterName</key>
<string>profileId</string>
<key>INIntentParameterSupportsMultipleValues</key>
<false/>
<key>INIntentParameterTag</key>
<integer>4</integer>
<key>INIntentParameterType</key>
<string>String</string>
</dict>
</array>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string>Unable to connect</string>
<key>INIntentResponseCodeFormatStringID</key>
<string>uKU9RD</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string>Successfully connected!</string>
<key>INIntentResponseCodeFormatStringID</key>
<string>WNbRl5</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Connect to VPN</string>
<key>INIntentTitleID</key>
<string>LA99yM</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>BKxs8X</string>
<key>INIntentLastParameterTag</key>
<integer>0</integer>
<key>INIntentName</key>
<string>TrustCurrentNetwork</string>
<key>INIntentParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string></string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>AxbXUn</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string></string>
<key>INIntentParameterCombinationTitleID</key>
<string>POyDPM</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array/>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>x3pVKZ</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>qEDn4J</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Trust current network</string>
<key>INIntentTitleID</key>
<string>m2E7SI</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>eQ1yzr</string>
<key>INIntentLastParameterTag</key>
<integer>0</integer>
<key>INIntentName</key>
<string>DisableVPN</string>
<key>INIntentParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string></string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>85kxu8</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string></string>
<key>INIntentParameterCombinationTitleID</key>
<string>IeGsEq</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array/>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>fnSNbT</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>oKHXZ3</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Disable VPN service</string>
<key>INIntentTitleID</key>
<string>1ZRTCZ</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>7eoAss</string>
<key>INIntentLastParameterTag</key>
<integer>0</integer>
<key>INIntentName</key>
<string>UntrustCurrentNetwork</string>
<key>INIntentParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string></string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>pb3MGt</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string></string>
<key>INIntentParameterCombinationTitleID</key>
<string>0Wu9nb</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array/>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>r0ZjO4</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>rtaAzk</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Untrust current network</string>
<key>INIntentTitleID</key>
<string>rd1T8p</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>9GpJt5</string>
<key>INIntentLastParameterTag</key>
<integer>0</integer>
<key>INIntentName</key>
<string>TrustCellularNetwork</string>
<key>INIntentParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string></string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>vIPVA5</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string></string>
<key>INIntentParameterCombinationTitleID</key>
<string>NWWgCl</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array/>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>nMRaxS</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>gSqy7Y</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Trust cellular network</string>
<key>INIntentTitleID</key>
<string>H4taev</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>0jRWn5</string>
<key>INIntentLastParameterTag</key>
<integer>0</integer>
<key>INIntentName</key>
<string>UntrustCellularNetwork</string>
<key>INIntentParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<true/>
<key>INIntentParameterCombinationSubtitle</key>
<string></string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>qRkLSU</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string></string>
<key>INIntentParameterCombinationTitleID</key>
<string>ggzKA2</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array/>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>YncGoj</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>BW8KLX</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Untrust cellular network</string>
<key>INIntentTitleID</key>
<string>wB1iYX</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentDescriptionID</key>
<string>KjkCfU</string>
<key>INIntentLastParameterTag</key>
<integer>3</integer>
<key>INIntentName</key>
<string>MoveToLocation</string>
<key>INIntentParameterCombinations</key>
<dict>
<key>providerId,poolName,poolId</key>
<dict>
<key>INIntentParameterCombinationIsPrimary</key>
<false/>
<key>INIntentParameterCombinationSubtitle</key>
<string>With ${providerId} provider</string>
<key>INIntentParameterCombinationSubtitleID</key>
<string>66bZBE</string>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string>Move to ${poolName}</string>
<key>INIntentParameterCombinationTitleID</key>
<string>WnTPFg</string>
</dict>
</dict>
<key>INIntentParameters</key>
<array>
<dict>
<key>INIntentParameterDisplayPriority</key>
<integer>1</integer>
<key>INIntentParameterName</key>
<string>providerId</string>
<key>INIntentParameterSupportsMultipleValues</key>
<false/>
<key>INIntentParameterTag</key>
<integer>2</integer>
<key>INIntentParameterType</key>
<string>String</string>
</dict>
<dict>
<key>INIntentParameterDisplayPriority</key>
<integer>2</integer>
<key>INIntentParameterName</key>
<string>poolId</string>
<key>INIntentParameterSupportsMultipleValues</key>
<false/>
<key>INIntentParameterTag</key>
<integer>3</integer>
<key>INIntentParameterType</key>
<string>String</string>
</dict>
<dict>
<key>INIntentParameterDisplayPriority</key>
<integer>3</integer>
<key>INIntentParameterName</key>
<string>poolName</string>
<key>INIntentParameterSupportsMultipleValues</key>
<false/>
<key>INIntentParameterTag</key>
<integer>1</integer>
<key>INIntentParameterType</key>
<string>String</string>
</dict>
</array>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>PmWNkC</string>
<key>INIntentResponseCodeName</key>
<string>failure</string>
<key>INIntentResponseCodeSuccess</key>
<false/>
</dict>
<dict>
<key>INIntentResponseCodeFormatString</key>
<string></string>
<key>INIntentResponseCodeFormatStringID</key>
<string>O6nCLQ</string>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>0</integer>
<key>INIntentResponseParameters</key>
<array/>
</dict>
<key>INIntentRestrictions</key>
<integer>0</integer>
<key>INIntentTitle</key>
<string>Move to provider location</string>
<key>INIntentTitleID</key>
<string>qo3Szz</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentUserConfirmationRequired</key>
<false/>
<key>INIntentVerb</key>
<string>Go</string>
</dict>
</array>
</dict>
</plist>

View File

@ -0,0 +1,287 @@
//
// InteractionsHandler.swift
// Passepartout-iOS
//
// Created by Davide De Rosa on 3/8/19.
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
//
// 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
extension Notification.Name {
static let IntentDidUpdateService = Notification.Name("IntentDidUpdateService")
}
@available(iOS 12, *)
class InteractionsHandler {
private class Groups {
static let vpn = "VPN"
static let trust = "Trust"
}
static func donateConnectVPN(with profile: ConnectionProfile) {
let profileKey = ProfileKey(profile)
let intent = ConnectVPNIntent()
intent.context = profileKey.context.rawValue
intent.profileId = profileKey.id
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = profileKey.rawValue
interaction.donateAndLog()
}
static func donateDisableVPN() {
let intent = DisableVPNIntent()
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = Groups.vpn
interaction.donateAndLog()
}
static func donateMoveToLocation(with profile: ProviderConnectionProfile, pool: Pool) {
let profileKey = ProfileKey(profile)
let intent = MoveToLocationIntent()
intent.providerId = profile.id
intent.poolId = pool.id
intent.poolName = pool.name
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = profileKey.rawValue
interaction.donateAndLog()
}
static func donateTrustCurrentNetwork() {
let intent = TrustCurrentNetworkIntent()
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = Groups.trust
interaction.donateAndLog()
}
static func donateUntrustCurrentNetwork() {
let intent = UntrustCurrentNetworkIntent()
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = Groups.trust
interaction.donateAndLog()
}
static func donateTrustCellularNetwork() {
let intent = TrustCellularNetworkIntent()
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = Groups.trust
interaction.donateAndLog()
}
static func donateUntrustCellularNetwork() {
let intent = UntrustCellularNetworkIntent()
let interaction = INInteraction(intent: intent, response: nil)
interaction.groupIdentifier = Groups.trust
interaction.donateAndLog()
}
//
static func handleInteraction(_ interaction: INInteraction) {
if let custom = interaction.intent as? ConnectVPNIntent {
handleConnectVPN(custom, interaction: interaction)
} else if let custom = interaction.intent as? DisableVPNIntent {
handleDisableVPN(custom, interaction: interaction)
} else if let custom = interaction.intent as? MoveToLocationIntent {
handleMoveToLocation(custom, interaction: interaction)
} else if let _ = interaction.intent as? TrustCurrentNetworkIntent {
handleCurrentNetwork(trust: true, interaction: interaction)
} else if let _ = interaction.intent as? UntrustCurrentNetworkIntent {
handleCurrentNetwork(trust: false, interaction: interaction)
} else if let _ = interaction.intent as? TrustCellularNetworkIntent {
handleCellularNetwork(trust: true, interaction: interaction)
} else if let _ = interaction.intent as? UntrustCellularNetworkIntent {
handleCellularNetwork(trust: false, interaction: interaction)
}
}
private static func handleConnectVPN(_ intent: ConnectVPNIntent, interaction: INInteraction) {
guard let contextValue = intent.context, let context = Context(rawValue: contextValue), let id = intent.profileId else {
INInteraction.delete(with: [interaction.identifier], completion: nil)
return
}
let profileKey = ProfileKey(context, id)
log.info("Connect to profile \(profileKey)")
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")
return
}
guard let profile = service.profile(withContext: context, id: id) else {
return
}
service.activateProfile(profile)
let configuration: VPNConfiguration
do {
configuration = try service.vpnConfiguration()
} catch let e {
log.error("Unable to build VPN configuration: \(e)")
notifyServiceController()
return
}
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
if let error = error {
log.error("Unable to connect to \(profileKey): \(error)")
return
}
log.info("Connecting to \(profileKey)...")
}
}
private static func handleDisableVPN(_ intent: DisableVPNIntent, interaction: INInteraction) {
VPN.shared.disconnect { (error) in
notifyServiceController()
}
}
private static func handleMoveToLocation(_ intent: MoveToLocationIntent, interaction: INInteraction) {
guard let providerId = intent.providerId, let poolId = intent.poolId else {
return
}
let service = TransientStore.shared.service
guard let providerProfile = service.profile(withContext: .provider, id: providerId) as? ProviderConnectionProfile else {
return
}
log.info("Move to provider \(providerId) @ [\(poolId)]")
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)")
return
}
providerProfile.poolId = poolId
service.activateProfile(providerProfile)
let configuration: VPNConfiguration
do {
configuration = try service.vpnConfiguration()
} catch let e {
log.error("Unable to build VPN configuration: \(e)")
return
}
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
if let error = error {
log.error("Unable to connect to \(providerId) @ [\(poolId)]: \(error)")
return
}
log.info("Connecting to \(providerId) @ [\(poolId)]...")
}
}
private static func handleCurrentNetwork(trust: Bool, interaction: INInteraction) {
guard let currentWifi = Utils.currentWifiNetworkName() else {
return
}
let service = TransientStore.shared.service
service.preferences.trustedWifis[currentWifi] = trust
TransientStore.shared.serialize(withProfiles: false)
reconnectVPN(service: service)
}
private static func handleCellularNetwork(trust: Bool, interaction: INInteraction) {
guard Utils.hasCellularData() else {
return
}
let service = TransientStore.shared.service
service.preferences.trustsMobileNetwork = trust
TransientStore.shared.serialize(withProfiles: false)
reconnectVPN(service: service)
}
private static func reconnectVPN(service: ConnectionService) {
let configuration: VPNConfiguration
do {
configuration = try service.vpnConfiguration()
} catch let e {
log.error("Unable to build VPN configuration: \(e)")
notifyServiceController()
return
}
let vpn = VPN.shared
switch vpn.status {
case .connected:
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
}
default:
vpn.install(configuration: configuration) { (error) in
notifyServiceController()
}
}
}
//
static func forgetProfile(withKey profileKey: ProfileKey) {
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")
}
}
//
private static func notifyServiceController() {
NotificationCenter.default.post(name: .IntentDidUpdateService, object: nil)
}
}
private extension INInteraction {
func donateAndLog() {
donate { (error) in
if let error = error {
log.error("Unable to donate interaction: \(error)")
}
log.debug("Donated \(self.intent)")
}
}
}

View File

@ -8,6 +8,8 @@
</array>
<key>com.apple.developer.networking.wifi-info</key>
<true/>
<key>com.apple.developer.siri</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.algoritmico.Passepartout</string>

View File

@ -223,6 +223,9 @@ class OrganizerViewController: UITableViewController, TableModelHost {
tableView.endUpdates()
service.removeProfile(rowProfile)
if #available(iOS 12, *) {
InteractionsHandler.forgetProfile(withKey: rowProfile)
}
}
private func confirmVpnProfileDeletion() {
@ -468,6 +471,10 @@ extension OrganizerViewController: ConnectionServiceDelegate {
reloadModel()
tableView.reloadData()
if #available(iOS 12, *) {
InteractionsHandler.donateConnectVPN(with: profile)
}
// XXX: hack around bad replace when detail presented in compact view
if let detailNav = navigationController?.viewControllers.last as? UINavigationController {
var existingServiceVC: ServiceViewController?
@ -510,5 +517,9 @@ extension OrganizerViewController: ConnectionServiceDelegate {
TransientStore.shared.serialize(withProfiles: false) // activate
tableView.reloadData()
if #available(iOS 12, *) {
InteractionsHandler.donateConnectVPN(with: profile)
}
}
}

View File

@ -98,6 +98,7 @@ class ServiceViewController: UIViewController, TableModelHost {
nc.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
nc.addObserver(self, selector: #selector(vpnDidUpdate), name: .VPNDidChangeStatus, object: nil)
nc.addObserver(self, selector: #selector(vpnDidUpdate), name: .VPNDidReinstall, object: nil)
nc.addObserver(self, selector: #selector(intentDidUpdateService), name: .IntentDidUpdateService, object: nil)
// run this no matter what
// XXX: convenient here vs AppDelegate for updating table
@ -243,12 +244,24 @@ class ServiceViewController: UIViewController, TableModelHost {
self.reloadModel()
self.tableView.reloadData()
}
if #available(iOS 12, *) {
InteractionsHandler.donateDisableVPN()
}
} else {
vpn.disconnect { (error) in
self.reloadModel()
self.tableView.reloadData()
}
}
if #available(iOS 12, *) {
if let provider = profile as? ProviderConnectionProfile, let pool = provider.pool {
InteractionsHandler.donateMoveToLocation(with: provider, pool: pool)
} else {
InteractionsHandler.donateConnectVPN(with: uncheckedProfile)
}
}
}
private func confirmVpnReconnection() {
@ -408,6 +421,10 @@ class ServiceViewController: UIViewController, TableModelHost {
}
}
@objc private func intentDidUpdateService() {
setProfile(profile)
}
@objc private func applicationDidBecomeActive() {
reloadVpnStatus()
}
@ -763,6 +780,11 @@ extension ServiceViewController: UITableViewDataSource, UITableViewDelegate, Tog
return true
case .trustedAddCurrentWiFi:
if #available(iOS 12, *) {
InteractionsHandler.donateTrustCurrentNetwork()
InteractionsHandler.donateUntrustCurrentNetwork()
}
guard trustedNetworks.addCurrentWifi() else {
let alert = Macros.alert(
L10n.Service.Sections.Trusted.header,
@ -807,6 +829,11 @@ extension ServiceViewController: UITableViewDataSource, UITableViewDelegate, Tog
toggleDisconnectsOnSleep(cell.isOn)
case .trustedMobile:
if #available(iOS 12, *) {
InteractionsHandler.donateTrustCellularNetwork()
InteractionsHandler.donateUntrustCellularNetwork()
}
trustedNetworks.setMobile(cell.isOn)
case .trustedWiFi:
@ -1066,6 +1093,10 @@ extension ServiceViewController: ProviderPoolViewControllerDelegate {
uncheckedProviderProfile.poolId = pool.id
reloadSelectedRow(andRowAt: endpointIndexPath)
vpn.reinstallIfEnabled()
if #available(iOS 12, *) {
InteractionsHandler.donateMoveToLocation(with: uncheckedProviderProfile, pool: pool)
}
}
}

View File

@ -31,6 +31,8 @@
0E4FD7E120D3E4C5002221FF /* MockVPNProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E4FD7E020D3E4C5002221FF /* MockVPNProvider.swift */; };
0E4FD7EE20D539A0002221FF /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E4FD7ED20D539A0002221FF /* Utils.swift */; };
0E4FD7F120D58618002221FF /* Macros.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E4FD7F020D58618002221FF /* Macros.swift */; };
0E50E7C322330E4900D5F76C /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 0E50E7C222330E4900D5F76C /* Intents.intentdefinition */; };
0E50E7C6223318A500D5F76C /* InteractionsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E50E7C5223318A500D5F76C /* InteractionsHandler.swift */; };
0E57F63C20C83FC5008323CF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E57F63B20C83FC5008323CF /* AppDelegate.swift */; };
0E57F63E20C83FC5008323CF /* ServiceViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E57F63D20C83FC5008323CF /* ServiceViewController.swift */; };
0E57F64120C83FC5008323CF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0E57F63F20C83FC5008323CF /* Main.storyboard */; };
@ -152,6 +154,8 @@
0E4FD7ED20D539A0002221FF /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
0E4FD7F020D58618002221FF /* Macros.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Macros.swift; sourceTree = "<group>"; };
0E4FD80420D683A2002221FF /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/NetworkExtension.framework; sourceTree = DEVELOPER_DIR; };
0E50E7C222330E4900D5F76C /* Intents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Intents.intentdefinition; sourceTree = "<group>"; };
0E50E7C5223318A500D5F76C /* InteractionsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InteractionsHandler.swift; sourceTree = "<group>"; };
0E57F63820C83FC5008323CF /* Passepartout.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Passepartout.app; sourceTree = BUILT_PRODUCTS_DIR; };
0E57F63B20C83FC5008323CF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
0E57F63D20C83FC5008323CF /* ServiceViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceViewController.swift; sourceTree = "<group>"; };
@ -289,6 +293,15 @@
path = VPN;
sourceTree = "<group>";
};
0E50E7C422330E5100D5F76C /* Intents */ = {
isa = PBXGroup;
children = (
0E50E7C222330E4900D5F76C /* Intents.intentdefinition */,
0E50E7C5223318A500D5F76C /* InteractionsHandler.swift */,
);
path = Intents;
sourceTree = "<group>";
};
0E57F62F20C83FC5008323CF = {
isa = PBXGroup;
children = (
@ -319,6 +332,7 @@
0E1066CA20E0F85C004F98B7 /* Cells */,
0ECEE44C20E1120F00A6BB43 /* Tables */,
0EDE8DF120C93ED8004C739C /* Scenes */,
0E50E7C422330E5100D5F76C /* Intents */,
0EDE8DE220C86A13004C739C /* Passepartout.entitlements */,
0E57F63B20C83FC5008323CF /* AppDelegate.swift */,
0E57F63F20C83FC5008323CF /* Main.storyboard */,
@ -602,6 +616,9 @@
com.apple.NetworkExtensions.iOS = {
enabled = 1;
};
com.apple.Siri = {
enabled = 1;
};
};
};
0E57F64B20C83FC7008323CF = {
@ -823,6 +840,7 @@
0E1066C920E0F84A004F98B7 /* Cells.swift in Sources */,
0EF56BBB2185AC8500B0C8AB /* SwiftGen+Segues.swift in Sources */,
0EBE3AA6213DC1B000BFA2F5 /* ProviderConnectionProfile.swift in Sources */,
0E50E7C322330E4900D5F76C /* Intents.intentdefinition in Sources */,
0E3DA371215CB5BF00B40FC9 /* VersionViewController.swift in Sources */,
0EBBE8F52182361800106008 /* ConnectionService+Migration.swift in Sources */,
0E39BCF3214DA9310035E9DE /* AppConstants.swift in Sources */,
@ -831,6 +849,7 @@
0E79D14121919F5600BB5FB2 /* ProfileKey.swift in Sources */,
0E89DFC5213DF7AE00741BA1 /* Preferences.swift in Sources */,
0E6BE13A20CFB76800A6DD36 /* ApplicationError.swift in Sources */,
0E50E7C6223318A500D5F76C /* InteractionsHandler.swift in Sources */,
0EFD9440215BED8E00529B64 /* LabelViewController.swift in Sources */,
0ED31C2C20CF2D6F0027975F /* ProviderPoolViewController.swift in Sources */,
0E2B494020FCFF990094784C /* Theme+Titles.swift in Sources */,

View File

@ -25,7 +25,7 @@
import Foundation
struct ProfileKey: RawRepresentable, Hashable, Codable {
struct ProfileKey: RawRepresentable, Hashable, Codable, CustomStringConvertible {
private static let separator: Character = "."
let context: Context
@ -63,4 +63,10 @@ struct ProfileKey: RawRepresentable, Hashable, Codable {
let idEnd = rawValue.endIndex
id = String(rawValue[idStart..<idEnd])
}
// MARK: CustomStringConvertible
var description: String {
return "{context=\(context), id=\(id)}"
}
}

View File

@ -28,6 +28,8 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
// FIXME: replace completionHandler?(nil) with completionHandler?(some_error)
class GracefulVPN {
private let service: ConnectionService