aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-10-24 14:11:34 +0530
committerRoopesh Chander <roop@roopc.net>2018-10-27 15:13:01 +0530
commit571e9b582647ecca9cb679ed30333c2e74959e54 (patch)
treea6270ef8792278331a1d2627a11481288d792cb1
parentTunnel view model: Populate interface's public key (diff)
downloadwireguard-apple-571e9b582647ecca9cb679ed30333c2e74959e54.tar.xz
wireguard-apple-571e9b582647ecca9cb679ed30333c2e74959e54.zip
Tunnel creation: Compute public key from private key as the text is being edited
Signed-off-by: Roopesh Chander <roop@roopc.net>
-rw-r--r--WireGuard/WireGuard/UI/TunnelViewModel.swift12
-rw-r--r--WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift19
2 files changed, 31 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift
index b28abeb..e7ab12d 100644
--- a/WireGuard/WireGuard/UI/TunnelViewModel.swift
+++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift
@@ -27,6 +27,8 @@ class TunnelViewModel {
case deletePeer = "Delete peer"
}
+ static let keyLengthInBase64 = 44
+
class InterfaceData {
var scratchpad: [InterfaceField: String] = [:]
var fieldsWithError: Set<InterfaceField> = []
@@ -53,6 +55,16 @@ class TunnelViewModel {
} else {
scratchpad[field] = stringValue
}
+ if (field == .privateKey) {
+ if (stringValue.count == TunnelViewModel.keyLengthInBase64),
+ let privateKey = Data(base64Encoded: stringValue),
+ privateKey.count == 32 {
+ let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey)
+ scratchpad[.publicKey] = publicKey.base64EncodedString()
+ } else {
+ scratchpad.removeValue(forKey: .publicKey)
+ }
+ }
}
}
diff --git a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift
index 89446e9..f203e09 100644
--- a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift
@@ -160,6 +160,17 @@ extension TunnelEditTableViewController {
cell.onValueChanged = { [weak interfaceData] value in
interfaceData?[field] = value
}
+ // Compute public key live
+ if (field == .privateKey) {
+ cell.onValueBeingEdited = { [weak self, weak interfaceData] value in
+ if let interfaceData = interfaceData, let s = self {
+ interfaceData[.privateKey] = value
+ if let row = s.interfaceFieldsBySection[section].firstIndex(of: .publicKey) {
+ s.tableView.reloadRows(at: [IndexPath(row: row, section: section)], with: .none)
+ }
+ }
+ }
+ }
return cell
}
} else if ((numberOfPeers > 0) && (section < (numberOfInterfaceSections + numberOfPeers * numberOfPeerSections))) {
@@ -289,6 +300,7 @@ class TunnelsEditTableViewKeyValueCell: UITableViewCell {
}
var onValueChanged: ((String) -> Void)? = nil
+ var onValueBeingEdited: ((String) -> Void)? = nil
let keyLabel: UILabel
let valueTextField: UITextField
@@ -352,6 +364,13 @@ extension TunnelsEditTableViewKeyValueCell: UITextFieldDelegate {
onValueChanged(textField.text ?? "")
}
}
+ func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
+ if let onValueBeingEdited = onValueBeingEdited {
+ let modifiedText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
+ onValueBeingEdited(modifiedText)
+ }
+ return true
+ }
}
class TunnelsEditTableViewButtonCell: UITableViewCell {