2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// IntentDispatcher.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/8/19.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +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
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
class IntentDispatcher {
|
|
|
|
private struct Groups {
|
|
|
|
static let vpn = "VPN"
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static let trust = "Trust"
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// MARK: Intents
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentConnect(header: Profile.Header) -> ConnectVPNIntent {
|
|
|
|
let intent = ConnectVPNIntent()
|
|
|
|
intent.profileId = header.id.uuidString
|
|
|
|
intent.profileName = header.name
|
|
|
|
return intent
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentMoveTo(header: Profile.Header, providerFullName: String, server: ProviderServer) -> MoveToLocationIntent {
|
|
|
|
let intent = MoveToLocationIntent()
|
|
|
|
intent.profileId = header.id.uuidString
|
|
|
|
intent.providerFullName = providerFullName
|
|
|
|
intent.serverId = server.id
|
2023-03-17 17:09:21 +00:00
|
|
|
intent.serverName = server.localizedLongDescription(withCategory: false)
|
2022-04-12 13:09:14 +00:00
|
|
|
return intent
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentEnable() -> EnableVPNIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
EnableVPNIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentDisable() -> DisableVPNIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
DisableVPNIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentTrustWiFi() -> TrustCurrentNetworkIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
TrustCurrentNetworkIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentUntrustWiFi() -> UntrustCurrentNetworkIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
UntrustCurrentNetworkIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentTrustCellular() -> TrustCellularNetworkIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
TrustCellularNetworkIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func intentUntrustCellular() -> UntrustCellularNetworkIntent {
|
2022-09-04 18:09:31 +00:00
|
|
|
UntrustCellularNetworkIntent()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Donations
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateConnection(with profile: Profile, providerManager: ProviderManager) {
|
|
|
|
let genericIntent: INIntent
|
|
|
|
if let providerName = profile.header.providerName {
|
|
|
|
guard let provider = providerManager.provider(withName: providerName) else {
|
|
|
|
pp_log.warning("Intent provider not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let server = profile.providerServer(providerManager) else {
|
|
|
|
pp_log.warning("Intent server not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
genericIntent = intentMoveTo(header: profile.header, providerFullName: provider.fullName, server: server)
|
|
|
|
} else {
|
|
|
|
genericIntent = intentConnect(header: profile.header)
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
let interaction = INInteraction(intent: genericIntent, response: nil)
|
|
|
|
interaction.groupIdentifier = profile.id.uuidString
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateEnableVPN() {
|
|
|
|
let interaction = INInteraction(intent: intentEnable(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.vpn
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateDisableVPN() {
|
|
|
|
let interaction = INInteraction(intent: intentDisable(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.vpn
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateTrustCurrentNetwork() {
|
|
|
|
let interaction = INInteraction(intent: intentTrustWiFi(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
|
|
|
|
|
|
|
static func donateUntrustCurrentNetwork() {
|
|
|
|
let interaction = INInteraction(intent: intentUntrustWiFi(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateTrustCellularNetwork() {
|
|
|
|
let interaction = INInteraction(intent: intentTrustCellular(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func donateUntrustCellularNetwork() {
|
|
|
|
let interaction = INInteraction(intent: intentUntrustCellular(), response: nil)
|
|
|
|
interaction.groupIdentifier = Groups.trust
|
|
|
|
interaction.donateAndLog()
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func forgetProfile(withHeader header: Profile.Header) {
|
|
|
|
INInteraction.delete(with: header.id.uuidString) { (error) in
|
|
|
|
if let error = error {
|
|
|
|
pp_log.warning("Unable to forget interactions: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pp_log.debug("Removed profile \(header.name) interactions")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension INInteraction {
|
|
|
|
func donateAndLog() {
|
|
|
|
donate { (error) in
|
|
|
|
if let error = error {
|
|
|
|
pp_log.error("Unable to donate interaction: \(error)")
|
|
|
|
}
|
|
|
|
pp_log.debug("Donated \(self.intent)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|