]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Settings: Export log file
authorRoopesh Chander <roop@roopc.net>
Thu, 29 Nov 2018 19:25:42 +0000 (00:55 +0530)
committerRoopesh Chander <roop@roopc.net>
Sun, 2 Dec 2018 07:56:53 +0000 (13:26 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift

index b003b08154164e586c836c13b7522bdc7f37d909..994eb222fe4102f54a5ec9df3121db89d12b71b4 100644 (file)
@@ -10,11 +10,13 @@ class SettingsTableViewController: UITableViewController {
         case iosAppVersion = "WireGuard for iOS"
         case goBackendVersion = "WireGuard Go Backend"
         case exportZipArchive = "Export zip archive"
+        case exportLogFile = "Export log file"
     }
 
     let settingsFieldsBySection: [[SettingsFields]] = [
         [.iosAppVersion, .goBackendVersion],
-        [.exportZipArchive]
+        [.exportZipArchive],
+        [.exportLogFile]
     ]
 
     let tunnelsManager: TunnelsManager?
@@ -89,6 +91,44 @@ class SettingsTableViewController: UITableViewController {
         }
     }
 
+    func exportLogForLastActivatedTunnel(sourceView: UIView) {
+        guard let destinationDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
+            return
+        }
+
+        let destinationURL = destinationDir.appendingPathComponent("WireGuard_iOS_log.txt")
+
+        if (FileManager.default.fileExists(atPath: destinationURL.path)) {
+            do {
+                try FileManager.default.removeItem(at: destinationURL)
+            } catch {
+                os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription)
+                showErrorAlert(title: "No log available", message: "The pre-existing log could not be cleared")
+                return
+            }
+        }
+
+        guard let networkExtensionLogFileURL = FileManager.networkExtensionLogFileURL,
+            FileManager.default.fileExists(atPath: networkExtensionLogFileURL.path) else {
+                showErrorAlert(title: "No log available", message: "Please activate a tunnel and then export the log")
+                return
+        }
+
+        do {
+            try FileManager.default.copyItem(at: networkExtensionLogFileURL, to: destinationURL)
+        } catch {
+            os_log("Failed to copy file: %{public}@ to %{public}@: %{public}@", log: OSLog.default, type: .error, networkExtensionLogFileURL.absoluteString, destinationURL.absoluteString, error.localizedDescription)
+            showErrorAlert(title: "No log available", message: "The log could not be accessed")
+            return
+        }
+
+        let activityVC = UIActivityViewController(activityItems: [destinationURL], applicationActivities: nil)
+        // popoverPresentationController shall be non-nil on the iPad
+        activityVC.popoverPresentationController?.sourceView = sourceView
+        activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
+        self.present(activityVC, animated: true)
+    }
+
     func showErrorAlert(title: String, message: String) {
         let okAction = UIAlertAction(title: "OK", style: .default)
         let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
@@ -115,6 +155,8 @@ extension SettingsTableViewController {
             return "About"
         case 1:
             return "Export configurations"
+        case 2:
+            return "Tunnel log"
         default:
             return nil
         }
@@ -135,14 +177,21 @@ extension SettingsTableViewController {
                 cell.value = WIREGUARD_GO_VERSION
             }
             return cell
-        } else {
-            assert(field == .exportZipArchive)
+        } else if (field == .exportZipArchive) {
             let cell = tableView.dequeueReusableCell(withIdentifier: TunnelSettingsTableViewButtonCell.id, for: indexPath) as! TunnelSettingsTableViewButtonCell
             cell.buttonText = field.rawValue
             cell.onTapped = { [weak self] in
                 self?.exportConfigurationsAsZipFile(sourceView: cell.button)
             }
             return cell
+        } else {
+            assert(field == .exportLogFile)
+            let cell = tableView.dequeueReusableCell(withIdentifier: TunnelSettingsTableViewButtonCell.id, for: indexPath) as! TunnelSettingsTableViewButtonCell
+            cell.buttonText = field.rawValue
+            cell.onTapped = { [weak self] in
+                self?.exportLogForLastActivatedTunnel(sourceView: cell.button)
+            }
+            return cell
         }
     }
 }