]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
on-demand: iOS: Add ability to add current SSID
authorRoopesh Chander <roop@roopc.net>
Tue, 5 Mar 2019 13:07:53 +0000 (18:37 +0530)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 18 Mar 2019 05:46:55 +0000 (06:46 +0100)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/Base.lproj/Localizable.strings
WireGuard/WireGuard/UI/iOS/ViewController/SSIDOptionEditTableViewController.swift

index 41130bb34df5eca9c5ee3bd82a2a410d89d8fa1b..30b633c502f78b1a87c7cd5bd62edc5ca5331c57 100644 (file)
@@ -95,7 +95,8 @@
 "tunnelOnDemandSelectionViewTitle" = "Select SSIDs";
 "tunnelOnDemandSectionTitleSelectedSSIDs" = "Selected SSIDs";
 "tunnelOnDemandSectionTitleAddSSIDs" = "Add SSIDs";
-"tunnelOnDemandAddMessageAddNew" = "Add manually";
+"tunnelOnDemandAddMessageAddNewSSID" = "Add manually";
+"tunnelOnDemandAddMessageAddConnectedSSID (%@)" = "Connected: %@";
 
 "tunnelOnDemandKey" = "Activate on demand";
 "tunnelOnDemandOptionOff" = "Off";
index 7027d34b675a0b576bdab8784d5a495cbd21bac2..f83d4789df07dc4eb5462e38a03df302980aa267 100644 (file)
@@ -2,6 +2,7 @@
 // Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
 
 import UIKit
