aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-10-29 01:52:43 +0530
committerRoopesh Chander <roop@roopc.net>2018-10-29 01:52:43 +0530
commit3356cfd6888f89b5aeb8fbc02c08495891ce9edf (patch)
tree24df254eaac5dd0994151a69e15c0408ed923e3d /WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
parentZip: Add zip file reader (diff)
downloadwireguard-apple-3356cfd6888f89b5aeb8fbc02c08495891ce9edf.tar.xz
wireguard-apple-3356cfd6888f89b5aeb8fbc02c08495891ce9edf.zip
Zip: Import configs from zip files
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift')
-rw-r--r--WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift32
1 files changed, 29 insertions, 3 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
index 5d8646c..b760176 100644
--- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift
@@ -2,6 +2,7 @@
// Copyright © 2018 WireGuard LLC. All rights reserved.
import UIKit
+import MobileCoreServices
class TunnelsListTableViewController: UITableViewController {
@@ -42,7 +43,7 @@ class TunnelsListTableViewController: UITableViewController {
let alert = UIAlertController(title: "",
message: "Add a tunnel",
preferredStyle: .actionSheet)
- let importFileAction = UIAlertAction(title: "Import wg-quick config (.conf)", style: .default) { [weak self] (action) in
+ let importFileAction = UIAlertAction(title: "Import file or archive", style: .default) { [weak self] (action) in
self?.presentViewControllerForFileImport()
}
alert.addAction(importFileAction)
@@ -96,7 +97,8 @@ class TunnelsListTableViewController: UITableViewController {
}
func presentViewControllerForFileImport() {
- let filePicker = FileImportViewController(documentTypes: [.wgQuickConfigFile])
+ let documentTypes = ["com.wireguard.config.quick", String(kUTTypeZipArchive)]
+ let filePicker = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
filePicker.delegate = self
self.present(filePicker, animated: true)
}
@@ -138,7 +140,31 @@ extension TunnelsListTableViewController: TunnelEditTableViewControllerDelegate
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let url = urls.first {
- openForEditing(configFileURL: url)
+ if (url.pathExtension == "conf") {
+ openForEditing(configFileURL: url)
+ } else if (url.pathExtension == "zip") {
+ var unarchivedFiles: [(fileName: String, contents: Data)] = []
+ do {
+ unarchivedFiles = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"])
+ } catch ZipArchiveError.cantOpenInputZipFile {
+ showErrorAlert(title: "Cannot read zip archive", message: "The zip file couldn't be read")
+ } catch ZipArchiveError.badArchive {
+ showErrorAlert(title: "Cannot read zip archive", message: "Bad archive")
+ } catch (let error) {
+ print("Error opening zip archive: \(error)")
+ }
+ for unarchivedFile in unarchivedFiles {
+ if let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent,
+ let fileContents = String(data: unarchivedFile.contents, encoding: .utf8),
+ let tunnelConfiguration = try? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) {
+ tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (tunnel, error) in
+ if (error != nil) {
+ print("Error adding configuration: \(tunnelConfiguration.interface.name)")
+ }
+ }
+ }
+ }
+ }
}
}
}