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-08 05:04:42 +00:00
|
|
|
var providerManagers: [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
|
|
|
|
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)
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Functions
|
|
|
|
|
|
|
|
/// Starts the coordinator
|
|
|
|
public func start() {
|
2018-08-08 05:04:42 +00:00
|
|
|
NETunnelProviderManager.loadAllFromPreferences { [weak self] (managers, error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("Unable to load provider managers: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
}
|
|
|
|
self?.providerManagers = managers
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:49:10 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
@objc private func VPNStatusDidChange(notification: NSNotification) {
|
|
|
|
}
|
|
|
|
|
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-08 05:04:42 +00:00
|
|
|
func connect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
|
|
|
|
let manager = self.providerManager(for: tunnel)!
|
2018-08-04 19:28:19 +00:00
|
|
|
let block = {
|
2018-08-08 05:04:42 +00:00
|
|
|
switch manager.connection.status {
|
2018-08-04 19:28:19 +00:00
|
|
|
case .invalid, .disconnected:
|
|
|
|
self.connect(tunnel: tunnel)
|
|
|
|
|
|
|
|
case .connected, .connecting:
|
2018-08-08 05:04:42 +00:00
|
|
|
self.disconnect(tunnel: tunnel)
|
2018-08-04 19:28:19 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
if manager.connection.status == .invalid {
|
|
|
|
manager.loadFromPreferences { (_) in
|
2018-08-04 19:28:19 +00:00
|
|
|
block()
|
2018-08-08 05:04:42 +00:00
|
|
|
}
|
2018-08-04 19:28:19 +00:00
|
|
|
} else {
|
|
|
|
block()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
private func connect(tunnel: Tunnel) {
|
|
|
|
os_log("connect tunnel: %{public}@", log: Log.general, type: .info, tunnel.description)
|
|
|
|
// Should the manager be enabled?
|
2018-08-04 19:28:19 +00:00
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
let manager = providerManager(for: tunnel)
|
|
|
|
let session = manager?.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)
|
2018-08-04 19:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
func disconnect(tunnel: Tunnel) {
|
|
|
|
let manager = providerManager(for: tunnel)
|
|
|
|
manager?.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-08-08 05:04:42 +00:00
|
|
|
let manager = providerManager(for: tunnel)
|
|
|
|
manager?.removeFromPreferences { (error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("error removing preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os_log("removed preferences", log: Log.general, type: .info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func providerManager(for tunnel: Tunnel) -> NETunnelProviderManager? {
|
|
|
|
return self.providerManagers?.first {
|
|
|
|
guard let prot = $0.protocolConfiguration as? NETunnelProviderProtocol else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard let tunnelIdentifier = prot.providerConfiguration?["tunnelIdentifier"] as? String else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return tunnelIdentifier == tunnel.tunnelIdentifier
|
2018-05-29 20:21:44 +00:00
|
|
|
}
|
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) {
|
2018-08-08 05:04:42 +00:00
|
|
|
let manager = providerManager(for: tunnel) ?? NETunnelProviderManager()
|
|
|
|
manager.localizedDescription = tunnel.title
|
|
|
|
|
|
|
|
let protocolConfiguration = NETunnelProviderProtocol()
|
|
|
|
protocolConfiguration.providerBundleIdentifier = VPNBUNDLE
|
|
|
|
protocolConfiguration.serverAddress = (tunnel.peers?.array as? [Peer])?.compactMap { $0.endpoint}.joined(separator: ", ")
|
|
|
|
//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()
|
|
|
|
|
|
|
|
manager.protocolConfiguration = protocolConfiguration
|
|
|
|
manager.onDemandRules = [NEOnDemandRuleConnect()]
|
|
|
|
|
|
|
|
manager.saveToPreferences { (error) in
|
|
|
|
if let error = error {
|
|
|
|
os_log("error saving preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os_log("saved preferences", log: Log.general, type: .info)
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
navigationController.popToRootViewController(animated: true)
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
|
|
|
|
}
|