passepartout-apple/Passepartout/App/Domain/IntentDispatcher+Activities...

162 lines
5.9 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// IntentDispatcher+Activities.swift
// Passepartout
//
// Created by Davide De Rosa on 3/30/22.
2024-01-14 13:34:21 +00:00
// Copyright (c) 2024 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/>.
//
2023-12-16 19:58:54 +00:00
#if !os(tvOS)
2022-04-12 13:09:14 +00:00
import Foundation
import Intents
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
2022-04-12 13:09:14 +00:00
@MainActor
2022-04-12 13:09:14 +00:00
extension IntentDispatcher {
typealias VPNIntentActivity = IntentActivity<VPNManager>
2023-03-17 20:55:47 +00:00
static let enableVPN = VPNIntentActivity(name: Constants.Activities.enableVPN) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Enabling VPN...")
Task {
do {
try await vpnManager.connectWithActiveProfile(toServer: nil)
2022-04-12 13:09:14 +00:00
} catch {
pp_log.error("Unable to connect with active profile: \(error)")
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(error)
2022-04-12 13:09:14 +00:00
}
}
}
2023-03-17 20:55:47 +00:00
static let disableVPN = VPNIntentActivity(name: Constants.Activities.disableVPN) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Disabling VPN...")
Task {
await vpnManager.disable()
}
}
static let connectVPN = VPNIntentActivity(name: Constants.Activities.connectVPN) { activity, vpnManager in
pp_log.info("Connecting VPN...")
guard let intent = activity.interaction?.intent as? ConnectVPNIntent else {
assertionFailure("Not a ConnectVPNIntent?")
return
}
guard let uuid = intent.profileId, let profileId = UUID(uuidString: uuid) else {
assertionFailure("Profile id is not valid")
if let interactionIdentifier = activity.interaction?.identifier {
INInteraction.delete(with: [interactionIdentifier], completion: nil)
}
return
}
Task {
do {
_ = try await vpnManager.connect(with: profileId)
2022-04-12 13:09:14 +00:00
} catch {
pp_log.error("Unable to connect with profile \(profileId): \(error)")
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(error)
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 let moveToLocation = VPNIntentActivity(name: Constants.Activities.moveToLocation) { activity, vpnManager in
pp_log.info("Moving to VPN location...")
guard let intent = activity.interaction?.intent as? MoveToLocationIntent else {
assertionFailure("Not a MoveToLocationIntent?")
return
}
guard let uuid = intent.profileId, let profileId = UUID(uuidString: uuid) else {
if let interactionIdentifier = activity.interaction?.identifier {
INInteraction.delete(with: [interactionIdentifier], completion: nil)
}
return
}
guard let newServerId = intent.serverId else {
assertionFailure("Missing serverId")
if let interactionIdentifier = activity.interaction?.identifier {
INInteraction.delete(with: [interactionIdentifier], completion: nil)
}
return
}
Task {
do {
_ = try await vpnManager.connect(with: profileId, toServer: newServerId)
2022-04-12 13:09:14 +00:00
} catch {
pp_log.error("Unable to connect with profile \(profileId): \(error)")
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(error)
2022-04-12 13:09:14 +00:00
}
}
}
2023-03-17 20:55:47 +00:00
static let trustCellularNetwork = VPNIntentActivity(name: Constants.Activities.trustCellularNetwork) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Trusting mobile network...")
handleCellularNetwork(true, vpnManager)
}
2023-03-17 20:55:47 +00:00
static let trustCurrentNetwork = VPNIntentActivity(name: Constants.Activities.trustCurrentNetwork) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Trusting current Wi-Fi...")
handleCurrentNetwork(true, vpnManager)
}
2023-03-17 20:55:47 +00:00
static let untrustCellularNetwork = VPNIntentActivity(name: Constants.Activities.untrustCellularNetwork) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Untrusting mobile network...")
handleCellularNetwork(false, vpnManager)
}
2023-03-17 20:55:47 +00:00
static let untrustCurrentNetwork = VPNIntentActivity(name: Constants.Activities.untrustCurrentNetwork) { _, vpnManager in
2022-04-12 13:09:14 +00:00
pp_log.info("Untrusting current Wi-Fi...")
handleCurrentNetwork(false, vpnManager)
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private static func handleCellularNetwork(_ trust: Bool, _ vpnManager: VPNManager) {
Task {
do {
try await vpnManager.modifyActiveProfile {
$0.onDemand.withMobileNetwork = trust
}
} catch {
pp_log.error("Unable to modify cellular trust: \(error)")
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(error)
2022-04-12 13:09:14 +00:00
}
}
}
private static func handleCurrentNetwork(_ trust: Bool, _ vpnManager: VPNManager) {
Task {
guard let ssid = await Utils.currentWifiSSID() else {
pp_log.warning("Not connected to any Wi-Fi or no permission to read location (needs 'While Using' or 'Always')")
return
}
2022-04-12 13:09:14 +00:00
do {
try await vpnManager.modifyActiveProfile {
pp_log.info("Wi-Fi SSID: \(ssid)")
$0.onDemand.withSSIDs[ssid] = trust
}
} catch {
pp_log.error("Unable to modify Wi-Fi trust: \(error)")
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(error)
2022-04-12 13:09:14 +00:00
}
}
}
}
2023-12-16 19:58:54 +00:00
#endif