2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// NetworkExtensionVPN.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/15/18.
|
2024-01-14 13:33:14 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit 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.
|
|
|
|
//
|
|
|
|
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import NetworkExtension
|
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
/// `VPN` based on the NetworkExtension framework.
|
|
|
|
public class NetworkExtensionVPN: VPN {
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initializes a provider.
|
|
|
|
*/
|
|
|
|
public init() {
|
|
|
|
let nc = NotificationCenter.default
|
|
|
|
nc.addObserver(self, selector: #selector(vpnDidUpdate(_:)), name: .NEVPNStatusDidChange, object: nil)
|
|
|
|
nc.addObserver(self, selector: #selector(vpnDidReinstall(_:)), name: .NEVPNConfigurationChange, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
// MARK: Public
|
2022-03-04 23:58:07 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
public func prepare() async {
|
2022-10-12 20:28:33 +00:00
|
|
|
_ = try? await NETunnelProviderManager.loadAllFromPreferences()
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
public func install(
|
2022-04-04 00:50:43 +00:00
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: NetworkExtensionConfiguration,
|
|
|
|
extra: NetworkExtensionExtra?
|
|
|
|
) async throws {
|
|
|
|
_ = try await installReturningManager(
|
|
|
|
tunnelBundleIdentifier,
|
|
|
|
configuration: configuration,
|
|
|
|
extra: extra
|
|
|
|
)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-07-23 07:58:48 +00:00
|
|
|
public func reconnect(after: DispatchTimeInterval) async throws {
|
|
|
|
let managers = try await lookupAll()
|
|
|
|
guard let manager = managers.first else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if manager.connection.status != .disconnected {
|
|
|
|
manager.connection.stopVPNTunnel()
|
|
|
|
try await Task.sleep(nanoseconds: after.nanoseconds)
|
|
|
|
}
|
|
|
|
try manager.connection.startVPNTunnel()
|
|
|
|
}
|
2022-04-04 00:50:43 +00:00
|
|
|
|
|
|
|
public func reconnect(
|
2022-03-04 23:58:07 +00:00
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: NetworkExtensionConfiguration,
|
|
|
|
extra: NetworkExtensionExtra?,
|
2022-04-04 00:50:43 +00:00
|
|
|
after: DispatchTimeInterval
|
|
|
|
) async throws {
|
2022-03-04 23:58:07 +00:00
|
|
|
do {
|
2022-04-04 00:50:43 +00:00
|
|
|
let manager = try await installReturningManager(
|
|
|
|
tunnelBundleIdentifier,
|
|
|
|
configuration: configuration,
|
2022-03-04 23:58:07 +00:00
|
|
|
extra: extra
|
|
|
|
)
|
2022-04-04 00:50:43 +00:00
|
|
|
if manager.connection.status != .disconnected {
|
|
|
|
manager.connection.stopVPNTunnel()
|
|
|
|
try await Task.sleep(nanoseconds: after.nanoseconds)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2022-04-04 00:50:43 +00:00
|
|
|
try manager.connection.startVPNTunnel()
|
|
|
|
} catch {
|
2022-04-27 13:44:40 +00:00
|
|
|
notifyInstallError(error)
|
2022-04-04 00:50:43 +00:00
|
|
|
throw error
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
public func disconnect() async {
|
2022-10-12 20:28:33 +00:00
|
|
|
guard let managers = try? await lookupAll() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard !managers.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for m in managers {
|
|
|
|
m.connection.stopVPNTunnel()
|
|
|
|
m.isOnDemandEnabled = false
|
|
|
|
m.isEnabled = false
|
|
|
|
try? await m.saveToPreferences()
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
public func uninstall() async {
|
2022-10-12 20:28:33 +00:00
|
|
|
guard let managers = try? await lookupAll() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard !managers.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for m in managers {
|
|
|
|
m.connection.stopVPNTunnel()
|
|
|
|
try? await m.removeFromPreferences()
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
2022-04-04 00:50:43 +00:00
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
private func installReturningManager(
|
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: NetworkExtensionConfiguration,
|
|
|
|
extra: NetworkExtensionExtra?
|
|
|
|
) async throws -> NETunnelProviderManager {
|
|
|
|
let proto = try configuration.asTunnelProtocol(
|
|
|
|
withBundleIdentifier: tunnelBundleIdentifier,
|
|
|
|
extra: extra
|
|
|
|
)
|
|
|
|
let managers = try await lookupAll()
|
|
|
|
|
2023-12-23 23:40:42 +00:00
|
|
|
extra?.userData?.forEach {
|
|
|
|
proto.providerConfiguration?[$0.key] = $0.value
|
|
|
|
}
|
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
// install (new or existing) then callback
|
|
|
|
let targetManager = managers.first {
|
|
|
|
$0.isTunnel(withIdentifier: tunnelBundleIdentifier)
|
|
|
|
} ?? NETunnelProviderManager()
|
|
|
|
|
|
|
|
_ = try await install(
|
|
|
|
targetManager,
|
|
|
|
title: configuration.title,
|
|
|
|
protocolConfiguration: proto,
|
|
|
|
onDemandRules: extra?.onDemandRules ?? []
|
|
|
|
)
|
|
|
|
|
|
|
|
// remove others afterwards (to avoid permission request)
|
|
|
|
await retainManagers(managers) {
|
|
|
|
$0.isTunnel(withIdentifier: tunnelBundleIdentifier)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
return targetManager
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
@discardableResult
|
2022-03-04 23:58:07 +00:00
|
|
|
private func install(
|
|
|
|
_ manager: NETunnelProviderManager,
|
|
|
|
title: String,
|
|
|
|
protocolConfiguration: NETunnelProviderProtocol,
|
2022-04-04 00:50:43 +00:00
|
|
|
onDemandRules: [NEOnDemandRule]
|
|
|
|
) async throws -> NETunnelProviderManager {
|
2022-10-12 20:28:33 +00:00
|
|
|
manager.localizedDescription = title
|
|
|
|
manager.protocolConfiguration = protocolConfiguration
|
|
|
|
|
|
|
|
if !onDemandRules.isEmpty {
|
|
|
|
manager.onDemandRules = onDemandRules
|
|
|
|
manager.isOnDemandEnabled = true
|
|
|
|
} else {
|
|
|
|
manager.isOnDemandEnabled = false
|
|
|
|
}
|
2022-04-04 00:50:43 +00:00
|
|
|
|
2022-10-12 20:28:33 +00:00
|
|
|
manager.isEnabled = true
|
|
|
|
do {
|
|
|
|
try await manager.saveToPreferences()
|
|
|
|
try await manager.loadFromPreferences()
|
|
|
|
notifyReinstall(manager)
|
|
|
|
return manager
|
|
|
|
} catch {
|
|
|
|
manager.isOnDemandEnabled = false
|
|
|
|
manager.isEnabled = false
|
|
|
|
notifyInstallError(error)
|
|
|
|
throw error
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
private func retainManagers(_ managers: [NETunnelProviderManager], isIncluded: (NETunnelProviderManager) -> Bool) async {
|
2022-10-12 20:28:33 +00:00
|
|
|
let others = managers.filter {
|
|
|
|
!isIncluded($0)
|
|
|
|
}
|
|
|
|
guard !others.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for o in others {
|
|
|
|
try? await o.removeFromPreferences()
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
private func lookupAll() async throws -> [NETunnelProviderManager] {
|
2022-10-12 20:28:33 +00:00
|
|
|
try await NETunnelProviderManager.loadAllFromPreferences()
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc private func vpnDidUpdate(_ notification: Notification) {
|
|
|
|
guard let connection = notification.object as? NETunnelProviderSession else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
notifyStatus(connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func vpnDidReinstall(_ notification: Notification) {
|
|
|
|
guard let manager = notification.object as? NETunnelProviderManager else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
notifyReinstall(manager)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
private func notifyReinstall(_ manager: NETunnelProviderManager) {
|
2023-12-22 21:06:16 +00:00
|
|
|
guard let bundleId = manager.tunnelBundleIdentifier else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.debug("VPN did reinstall (\(bundleId)): isEnabled=\(manager.isEnabled)")
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
var notification = Notification(name: VPNNotification.didReinstall)
|
|
|
|
notification.vpnBundleIdentifier = bundleId
|
|
|
|
notification.vpnIsEnabled = manager.isEnabled
|
|
|
|
NotificationCenter.default.post(notification)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
private func notifyStatus(_ connection: NETunnelProviderSession) {
|
|
|
|
guard let _ = connection.manager.localizedDescription else {
|
|
|
|
log.verbose("Ignoring VPN notification from bogus manager")
|
|
|
|
return
|
|
|
|
}
|
2023-12-22 21:06:16 +00:00
|
|
|
guard let bundleId = connection.manager.tunnelBundleIdentifier else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.debug("VPN status did change (\(bundleId)): isEnabled=\(connection.manager.isEnabled), status=\(connection.status.rawValue)")
|
2022-03-04 23:58:07 +00:00
|
|
|
var notification = Notification(name: VPNNotification.didChangeStatus)
|
|
|
|
notification.vpnBundleIdentifier = bundleId
|
|
|
|
notification.vpnIsEnabled = connection.manager.isEnabled
|
|
|
|
notification.vpnStatus = connection.status.wrappedStatus
|
2023-12-14 21:01:26 +00:00
|
|
|
notification.connectionDate = connection.connectedDate
|
2022-03-04 23:58:07 +00:00
|
|
|
NotificationCenter.default.post(notification)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-27 13:44:40 +00:00
|
|
|
private func notifyInstallError(_ error: Error) {
|
|
|
|
log.error("VPN installation failed: \(error))")
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
var notification = Notification(name: VPNNotification.didFail)
|
|
|
|
notification.vpnError = error
|
2022-04-27 13:44:40 +00:00
|
|
|
notification.vpnIsEnabled = false
|
2022-03-04 23:58:07 +00:00
|
|
|
NotificationCenter.default.post(notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NEVPNManager {
|
|
|
|
var tunnelBundleIdentifier: String? {
|
|
|
|
guard let proto = protocolConfiguration as? NETunnelProviderProtocol else {
|
2023-12-22 21:06:16 +00:00
|
|
|
log.warning("No bundle identifier found because protocolConfiguration is not NETunnelProviderProtocol (\(type(of: protocolConfiguration))")
|
2022-03-04 23:58:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return proto.providerBundleIdentifier
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
func isTunnel(withIdentifier bundleIdentifier: String) -> Bool {
|
|
|
|
return tunnelBundleIdentifier == bundleIdentifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NEVPNStatus {
|
|
|
|
var wrappedStatus: VPNStatus {
|
|
|
|
switch self {
|
|
|
|
case .connected:
|
|
|
|
return .connected
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
case .connecting, .reasserting:
|
|
|
|
return .connecting
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
case .disconnecting:
|
|
|
|
return .disconnecting
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
case .disconnected, .invalid:
|
|
|
|
return .disconnected
|
|
|
|
|
|
|
|
@unknown default:
|
|
|
|
return .disconnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|