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

This commit is contained in:
Roopesh Chander 2018-10-25 11:10:18 +05:30
parent 44a2eff1e0
commit 4e2a1aa07a
4 changed files with 67 additions and 6 deletions

View File

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

View File

@ -2,11 +2,13 @@
// Copyright © 2018 WireGuard LLC. All rights reserved.
import UIKit
import os.log
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainVC: MainViewController?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
@ -15,9 +17,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window.backgroundColor = UIColor.white
self.window = window
window.rootViewController = MainViewController()
let mainVC = MainViewController()
window.rootViewController = mainVC
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
}
}

View File

@ -4,6 +4,8 @@
import UIKit
class MainViewController: UISplitViewController {
var tunnelsListVC: TunnelsListTableViewController?
override func loadView() {
let detailVC = UIViewController()
let detailNC = UINavigationController(rootViewController: detailVC)
@ -14,6 +16,8 @@ class MainViewController: UISplitViewController {
self.viewControllers = [ masterNC, detailNC ]
super.loadView()
tunnelsListVC = masterVC
}
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
self.preferredDisplayMode = .allVisible
}
func openForEditing(configFileURL: URL) {
tunnelsListVC?.openForEditing(configFileURL: configFileURL)
}
}
extension MainViewController: UISplitViewControllerDelegate {

View File

@ -6,6 +6,7 @@ import UIKit
class TunnelsListTableViewController: UITableViewController {
var tunnelsManager: TunnelsManager? = nil
var onTunnelsManagerReady: ((TunnelsManager) -> Void)? = nil
init() {
super.init(style: .plain)
@ -28,6 +29,8 @@ class TunnelsListTableViewController: UITableViewController {
if let s = self {
tunnelsManager.delegate = s
s.tunnelsManager = tunnelsManager
s.onTunnelsManagerReady?(tunnelsManager)
s.onTunnelsManagerReady = nil
s.tableView.reloadData()
}
}
@ -40,10 +43,7 @@ class TunnelsListTableViewController: UITableViewController {
alert.addAction(
UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in
if let s = self, let tunnelsManager = s.tunnelsManager {
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager)
editVC.delegate = s
let editNC = UINavigationController(rootViewController: editVC)
s.present(editNC, animated: true)
s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil)
}
}
)
@ -54,6 +54,41 @@ class TunnelsListTableViewController: UITableViewController {
alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
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