aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-12-29 01:36:46 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:29 +0530
commit6a27626fc003399b1f542b7ef64ea838001375dc (patch)
tree97306ad99b7c4fdd429a464068d3caa457f3e47e /WireGuard/WireGuard
parentmacOS: Add tunnel management menu items (diff)
downloadwireguard-apple-6a27626fc003399b1f542b7ef64ea838001375dc.tar.xz
wireguard-apple-6a27626fc003399b1f542b7ef64ea838001375dc.zip
iOS: Refactor importFromFile
So that it can be used in macOS as well Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard')
-rw-r--r--WireGuard/WireGuard/UI/ErrorPresenterProtocol.swift25
-rw-r--r--WireGuard/WireGuard/UI/TunnelImporter.swift43
-rw-r--r--WireGuard/WireGuard/UI/iOS/AppDelegate.swift3
-rw-r--r--WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift11
-rw-r--r--WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift42
5 files changed, 76 insertions, 48 deletions
diff --git a/WireGuard/WireGuard/UI/ErrorPresenterProtocol.swift b/WireGuard/WireGuard/UI/ErrorPresenterProtocol.swift
new file mode 100644
index 0000000..37e567c
--- /dev/null
+++ b/WireGuard/WireGuard/UI/ErrorPresenterProtocol.swift
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+protocol ErrorPresenterProtocol {
+ static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?, onPresented: (() -> Void)?, onDismissal: (() -> Void)?)
+}
+
+extension ErrorPresenterProtocol {
+ static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?, onPresented: (() -> Void)?) {
+ showErrorAlert(title: title, message: message, from: sourceVC, onPresented: onPresented, onDismissal: nil)
+ }
+
+ static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?, onDismissal: (() -> Void)?) {
+ showErrorAlert(title: title, message: message, from: sourceVC, onPresented: nil, onDismissal: onDismissal)
+ }
+
+ static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?) {
+ showErrorAlert(title: title, message: message, from: sourceVC, onPresented: nil, onDismissal: nil)
+ }
+
+ static func showErrorAlert(error: WireGuardAppError, from sourceVC: AnyObject?, onPresented: (() -> Void)? = nil, onDismissal: (() -> Void)? = nil) {
+ let (title, message) = error.alertText
+ showErrorAlert(title: title, message: message, from: sourceVC, onPresented: onPresented, onDismissal: onDismissal)
+ }
+}
diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift
new file mode 100644
index 0000000..4a31504
--- /dev/null
+++ b/WireGuard/WireGuard/UI/TunnelImporter.swift
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Foundation
+
+class TunnelImporter {
+ static func importFromFile(url: URL, into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) {
+ if url.pathExtension == "zip" {
+ ZipImporter.importConfigFiles(from: url) { result in
+ if let error = result.error {
+ errorPresenterType.showErrorAlert(error: error, from: sourceVC)
+ return
+ }
+ let configs = result.value!
+ tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
+ if numberSuccessful == configs.count {
+ completionHandler?()
+ return
+ }
+ let title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful)
+ let message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count)
+ errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
+ }
+ }
+ } else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
+ let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
+ if let fileContents = try? String(contentsOf: url),
+ let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) {
+ tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { result in
+ if let error = result.error {
+ errorPresenterType.showErrorAlert(error: error, from: sourceVC, onPresented: completionHandler)
+ } else {
+ completionHandler?()
+ }
+ }
+ } else {
+ errorPresenterType.showErrorAlert(title: tr("alertUnableToImportTitle"), message: tr("alertUnableToImportMessage"),
+ from: sourceVC, onPresented: completionHandler)
+ }
+ }
+ }
+
+}
diff --git a/WireGuard/WireGuard/UI/iOS/AppDelegate.swift b/WireGuard/WireGuard/UI/iOS/AppDelegate.swift
index 52c5b0a..c295002 100644
--- a/WireGuard/WireGuard/UI/iOS/AppDelegate.swift
+++ b/WireGuard/WireGuard/UI/iOS/AppDelegate.swift
@@ -27,7 +27,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
- mainVC?.tunnelsListVC?.importFromFile(url: url) {
+ guard let tunnelsManager = mainVC?.tunnelsManager else { return true }
+ TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: mainVC, errorPresenterType: ErrorPresenter.self) {
_ = FileManager.deleteFile(at: url)
}
return true
diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift
index 1746c81..7d134de 100644
--- a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift
+++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift
@@ -4,14 +4,9 @@
import UIKit
import os.log
-class ErrorPresenter {
- static func showErrorAlert(error: WireGuardAppError, from sourceVC: UIViewController?, onPresented: (() -> Void)? = nil, onDismissal: (() -> Void)? = nil) {
- let (title, message) = error.alertText
- showErrorAlert(title: title, message: message, from: sourceVC, onPresented: onPresented, onDismissal: onDismissal)
- }
-
- static func showErrorAlert(title: String, message: String, from sourceVC: UIViewController?, onPresented: (() -> Void)? = nil, onDismissal: (() -> Void)? = nil) {
- guard let sourceVC = sourceVC else { return }
+class ErrorPresenter: ErrorPresenterProtocol {
+ static func showErrorAlert(title: String, message: String, from sourceVC: AnyObject?, onPresented: (() -> Void)?, onDismissal: (() -> Void)?) {
+ guard let sourceVC = sourceVC as? UIViewController else { return }
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
onDismissal?()
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
index e6252d9..058063d 100644
--- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
@@ -157,49 +157,13 @@ class TunnelsListTableViewController: UIViewController {
scanQRCodeNC.modalPresentationStyle = .fullScreen
present(scanQRCodeNC, animated: true)
}
-
- func importFromFile(url: URL, completionHandler: (() -> Void)?) {
- guard let tunnelsManager = tunnelsManager else { return }
- if url.pathExtension == "zip" {
- ZipImporter.importConfigFiles(from: url) { [weak self] result in
- if let error = result.error {
- ErrorPresenter.showErrorAlert(error: error, from: self)
- return
- }
- let configs = result.value!
- tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { [weak self] numberSuccessful in
- if numberSuccessful == configs.count {
- completionHandler?()
- return
- }
- let title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful)
- let message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count)
- ErrorPresenter.showErrorAlert(title: title, message: message, from: self, onPresented: completionHandler)
- }
- }
- } else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
- let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
- if let fileContents = try? String(contentsOf: url),
- let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) {
- tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] result in
- if let error = result.error {
- ErrorPresenter.showErrorAlert(error: error, from: self, onPresented: completionHandler)
- } else {
- completionHandler?()
- }
- }
- } else {
- ErrorPresenter.showErrorAlert(title: tr("alertUnableToImportTitle"), message: tr("alertUnableToImportMessage"),
- from: self, onPresented: completionHandler)
- }
- }
- }
}
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
- urls.forEach {
- importFromFile(url: $0, completionHandler: nil)
+ guard let tunnelsManager = tunnelsManager else { return }
+ urls.forEach { url in
+ TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: self, errorPresenterType: ErrorPresenter.self)
}
}
}