tunnelkit/Sources/TunnelKitManager/MockVPN.swift

131 lines
3.6 KiB
Swift
Raw Normal View History

//
// MockVPN.swift
// TunnelKit
//
// Created by Davide De Rosa on 6/15/18.
2023-03-17 15:58:36 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
//
// 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
}
2023-04-20 19:52:45 +00:00
// MARK: VPN
2023-04-20 19:52:45 +00:00
public func prepare() {
}
2023-04-20 19:52:45 +00:00
public func install(
_ tunnelBundleIdentifier: String,
configuration: NetworkExtensionConfiguration,
2022-10-12 20:35:02 +00:00
extra: NetworkExtensionExtra?
) {
2022-10-12 20:35:02 +00:00
self.tunnelBundleIdentifier = tunnelBundleIdentifier
2022-10-15 06:31:47 +00:00
isEnabled = true
vpnStatus = .disconnected
}
2023-04-20 19:52:45 +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
}
2023-04-20 19:52:45 +00:00
public func reconnect(
_ tunnelBundleIdentifier: String,
configuration: NetworkExtensionConfiguration,
2022-10-12 20:35:02 +00:00
extra: NetworkExtensionExtra?,
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
}
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
}
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
}
2023-04-20 19:52:45 +00:00
// MARK: Helpers
2023-04-20 19:52:45 +00:00
private func notifyReinstall(_ isEnabled: Bool) {
var notification = Notification(name: VPNNotification.didReinstall)
2022-10-12 20:35:02 +00:00
notification.vpnBundleIdentifier = tunnelBundleIdentifier
notification.vpnIsEnabled = isEnabled
NotificationCenter.default.post(notification)
}
2023-04-20 19:52:45 +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
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)
}
}