2020-06-12 21:08:25 +00:00
|
|
|
//
|
2020-11-14 17:08:09 +00:00
|
|
|
// OpenVPNProvider.swift
|
2020-06-12 21:08:25 +00:00
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/15/18.
|
2020-12-27 16:29:39 +00:00
|
|
|
// Copyright (c) 2021 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
|
|
|
|
import NetworkExtension
|
|
|
|
|
2021-08-26 17:58:39 +00:00
|
|
|
/// `VPNProvider` for OpenVPN protocol.
|
2021-08-25 19:02:45 +00:00
|
|
|
public class OpenVPNProvider: VPNProvider, VPNProviderIPC {
|
|
|
|
private let provider: NetworkExtensionVPNProvider
|
2020-06-12 21:08:25 +00:00
|
|
|
|
2021-08-26 17:58:39 +00:00
|
|
|
/**
|
|
|
|
Initializes a provider with the bundle identifier of the `OpenVPNTunnelProvider`.
|
|
|
|
|
|
|
|
- Parameter bundleIdentifier: The bundle identifier of the `OpenVPNTunnelProvider`.
|
|
|
|
*/
|
2020-06-12 21:08:25 +00:00
|
|
|
public init(bundleIdentifier: String) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider = NetworkExtensionVPNProvider(locator: NetworkExtensionTunnelLocator(bundleIdentifier: bundleIdentifier))
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
2021-08-25 19:02:45 +00:00
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
// MARK: VPNProvider
|
|
|
|
|
|
|
|
public var isPrepared: Bool {
|
2021-08-25 19:02:45 +00:00
|
|
|
return provider.isPrepared
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var isEnabled: Bool {
|
2021-08-25 19:02:45 +00:00
|
|
|
return provider.isEnabled
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var status: VPNStatus {
|
2021-08-25 19:02:45 +00:00
|
|
|
return provider.status
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func prepare(completionHandler: (() -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.prepare(completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func install(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.install(configuration: configuration, completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func connect(completionHandler: ((Error?) -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.connect(completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func disconnect(completionHandler: ((Error?) -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.disconnect(completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func reconnect(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.reconnect(configuration: configuration, completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func uninstall(completionHandler: (() -> Void)?) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.uninstall(completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 19:02:45 +00:00
|
|
|
// MARK: VPNProviderIPC
|
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
public func requestDebugLog(fallback: (() -> String)?, completionHandler: @escaping (String) -> Void) {
|
2021-08-25 19:02:45 +00:00
|
|
|
guard provider.status != .disconnected else {
|
2020-06-12 21:08:25 +00:00
|
|
|
completionHandler(fallback?() ?? "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
findAndRequestDebugLog { (recent) in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let recent = recent else {
|
|
|
|
completionHandler(fallback?() ?? "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completionHandler(recent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:02:45 +00:00
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
public func requestBytesCount(completionHandler: @escaping ((UInt, UInt)?) -> Void) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.lookup { manager, error in
|
|
|
|
guard let session = manager?.connection as? NETunnelProviderSession else {
|
2020-06-12 21:08:25 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try session.sendProviderMessage(OpenVPNTunnelProvider.Message.dataCount.data) { (data) in
|
|
|
|
guard let data = data, data.count == 16 else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let bytesIn: UInt = data.subdata(in: 0..<8).withUnsafeBytes { $0.load(as: UInt.self) }
|
|
|
|
let bytesOut: UInt = data.subdata(in: 8..<16).withUnsafeBytes { $0.load(as: UInt.self) }
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler((bytesIn, bytesOut))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:02:45 +00:00
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
public func requestServerConfiguration(completionHandler: @escaping (Any?) -> Void) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.lookup { manager, error in
|
|
|
|
guard let session = manager?.connection as? NETunnelProviderSession else {
|
2020-06-12 21:08:25 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try session.sendProviderMessage(OpenVPNTunnelProvider.Message.serverConfiguration.data) { (data) in
|
|
|
|
guard let data = data, let cfg = try? JSONDecoder().decode(OpenVPN.Configuration.self, from: data) else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:02:45 +00:00
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func findAndRequestDebugLog(completionHandler: @escaping (String?) -> Void) {
|
2021-08-25 19:02:45 +00:00
|
|
|
provider.lookup { manager, error in
|
|
|
|
guard let session = manager?.connection as? NETunnelProviderSession else {
|
2020-06-12 21:08:25 +00:00
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 21:07:03 +00:00
|
|
|
OpenVPNProvider.requestDebugLog(session: session, completionHandler: completionHandler)
|
2020-06-12 21:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:02:45 +00:00
|
|
|
|
2020-06-12 21:08:25 +00:00
|
|
|
private static func requestDebugLog(session: NETunnelProviderSession, completionHandler: @escaping (String?) -> Void) {
|
|
|
|
do {
|
|
|
|
try session.sendProviderMessage(OpenVPNTunnelProvider.Message.requestLog.data) { (data) in
|
|
|
|
guard let data = data, !data.isEmpty else {
|
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let newestLog = String(data: data, encoding: .utf8)
|
|
|
|
completionHandler(newestLog)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|