Import: Support for opening a .conf file in the app

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-10-25 11:10:18 +05:30
parent 112b5c3b10
commit 05b9d6972b
4 changed files with 67 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import Foundation
@available(OSX 10.14, iOS 12.0, *) @available(OSX 10.14, iOS 12.0, *)
class TunnelConfiguration: Codable { class TunnelConfiguration: Codable {
let interface: InterfaceConfiguration var interface: InterfaceConfiguration
var peers: [PeerConfiguration] = [] var peers: [PeerConfiguration] = []
init(interface: InterfaceConfiguration) { init(interface: InterfaceConfiguration) {
self.interface = interface self.interface = interface

View File

@ -2,11 +2,13 @@
// Copyright © 2018 WireGuard LLC. All rights reserved. // Copyright © 2018 WireGuard LLC. All rights reserved.
import UIKit import UIKit
import os.log
@UIApplicationMain @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var window: UIWindow?
var mainVC: MainViewController?
func application(_ application: UIApplication, func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
@ -15,9 +17,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window.backgroundColor = UIColor.white window.backgroundColor = UIColor.white
self.window = window self.window = window
window.rootViewController = MainViewController() let mainVC = MainViewController()
window.rootViewController = mainVC
window.makeKeyAndVisible() window.makeKeyAndVisible()
self.mainVC = mainVC
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
// Based on importing code by Jeroen Leenarts <jeroen.leenarts@gmail.com> in commit 815f12c
defer {
do {
try FileManager.default.removeItem(at: url)
} catch {
os_log("Failed to remove item from Inbox: %{public}@", log: OSLog.default, type: .debug, url.absoluteString)
}
}
mainVC?.openForEditing(configFileURL: url)
return true return true
} }
} }

View File

@ -4,6 +4,8 @@
import UIKit import UIKit
class MainViewController: UISplitViewController { class MainViewController: UISplitViewController {
var tunnelsListVC: TunnelsListTableViewController?
override func loadView() { override func loadView() {
let detailVC = UIViewController() let detailVC = UIViewController()
let detailNC = UINavigationController(rootViewController: detailVC) let detailNC = UINavigationController(rootViewController: detailVC)
@ -14,6 +16,8 @@ class MainViewController: UISplitViewController {
self.viewControllers = [ masterNC, detailNC ] self.viewControllers = [ masterNC, detailNC ]
super.loadView() super.loadView()
tunnelsListVC = masterVC
} }
override func viewDidLoad() { override func viewDidLoad() {
@ -22,6 +26,10 @@ class MainViewController: UISplitViewController {
// On iPad, always show both masterVC and detailVC, even in portrait mode, like the Settings app // On iPad, always show both masterVC and detailVC, even in portrait mode, like the Settings app
self.preferredDisplayMode = .allVisible self.preferredDisplayMode = .allVisible
} }
func openForEditing(configFileURL: URL) {
tunnelsListVC?.openForEditing(configFileURL: configFileURL)
}
} }
extension MainViewController: UISplitViewControllerDelegate { extension MainViewController: UISplitViewControllerDelegate {

View File

@ -6,6 +6,7 @@ import UIKit
class TunnelsListTableViewController: UITableViewController { class TunnelsListTableViewController: UITableViewController {
var tunnelsManager: TunnelsManager? = nil var tunnelsManager: TunnelsManager? = nil
var onTunnelsManagerReady: ((TunnelsManager) -> Void)? = nil
init() { init() {
super.init(style: .plain) super.init(style: .plain)
@ -28,6 +29,8 @@ class TunnelsListTableViewController: UITableViewController {
if let s = self { if let s = self {
tunnelsManager.delegate = s tunnelsManager.delegate = s
s.tunnelsManager = tunnelsManager s.tunnelsManager = tunnelsManager
s.onTunnelsManagerReady?(tunnelsManager)
s.onTunnelsManagerReady = nil
s.tableView.reloadData() s.tableView.reloadData()
} }
} }
@ -40,10 +43,7 @@ class TunnelsListTableViewController: UITableViewController {
alert.addAction( alert.addAction(
UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in
if let s = self, let tunnelsManager = s.tunnelsManager { if let s = self, let tunnelsManager = s.tunnelsManager {
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager) s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil)
editVC.delegate = s
let editNC = UINavigationController(rootViewController: editVC)
s.present(editNC, animated: true)
} }
} }
) )
@ -54,6 +54,41 @@ class TunnelsListTableViewController: UITableViewController {
alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
self.present(alert, animated: true, completion: nil) self.present(alert, animated: true, completion: nil)
} }
func openForEditing(configFileURL: URL) {
let tunnelConfiguration: TunnelConfiguration?
let name = configFileURL.deletingPathExtension().lastPathComponent
do {
let fileContents = try String(contentsOf: configFileURL)
try tunnelConfiguration = WgQuickConfigFileParser.parse(fileContents)
} catch {
showErrorAlert(title: "Could not import config", message: "There was an error importing the config file")
return
}
tunnelConfiguration?.interface.name = name
if let tunnelsManager = tunnelsManager {
presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: tunnelConfiguration)
} else {
onTunnelsManagerReady = { [weak self] tunnelsManager in
self?.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: tunnelConfiguration)
}
}
}
func presentViewControllerForTunnelCreation(tunnelsManager: TunnelsManager, tunnelConfiguration: TunnelConfiguration?) {
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnelConfiguration: tunnelConfiguration)
editVC.delegate = self
let editNC = UINavigationController(rootViewController: editVC)
self.present(editNC, animated: true)
}
func showErrorAlert(title: String, message: String) {
let okAction = UIAlertAction(title: "Ok", style: .default)
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
} }
// MARK: TunnelEditTableViewControllerDelegate // MARK: TunnelEditTableViewControllerDelegate