2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// MockVPN.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
|
|
|
|
|
|
|
|
/// Simulates a VPN provider.
|
|
|
|
public class MockVPN: VPN {
|
2022-10-12 20:35:02 +00:00
|
|
|
private var tunnelBundleIdentifier: String?
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-15 06:31:47 +00:00
|
|
|
private var isEnabled: Bool {
|
|
|
|
didSet {
|
|
|
|
notifyReinstall(isEnabled)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-15 06:31:47 +00:00
|
|
|
private var vpnStatus: VPNStatus {
|
|
|
|
didSet {
|
|
|
|
notifyStatus(vpnStatus)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-12 20:35:02 +00:00
|
|
|
private let delayNanoseconds: UInt64
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-12 20:35:02 +00:00
|
|
|
public init(delay: Int = 1) {
|
|
|
|
delayNanoseconds = DispatchTimeInterval.seconds(delay).nanoseconds
|
2022-10-15 06:31:47 +00:00
|
|
|
isEnabled = false
|
|
|
|
vpnStatus = .disconnected
|
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: VPN
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
public func prepare() {
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
public func install(
|
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: NetworkExtensionConfiguration,
|
2022-10-12 20:35:02 +00:00
|
|
|
extra: NetworkExtensionExtra?
|
2022-04-04 00:50:43 +00:00
|
|
|
) {
|
2022-10-12 20:35:02 +00:00
|
|
|
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
2022-10-15 06:31:47 +00:00
|
|
|
isEnabled = true
|
|
|
|
vpnStatus = .disconnected
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-07-23 07:58:48 +00:00
|
|
|
public func reconnect(after: DispatchTimeInterval) async throws {
|
2022-10-15 06:31:47 +00:00
|
|
|
if vpnStatus == .connected {
|
|
|
|
vpnStatus = .disconnecting
|
|
|
|
await delay()
|
|
|
|
}
|
|
|
|
vpnStatus = .connecting
|
2022-10-12 20:35:02 +00:00
|
|
|
await delay()
|
2022-10-15 06:31:47 +00:00
|
|
|
vpnStatus = .connected
|
2022-07-23 07:58:48 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-04-04 00:50:43 +00:00
|
|
|
public func reconnect(
|
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: NetworkExtensionConfiguration,
|
2022-10-12 20:35:02 +00:00
|
|
|
extra: NetworkExtensionExtra?,
|
2022-04-04 00:50:43 +00:00
|
|
|
after: DispatchTimeInterval
|
2022-10-12 20:35:02 +00:00
|
|
|
) async throws {
|
|
|
|
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
2022-10-15 06:31:47 +00:00
|
|
|
isEnabled = true
|
|
|
|
if vpnStatus == .connected {
|
|
|
|
vpnStatus = .disconnecting
|
|
|
|
await delay()
|
|
|
|
}
|
|
|
|
vpnStatus = .connecting
|
2022-10-12 20:35:02 +00:00
|
|
|
await delay()
|
2022-10-15 06:31:47 +00:00
|
|
|
vpnStatus = .connected
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-12 20:35:02 +00:00
|
|
|
public func disconnect() async {
|
2022-10-15 06:31:47 +00:00
|
|
|
guard vpnStatus != .disconnected else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vpnStatus = .disconnecting
|
2022-10-12 20:35:02 +00:00
|
|
|
await delay()
|
2022-10-15 06:31:47 +00:00
|
|
|
vpnStatus = .disconnected
|
|
|
|
isEnabled = false
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-12 20:35:02 +00:00
|
|
|
public func uninstall() async {
|
2022-10-15 06:31:47 +00:00
|
|
|
vpnStatus = .disconnected
|
|
|
|
isEnabled = false
|
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: Helpers
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
private func notifyReinstall(_ isEnabled: Bool) {
|
|
|
|
var notification = Notification(name: VPNNotification.didReinstall)
|
2022-10-12 20:35:02 +00:00
|
|
|
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
2022-03-04 23:58:07 +00:00
|
|
|
notification.vpnIsEnabled = isEnabled
|
|
|
|
NotificationCenter.default.post(notification)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
private func notifyStatus(_ status: VPNStatus) {
|
|
|
|
var notification = Notification(name: VPNNotification.didChangeStatus)
|
2022-10-12 20:35:02 +00:00
|
|
|
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
2022-10-15 06:31:47 +00:00
|
|
|
notification.vpnIsEnabled = isEnabled
|
2022-03-04 23:58:07 +00:00
|
|
|
notification.vpnStatus = status
|
|
|
|
NotificationCenter.default.post(notification)
|
|
|
|
}
|
2022-10-12 20:35:02 +00:00
|
|
|
|
|
|
|
private func delay() async {
|
|
|
|
try? await Task.sleep(nanoseconds: delayNanoseconds)
|
|
|
|
}
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|