diff --git a/.jazzy.yaml b/.jazzy.yaml index 54e006d..b8f6cef 100644 --- a/.jazzy.yaml +++ b/.jazzy.yaml @@ -43,16 +43,19 @@ custom_categories: - NETCPSocket - NETunnelInterface - NEUDPSocket - - NSNotification - name: Manager children: - VPN - - VPNProvider - - MockVPNProvider - VPNConfiguration - - NetworkExtensionVPNConfiguration + - VPNProvider + - VPNProviderIPC - VPNStatus - - NSNotification + - NetworkExtensionLocator + - NetworkExtensionNativeLocator + - NetworkExtensionTunnelLocator + - NetworkExtensionVPNConfiguration + - NetworkExtensionVPNProvider + - MockVPNProvider - name: Protocols/OpenVPN children: - OpenVPN diff --git a/TunnelKit/Sources/Manager/MockVPNProvider.swift b/TunnelKit/Sources/Manager/MockVPNProvider.swift index 3344e43..37d7ccf 100644 --- a/TunnelKit/Sources/Manager/MockVPNProvider.swift +++ b/TunnelKit/Sources/Manager/MockVPNProvider.swift @@ -25,7 +25,7 @@ import Foundation -/// :nodoc: +/// Simulates a VPN provider. public class MockVPNProvider: VPNProvider, VPNProviderIPC { // MARK: VPNProvider diff --git a/TunnelKit/Sources/Manager/NetworkExtensionLocator.swift b/TunnelKit/Sources/Manager/NetworkExtensionLocator.swift index eee3311..b866d09 100644 --- a/TunnelKit/Sources/Manager/NetworkExtensionLocator.swift +++ b/TunnelKit/Sources/Manager/NetworkExtensionLocator.swift @@ -26,6 +26,7 @@ import Foundation import NetworkExtension +/// Entity able to look up a `NEVPNManager`. public protocol NetworkExtensionLocator { /** @@ -36,8 +37,10 @@ public protocol NetworkExtensionLocator { func lookup(completionHandler: @escaping (NEVPNManager?, Error?) -> Void) } +/// Locator for native VPN protocols. public class NetworkExtensionNativeLocator: NetworkExtensionLocator { + /// :nodoc: public init() { } @@ -55,9 +58,15 @@ public class NetworkExtensionNativeLocator: NetworkExtensionLocator { } } +/// Locator for tunnel VPN protocols. public class NetworkExtensionTunnelLocator: NetworkExtensionLocator { private let bundleIdentifier: String + /** + Initializes the locator with the bundle identifier of the tunnel provider. + + - Parameter bundleIdentifier: The bundle identifier of the tunnel provider. + */ public init(bundleIdentifier: String) { self.bundleIdentifier = bundleIdentifier } diff --git a/TunnelKit/Sources/Manager/NetworkExtensionVPNProvider.swift b/TunnelKit/Sources/Manager/NetworkExtensionVPNProvider.swift index c2eb669..334fb3d 100644 --- a/TunnelKit/Sources/Manager/NetworkExtensionVPNProvider.swift +++ b/TunnelKit/Sources/Manager/NetworkExtensionVPNProvider.swift @@ -29,6 +29,7 @@ import SwiftyBeaver private let log = SwiftyBeaver.self +/// `VPNProvider` based on the NetworkExtension framework. public class NetworkExtensionVPNProvider: VPNProvider { private var manager: NEVPNManager? @@ -36,6 +37,11 @@ public class NetworkExtensionVPNProvider: VPNProvider { private var lastNotifiedStatus: VPNStatus? + /** + Initializes a provider with a `NetworkExtensionLocator`. + + - Parameter locator: A `NetworkExtensionLocator` able to locate a `NEVPNManager`. + */ public init(locator: NetworkExtensionLocator) { self.locator = locator @@ -177,7 +183,7 @@ public class NetworkExtensionVPNProvider: VPNProvider { // MARK: Helpers - public func lookup(completionHandler: @escaping (NEVPNManager?, Error?) -> Void) { + func lookup(completionHandler: @escaping (NEVPNManager?, Error?) -> Void) { locator.lookup(completionHandler: completionHandler) } diff --git a/TunnelKit/Sources/Manager/VPNProviderIPC.swift b/TunnelKit/Sources/Manager/VPNProviderIPC.swift index 6a6c5ad..22b7110 100644 --- a/TunnelKit/Sources/Manager/VPNProviderIPC.swift +++ b/TunnelKit/Sources/Manager/VPNProviderIPC.swift @@ -25,6 +25,7 @@ import Foundation +/// Common IPC functions supported by interactive VPN providers. public protocol VPNProviderIPC { /** diff --git a/TunnelKit/Sources/Protocols/Native/NativeProvider.swift b/TunnelKit/Sources/Protocols/Native/NativeProvider.swift new file mode 100644 index 0000000..9b7bde1 --- /dev/null +++ b/TunnelKit/Sources/Protocols/Native/NativeProvider.swift @@ -0,0 +1,74 @@ +// +// NativeProvider.swift +// TunnelKit +// +// Created by Davide De Rosa on 4/11/21. +// Copyright (c) 2021 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 . +// + +import Foundation + +/// `VPNProvider` for native IPSec/IKEv2 configurations. +public class NativeProvider: VPNProvider { + private let provider: NetworkExtensionVPNProvider + + /// :nodoc: + public init() { + provider = NetworkExtensionVPNProvider(locator: NetworkExtensionNativeLocator()) + } + + // MARK: VPNProvider + + public var isPrepared: Bool { + return provider.isPrepared + } + + public var isEnabled: Bool { + return provider.isEnabled + } + + public var status: VPNStatus { + return provider.status + } + + public func prepare(completionHandler: (() -> Void)?) { + provider.prepare(completionHandler: completionHandler) + } + + public func install(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) { + provider.install(configuration: configuration, completionHandler: completionHandler) + } + + public func connect(completionHandler: ((Error?) -> Void)?) { + provider.connect(completionHandler: completionHandler) + } + + public func disconnect(completionHandler: ((Error?) -> Void)?) { + provider.disconnect(completionHandler: completionHandler) + } + + public func reconnect(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) { + provider.reconnect(configuration: configuration, completionHandler: completionHandler) + } + + public func uninstall(completionHandler: (() -> Void)?) { + provider.uninstall(completionHandler: completionHandler) + } +} diff --git a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNProvider.swift b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNProvider.swift index 9e01483..d9a0a1f 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNProvider.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNProvider.swift @@ -26,10 +26,15 @@ import Foundation import NetworkExtension -/// :nodoc: +/// `VPNProvider` for OpenVPN protocol. public class OpenVPNProvider: VPNProvider, VPNProviderIPC { private let provider: NetworkExtensionVPNProvider + /** + Initializes a provider with the bundle identifier of the `OpenVPNTunnelProvider`. + + - Parameter bundleIdentifier: The bundle identifier of the `OpenVPNTunnelProvider`. + */ public init(bundleIdentifier: String) { provider = NetworkExtensionVPNProvider(locator: NetworkExtensionTunnelLocator(bundleIdentifier: bundleIdentifier)) }