2020-06-12 21:08:25 +00:00
|
|
|
//
|
|
|
|
// VPN.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
2022-03-04 23:58:07 +00:00
|
|
|
// Created by Davide De Rosa on 9/6/18.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2020-06-12 21:08:25 +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
|
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// Helps controlling a VPN without messing with underlying implementations.
|
|
|
|
public protocol VPN {
|
|
|
|
associatedtype Configuration
|
2020-06-13 13:30:48 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
associatedtype Extra
|
|
|
|
|
|
|
|
/**
|
|
|
|
Synchronizes with the current VPN state.
|
|
|
|
*/
|
2022-04-04 00:50:43 +00:00
|
|
|
func prepare() async
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Installs the VPN profile.
|
|
|
|
|
|
|
|
- Parameter tunnelBundleIdentifier: The bundle identifier of the tunnel extension.
|
|
|
|
- Parameter configuration: The configuration to install.
|
|
|
|
- Parameter extra: Optional extra arguments.
|
|
|
|
*/
|
|
|
|
func install(
|
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: Configuration,
|
2022-04-04 00:50:43 +00:00
|
|
|
extra: Extra?
|
|
|
|
) async throws
|
2020-06-13 13:30:48 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/**
|
2022-07-23 07:58:48 +00:00
|
|
|
Reconnects to the VPN with current configuration.
|
|
|
|
|
|
|
|
- Parameter after: The reconnection delay.
|
|
|
|
**/
|
|
|
|
func reconnect(
|
|
|
|
after: DispatchTimeInterval
|
|
|
|
) async throws
|
|
|
|
|
|
|
|
/**
|
|
|
|
Reconnects to the VPN installing a new configuration.
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
- Parameter tunnelBundleIdentifier: The bundle identifier of the tunnel extension.
|
|
|
|
- Parameter configuration: The configuration to install.
|
|
|
|
- Parameter extra: Optional extra arguments.
|
2022-04-04 00:50:43 +00:00
|
|
|
- Parameter after: The reconnection delay.
|
2022-03-04 23:58:07 +00:00
|
|
|
*/
|
|
|
|
func reconnect(
|
|
|
|
_ tunnelBundleIdentifier: String,
|
|
|
|
configuration: Configuration,
|
|
|
|
extra: Extra?,
|
2022-04-04 00:50:43 +00:00
|
|
|
after: DispatchTimeInterval
|
|
|
|
) async throws
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Disconnects from the VPN.
|
|
|
|
*/
|
2022-04-04 00:50:43 +00:00
|
|
|
func disconnect() async
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Uninstalls the VPN profile.
|
|
|
|
*/
|
2022-04-04 00:50:43 +00:00
|
|
|
func uninstall() async
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DispatchTimeInterval {
|
2022-09-23 19:45:04 +00:00
|
|
|
|
|
|
|
/// Returns self in nanoseconds.
|
2022-04-04 00:50:43 +00:00
|
|
|
public var nanoseconds: UInt64 {
|
|
|
|
switch self {
|
|
|
|
case .seconds(let sec):
|
|
|
|
return UInt64(sec) * NSEC_PER_SEC
|
|
|
|
|
|
|
|
case .milliseconds(let msec):
|
|
|
|
return UInt64(msec) * NSEC_PER_MSEC
|
|
|
|
|
|
|
|
case .microseconds(let usec):
|
|
|
|
return UInt64(usec) * NSEC_PER_USEC
|
|
|
|
|
|
|
|
case .nanoseconds(let nsec):
|
|
|
|
return UInt64(nsec)
|
|
|
|
|
|
|
|
case .never:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@unknown default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|