2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
// AppCoordinator.swift
|
2018-05-24 18:13:21 +00:00
|
|
|
// WireGuard
|
2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 23-05-18.
|
2018-07-15 09:55:41 +00:00
|
|
|
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
|
2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-08-04 19:28:19 +00:00
|
|
|
import NetworkExtension
|
|
|
|
import KeychainSwift
|
|
|
|
import os.log
|
2018-05-23 19:49:10 +00:00
|
|
|
|
|
|
|
import CoreData
|
|
|
|
import BNRCoreDataStack
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
extension UINavigationController: Identifyable {}
|
|
|
|
|
2018-08-04 19:28:19 +00:00
|
|
|
let APPGROUP = "group.com.wireguard.ios.WireGuard"
|
|
|
|
let VPNBUNDLE = "com.wireguard.ios.WireGuard.WireGuardNetworkExtension"
|
|
|
|
|
2018-05-23 19:49:10 +00:00
|
|
|
class AppCoordinator: RootViewCoordinator {
|
|
|
|
|
2018-05-24 18:13:21 +00:00
|
|
|
let persistentContainer = NSPersistentContainer(name: "WireGuard")
|
2018-05-23 19:49:10 +00:00
|
|
|
let storyboard = UIStoryboard(name: "Main", bundle: nil)
|
2018-08-04 19:28:19 +00:00
|
|
|
var currentManager: NETunnelProviderManager?
|
2018-05-23 19:49:10 +00:00
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
var childCoordinators: [Coordinator] = []
|
|
|
|
|
|
|
|
var rootViewController: UIViewController {
|
2018-05-26 07:17:36 +00:00
|
|
|
return self.tunnelsTableViewController
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:28:19 +00:00
|
|
|
var status = NEVPNStatus.invalid {
|
|
|
|
didSet {
|
2018-08-04 20:49:03 +00:00
|
|
|
//TODO: signal status
|
2018-08-04 19:28:19 +00:00
|
|
|
switch status {
|
|
|
|
case .connected:
|
|
|
|
os_log("Connected VPN", log: Log.general, type: .info)
|
|
|
|
case .connecting, .disconnecting, .reasserting:
|
|
|
|
os_log("Connecting VPN", log: Log.general, type: .info)
|
|
|
|
case .disconnected, .invalid:
|
|
|
|
os_log("Disconnecting VPN", log: Log.general, type: .info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
var tunnelsTableViewController: TunnelsTableViewController!
|
2018-05-23 19:49:10 +00:00
|
|
|
|
|
|
|
/// Window to manage
|
|
|
|
let window: UIWindow
|
|
|
|
|
|
|
|
let navigationController: UINavigationController = {
|
2018-05-26 07:17:36 +00:00
|
|
|
let navController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(type: UINavigationController.self)
|
2018-05-23 19:49:10 +00:00
|
|
|
return navController
|
|
|
|
}()
|
|
|
|
|
|
|
|
// MARK: - Init
|
|
|
|
public init(window: UIWindow) {
|
|
|
|
self.window = window
|
|
|
|
|
|
|
|
self.window.rootViewController = self.navigationController
|
|
|
|
self.window.makeKeyAndVisible()
|
2018-08-04 19:28:19 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
selector: #selector(VPNStatusDidChange(notification:)),
|
|
|
|
name: .NEVPNStatusDidChange,
|
|
|
|
object: nil)
|
|
|
|
reloadCurrentManager(nil)
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Functions
|
|
|
|
|
|
|
|
/// Starts the coordinator
|
|
|
|
public func start() {
|
|
|
|
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
|
|
|
|
persistentContainer.loadPersistentStores { [weak self] (_, error) in
|
|
|
|
if let error = error {
|
|
|
|
print("Unable to Load Persistent Store. \(error), \(error.localizedDescription)")
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
//start
|
2018-05-26 07:17:36 +00:00
|
|
|
if let tunnelsTableViewController = self?.storyboard.instantiateViewController(type: TunnelsTableViewController.self) {
|
|
|
|
self?.tunnelsTableViewController = tunnelsTableViewController
|
|
|
|
self?.tunnelsTableViewController.viewContext = self?.persistentContainer.viewContext
|
|
|
|
self?.tunnelsTableViewController.delegate = self
|
|
|
|
self?.navigationController.viewControllers = [tunnelsTableViewController]
|
2018-05-23 19:49:10 +00:00
|
|
|
do {
|
2018-05-26 07:17:36 +00:00
|
|
|
if let context = self?.persistentContainer.viewContext, try Tunnel.countInContext(context) == 0 {
|
|
|
|
print("No tunnels ... yet")
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
self?.showError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:28:19 +00:00
|
|
|
// MARK: - NEVPNManager handling
|
|
|
|
|
|
|
|
func configureVPN(_ configure: @escaping (NETunnelProviderManager) -> NETunnelProviderProtocol?, completionHandler: @escaping (Error?) -> Void) {
|
|
|
|
reloadCurrentManager { (error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("error reloading preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
completionHandler(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let manager = self.currentManager!
|
|
|
|
if let protocolConfiguration = configure(manager) {
|
|
|
|
manager.protocolConfiguration = protocolConfiguration
|
|
|
|
}
|
|
|
|
manager.isEnabled = true
|
|
|
|
|
|
|
|
manager.saveToPreferences { (error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("error saving preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
completionHandler(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os_log("saved preferences", log: Log.general, type: .info)
|
|
|
|
self.reloadCurrentManager(completionHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reloadCurrentManager(_ completionHandler: ((Error?) -> Void)?) {
|
|
|
|
NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
|
|
|
|
if let error = error {
|
|
|
|
completionHandler?(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var manager: NETunnelProviderManager?
|
|
|
|
|
|
|
|
for man in managers! {
|
|
|
|
if let prot = man.protocolConfiguration as? NETunnelProviderProtocol {
|
|
|
|
if prot.providerBundleIdentifier == VPNBUNDLE {
|
|
|
|
manager = man
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager == nil {
|
|
|
|
manager = NETunnelProviderManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.currentManager = manager
|
|
|
|
self.status = manager!.connection.status
|
|
|
|
completionHandler?(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func VPNStatusDidChange(notification: NSNotification) {
|
|
|
|
guard let status = currentManager?.connection.status else {
|
|
|
|
os_log("VPNStatusDidChange", log: Log.general, type: .debug)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os_log("VPNStatusDidChange: %{public}@", log: Log.general, type: .debug, description(for: status))
|
|
|
|
self.status = status
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:49:10 +00:00
|
|
|
public func showError(_ error: Error) {
|
|
|
|
showAlert(title: NSLocalizedString("Error", comment: "Error alert title"), message: error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func showAlert(title: String, message: String) {
|
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK button"), style: .default))
|
|
|
|
self.navigationController.present(alert, animated: true)
|
|
|
|
}
|
2018-08-04 19:28:19 +00:00
|
|
|
|
|
|
|
private func description(for status: NEVPNStatus) -> String {
|
|
|
|
switch status {
|
|
|
|
case .connected:
|
|
|
|
return "Connected"
|
|
|
|
case .connecting:
|
|
|
|
return "Connecting"
|
|
|
|
case .disconnected:
|
|
|
|
return "Disconnected"
|
|
|
|
case .disconnecting:
|
|
|
|
return "Disconnecting"
|
|
|
|
case .invalid:
|
|
|
|
return "Invalid"
|
|
|
|
case .reasserting:
|
|
|
|
return "Reasserting"
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
extension AppCoordinator: TunnelsTableViewControllerDelegate {
|
|
|
|
func addProvider(tunnelsTableViewController: TunnelsTableViewController) {
|
2018-05-24 20:07:04 +00:00
|
|
|
let addContext = persistentContainer.newBackgroundContext()
|
2018-05-26 07:17:36 +00:00
|
|
|
showTunnelConfigurationViewController(tunnel: nil, context: addContext)
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:28:19 +00:00
|
|
|
func connect(tunnel: Tunnel?, tunnelsTableViewController: TunnelsTableViewController) {
|
|
|
|
let block = {
|
|
|
|
switch self.status {
|
|
|
|
case .invalid, .disconnected:
|
|
|
|
self.connect(tunnel: tunnel)
|
|
|
|
|
|
|
|
case .connected, .connecting:
|
|
|
|
// TODO: this needs to check if the passed tunnel is the actual connected tunnel config
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == .invalid {
|
|
|
|
reloadCurrentManager({ (_) in
|
|
|
|
block()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
block()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func connect(tunnel: Tunnel?) {
|
|
|
|
// TODO implement NETunnelProviderManager VC showing current connection status, pushing this config into VPN stack
|
|
|
|
os_log("connect tunnel: %{public}@", log: Log.general, type: .info, tunnel?.description ?? "-none-")
|
|
|
|
|
|
|
|
guard let tunnel = tunnel else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configureVPN({ (_) in
|
|
|
|
//TODO: decide what to do with on demand
|
|
|
|
// self.currentManager?.isOnDemandEnabled = true
|
|
|
|
self.currentManager?.onDemandRules = [NEOnDemandRuleConnect()]
|
|
|
|
|
|
|
|
let protocolConfiguration = NETunnelProviderProtocol()
|
|
|
|
let keychain = KeychainSwift()
|
|
|
|
keychain.accessGroup = APPGROUP
|
|
|
|
//TODO: Set secrets to keychain?
|
|
|
|
|
|
|
|
protocolConfiguration.providerBundleIdentifier = VPNBUNDLE
|
|
|
|
//TODO obtain endpoint hostname
|
|
|
|
// protocolConfiguration.serverAddress = endpoint.hostname
|
2018-08-04 20:49:37 +00:00
|
|
|
protocolConfiguration.serverAddress = "168.192.0.1"
|
2018-08-04 19:28:19 +00:00
|
|
|
//TODO obtain endpoint username
|
|
|
|
// protocolConfiguration.username = endpoint.username
|
|
|
|
//TODO: how to obtain this?
|
|
|
|
// protocolConfiguration.passwordReference = try? keychain.passwordReference(for: endpoint.username)
|
|
|
|
protocolConfiguration.providerConfiguration = tunnel.generateProviderConfiguration()
|
|
|
|
|
|
|
|
return protocolConfiguration
|
|
|
|
}, completionHandler: { (error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("configure error: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let session = self.currentManager?.connection as! NETunnelProviderSession //swiftlint:disable:this force_cast
|
|
|
|
do {
|
|
|
|
try session.startTunnel()
|
|
|
|
} catch let error {
|
|
|
|
os_log("error starting tunnel: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func disconnect() {
|
|
|
|
configureVPN({ (_) in
|
|
|
|
//TODO: decide what to do with on demand
|
|
|
|
// self.currentManager?.isOnDemandEnabled = false
|
|
|
|
return nil
|
|
|
|
}, completionHandler: { (_) in
|
|
|
|
self.currentManager?.connection.stopVPNTunnel()
|
|
|
|
})
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
func configure(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
|
|
|
|
print("configure tunnel \(tunnel)")
|
2018-05-24 20:07:04 +00:00
|
|
|
let editContext = persistentContainer.newBackgroundContext()
|
2018-05-26 07:17:36 +00:00
|
|
|
var backgroundTunnel: Tunnel?
|
2018-05-24 20:07:04 +00:00
|
|
|
editContext.performAndWait {
|
2018-05-26 07:17:36 +00:00
|
|
|
backgroundTunnel = editContext.object(with: tunnel.objectID) as? Tunnel
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
showTunnelConfigurationViewController(tunnel: backgroundTunnel, context: editContext)
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
func showTunnelConfigurationViewController(tunnel: Tunnel?, context: NSManagedObjectContext) {
|
|
|
|
let tunnelConfigurationViewController = storyboard.instantiateViewController(type: TunnelConfigurationTableViewController.self)
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
tunnelConfigurationViewController.configure(context: context, delegate: self, tunnel: tunnel)
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
self.navigationController.pushViewController(tunnelConfigurationViewController, animated: true)
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
|
|
|
|
print("delete tunnel \(tunnel)")
|
2018-05-29 20:21:44 +00:00
|
|
|
|
|
|
|
if let moc = tunnel.managedObjectContext {
|
|
|
|
moc.perform {
|
|
|
|
moc.delete(tunnel)
|
|
|
|
moc.saveContextToStore()
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
extension AppCoordinator: TunnelConfigurationTableViewControllerDelegate {
|
2018-05-27 20:38:44 +00:00
|
|
|
func didSave(tunnel: Tunnel, tunnelConfigurationTableViewController: TunnelConfigurationTableViewController) {
|
|
|
|
navigationController.popToRootViewController(animated: true)
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
|
|
|
|
}
|