+import SystemConfiguration.CaptiveNetwork
 
 protocol SSIDOptionEditTableViewControllerDelegate: class {
     func ssidOptionSaved(option: ActivateOnDemandViewModel.OnDemandSSIDOption, ssids: [String])
@@ -14,9 +15,15 @@ class SSIDOptionEditTableViewController: UITableViewController {
         case addSSIDs
     }
 
+    private enum AddSSIDRow {
+        case addConnectedSSID(connectedSSID: String)
+        case addNewSSID
+    }
+
     weak var delegate: SSIDOptionEditTableViewControllerDelegate?
 
     private var sections = [Section]()
+    private var addSSIDRows = [AddSSIDRow]()
 
     let ssidOptionFields: [ActivateOnDemandViewModel.OnDemandSSIDOption] = [
         .anySSID,
@@ -26,12 +33,15 @@ class SSIDOptionEditTableViewController: UITableViewController {
 
     var selectedOption: ActivateOnDemandViewModel.OnDemandSSIDOption
     var selectedSSIDs: [String]
+    var connectedSSID: String?
 
     init(option: ActivateOnDemandViewModel.OnDemandSSIDOption, ssids: [String]) {
         selectedOption = option
         selectedSSIDs = ssids
         super.init(style: .grouped)
+        connectedSSID = getConnectedSSID()
         loadSections()
+        loadAddSSIDRows()
     }
 
     required init?(coder aDecoder: NSCoder) {
@@ -63,6 +73,30 @@ class SSIDOptionEditTableViewController: UITableViewController {
         }
     }
 
+    func loadAddSSIDRows() {
+        addSSIDRows.removeAll()
+        if let connectedSSID = connectedSSID {
+            if !selectedSSIDs.contains(connectedSSID) {
+                addSSIDRows.append(.addConnectedSSID(connectedSSID: connectedSSID))
+            }
+        }
+        addSSIDRows.append(.addNewSSID)
+    }
+
+    func updateTableViewAddSSIDRows() {
+        guard let addSSIDSection = sections.firstIndex(of: .addSSIDs) else { return }
+        let numberOfAddSSIDRows = addSSIDRows.count
+        let numberOfAddSSIDRowsInTableView = tableView.numberOfRows(inSection: addSSIDSection)
+        switch (numberOfAddSSIDRowsInTableView, numberOfAddSSIDRows) {
+        case (1, 2):
+            tableView.insertRows(at: [IndexPath(row: 0, section: addSSIDSection)], with: .automatic)
+        case (2, 1):
+            tableView.deleteRows(at: [IndexPath(row: 0, section: addSSIDSection)], with: .automatic)
+        default:
+            break
+        }
+    }
+
     override func viewWillDisappear(_ animated: Bool) {
         delegate?.ssidOptionSaved(option: selectedOption, ssids: selectedSSIDs)
     }
@@ -80,7 +114,7 @@ extension SSIDOptionEditTableViewController {
         case .selectedSSIDs:
             return selectedSSIDs.count
         case .addSSIDs:
-            return 1
+            return addSSIDRows.count
         }
     }
 
@@ -143,6 +177,8 @@ extension SSIDOptionEditTableViewController {
             guard let self = self, let cell = cell else { return }
             if let row = self.tableView.indexPath(for: cell)?.row {
                 self.selectedSSIDs[row] = text
+                self.loadAddSSIDRows()
+                self.updateTableViewAddSSIDRows()
             }
         }
         return cell
@@ -150,7 +186,12 @@ extension SSIDOptionEditTableViewController {
 
     private func addSSIDCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
         let cell: TextCell = tableView.dequeueReusableCell(for: indexPath)
-        cell.message = tr("tunnelOnDemandAddMessageAddNew")
+        switch addSSIDRows[indexPath.row] {
+        case .addConnectedSSID:
+            cell.message = tr(format: "tunnelOnDemandAddMessageAddConnectedSSID (%@)", connectedSSID!)
+        case .addNewSSID:
+            cell.message = tr("tunnelOnDemandAddMessageAddNewSSID")
+        }
         cell.isEditing = true
         return cell
     }
@@ -169,10 +210,19 @@ extension SSIDOptionEditTableViewController {
             } else {
                 tableView.deleteSections(IndexSet(integer: indexPath.section), with: .automatic)
             }
+            loadAddSSIDRows()
+            updateTableViewAddSSIDRows()
         case .addSSIDs:
             assert(editingStyle == .insert)
             let hasSelectedSSIDsSection = sections.contains(.selectedSSIDs)
-            selectedSSIDs.append("")
+            let newSSID: String
+            switch addSSIDRows[indexPath.row] {
+            case .addConnectedSSID(let connectedSSID):
+                newSSID = connectedSSID
+            case .addNewSSID:
+                newSSID = ""
+            }
+            selectedSSIDs.append(newSSID)
             loadSections()
             let selectedSSIDsSection = sections.firstIndex(of: .selectedSSIDs)!
             let indexPath = IndexPath(row: selectedSSIDs.count - 1, section: selectedSSIDsSection)
@@ -181,8 +231,12 @@ extension SSIDOptionEditTableViewController {
             } else {
                 tableView.insertRows(at: [indexPath], with: .automatic)
             }
-            if let selectedSSIDCell = tableView.cellForRow(at: indexPath) as? EditableTextCell {
-                selectedSSIDCell.beginEditing()
+            loadAddSSIDRows()
+            updateTableViewAddSSIDRows()
+            if newSSID.isEmpty {
+                if let selectedSSIDCell = tableView.cellForRow(at: indexPath) as? EditableTextCell {
+                    selectedSSIDCell.beginEditing()
+                }
             }
         }
     }
@@ -225,3 +279,15 @@ extension SSIDOptionEditTableViewController {
         }
     }
 }
+
+private func getConnectedSSID() -> String? {
+    guard let supportedInterfaces = CNCopySupportedInterfaces() as? [CFString] else { return nil }
+    for interface in supportedInterfaces {
+        if let networkInfo = CNCopyCurrentNetworkInfo(interface) {
+            if let ssid = (networkInfo as NSDictionary)[kCNNetworkInfoKeySSID as String] as? String {
+                return !ssid.isEmpty ? ssid : nil
+            }
+        }
+    }
+    return nil
+}