2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// StandardVPNProvider.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/15/18.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import NetworkExtension
|
|
|
|
import TunnelKit
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public class StandardVPNProvider: VPNProvider {
|
2018-10-11 07:13:19 +00:00
|
|
|
private let bundleIdentifier: String
|
|
|
|
|
|
|
|
private var manager: NETunnelProviderManager?
|
|
|
|
|
|
|
|
private var lastNotifiedStatus: VPNStatus?
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public init(bundleIdentifier: String) {
|
2018-10-11 07:13:19 +00:00
|
|
|
self.bundleIdentifier = bundleIdentifier
|
|
|
|
|
2018-10-22 08:04:58 +00:00
|
|
|
let nc = NotificationCenter.default
|
|
|
|
nc.addObserver(self, selector: #selector(vpnDidUpdate(_:)), name: .NEVPNStatusDidChange, object: nil)
|
|
|
|
nc.addObserver(self, selector: #selector(vpnDidReinstall(_:)), name: .NEVPNConfigurationChange, object: nil)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: VPNProvider
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var isPrepared: Bool {
|
2018-10-11 07:13:19 +00:00
|
|
|
return manager != nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var isEnabled: Bool {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let manager = manager else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return manager.isEnabled && manager.isOnDemandEnabled
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public var status: VPNStatus {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let neStatus = manager?.connection.status else {
|
|
|
|
return .disconnected
|
|
|
|
}
|
|
|
|
switch neStatus {
|
|
|
|
case .connected:
|
|
|
|
return .connected
|
|
|
|
|
|
|
|
case .connecting, .reasserting:
|
|
|
|
return .connecting
|
|
|
|
|
|
|
|
case .disconnecting:
|
|
|
|
return .disconnecting
|
|
|
|
|
|
|
|
case .disconnected, .invalid:
|
|
|
|
return .disconnected
|
2019-04-11 14:56:23 +00:00
|
|
|
|
|
|
|
@unknown default:
|
|
|
|
return .disconnected
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func prepare(completionHandler: (() -> Void)?) {
|
2018-10-11 07:13:19 +00:00
|
|
|
find(with: bundleIdentifier) {
|
|
|
|
self.manager = $0
|
|
|
|
NotificationCenter.default.post(name: .VPNDidPrepare, object: nil)
|
|
|
|
completionHandler?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func install(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let configuration = configuration as? NetworkExtensionVPNConfiguration else {
|
|
|
|
fatalError("Not a NetworkExtensionVPNConfiguration")
|
|
|
|
}
|
|
|
|
find(with: bundleIdentifier) {
|
2019-03-19 16:27:42 +00:00
|
|
|
guard let manager = $0 else {
|
|
|
|
completionHandler?(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.manager = manager
|
|
|
|
manager.protocolConfiguration = configuration.protocolConfiguration
|
|
|
|
manager.onDemandRules = configuration.onDemandRules
|
|
|
|
manager.isOnDemandEnabled = true
|
|
|
|
manager.isEnabled = true
|
|
|
|
manager.saveToPreferences { (error) in
|
2018-10-11 07:13:19 +00:00
|
|
|
guard error == nil else {
|
2019-03-19 16:27:42 +00:00
|
|
|
manager.isOnDemandEnabled = false
|
|
|
|
manager.isEnabled = false
|
2018-10-11 07:13:19 +00:00
|
|
|
completionHandler?(error)
|
|
|
|
return
|
|
|
|
}
|
2019-03-19 16:27:42 +00:00
|
|
|
manager.loadFromPreferences { (error) in
|
2018-10-11 07:13:19 +00:00
|
|
|
completionHandler?(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func connect(completionHandler: ((Error?) -> Void)?) {
|
2018-10-11 07:13:19 +00:00
|
|
|
do {
|
|
|
|
try manager?.connection.startVPNTunnel()
|
|
|
|
completionHandler?(nil)
|
|
|
|
} catch let e {
|
|
|
|
completionHandler?(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func disconnect(completionHandler: ((Error?) -> Void)?) {
|
2019-03-19 16:20:03 +00:00
|
|
|
guard let manager = manager else {
|
|
|
|
completionHandler?(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
manager.connection.stopVPNTunnel()
|
|
|
|
manager.isOnDemandEnabled = false
|
|
|
|
manager.isEnabled = false
|
|
|
|
manager.saveToPreferences(completionHandler: completionHandler)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func reconnect(configuration: VPNConfiguration, completionHandler: ((Error?) -> Void)?) {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard let configuration = configuration as? NetworkExtensionVPNConfiguration else {
|
|
|
|
fatalError("Not a NetworkExtensionVPNConfiguration")
|
|
|
|
}
|
|
|
|
install(configuration: configuration) { (error) in
|
|
|
|
guard error == nil else {
|
|
|
|
completionHandler?(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let connectBlock = {
|
|
|
|
self.connect(completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
if self.status != .disconnected {
|
|
|
|
self.manager?.connection.stopVPNTunnel()
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: connectBlock)
|
|
|
|
} else {
|
|
|
|
connectBlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func uninstall(completionHandler: (() -> Void)?) {
|
2018-10-11 07:13:19 +00:00
|
|
|
find(with: bundleIdentifier) { (manager) in
|
2019-03-19 16:27:42 +00:00
|
|
|
guard let manager = manager else {
|
|
|
|
completionHandler?()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
manager.connection.stopVPNTunnel()
|
|
|
|
manager.removeFromPreferences { (error) in
|
2018-10-11 07:13:19 +00:00
|
|
|
self.manager = nil
|
|
|
|
completionHandler?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func requestDebugLog(fallback: (() -> String)?, completionHandler: @escaping (String) -> Void) {
|
2018-10-11 07:13:19 +00:00
|
|
|
guard status != .disconnected else {
|
|
|
|
completionHandler(fallback?() ?? "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
findAndRequestDebugLog { (recent) in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let recent = recent else {
|
|
|
|
completionHandler(fallback?() ?? "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completionHandler(recent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:29:28 +00:00
|
|
|
public func requestBytesCount(completionHandler: @escaping ((UInt, UInt)?) -> Void) {
|
2018-10-11 07:13:19 +00:00
|
|
|
find(with: bundleIdentifier) {
|
|
|
|
self.manager = $0
|
|
|
|
guard let session = self.manager?.connection as? NETunnelProviderSession else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try session.sendProviderMessage(TunnelKitProvider.Message.dataCount.data) { (data) in
|
|
|
|
guard let data = data, data.count == 16 else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-04-11 14:56:23 +00:00
|
|
|
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) }
|
2018-10-11 07:13:19 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler((bytesIn, bytesOut))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func find(with bundleIdentifier: String, completionHandler: @escaping (NETunnelProviderManager?) -> Void) {
|
|
|
|
NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
|
|
|
|
guard error == nil else {
|
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let manager = managers?.first {
|
|
|
|
guard let ptm = $0.protocolConfiguration as? NETunnelProviderProtocol else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (ptm.providerBundleIdentifier == bundleIdentifier)
|
|
|
|
}
|
|
|
|
completionHandler(manager ?? NETunnelProviderManager())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func findAndRequestDebugLog(completionHandler: @escaping (String?) -> Void) {
|
|
|
|
find(with: bundleIdentifier) {
|
|
|
|
self.manager = $0
|
|
|
|
guard let session = self.manager?.connection as? NETunnelProviderSession else {
|
|
|
|
completionHandler(nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
StandardVPNProvider.requestDebugLog(session: session, completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func requestDebugLog(session: NETunnelProviderSession, completionHandler: @escaping (String?) -> Void) {
|
|
|
|
do {
|
|
|
|
try session.sendProviderMessage(TunnelKitProvider.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc private func vpnDidUpdate(_ notification: Notification) {
|
|
|
|
// guard let connection = notification.object as? NETunnelProviderSession else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// log.debug("VPN status did change: \(connection.status.rawValue)")
|
|
|
|
|
|
|
|
let status = self.status
|
|
|
|
if let last = lastNotifiedStatus {
|
|
|
|
guard status != last else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastNotifiedStatus = status
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .VPNDidChangeStatus, object: self)
|
|
|
|
}
|
2018-10-22 08:04:58 +00:00
|
|
|
|
|
|
|
@objc private func vpnDidReinstall(_ notification: Notification) {
|
|
|
|
NotificationCenter.default.post(name: .VPNDidReinstall, object: self)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|