From db7b24dd02dee992a2e937526836e4fd0b540a92 Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Tue, 29 May 2018 22:21:44 +0200 Subject: [PATCH] Allow adding unvalidated config. Signed-off-by: Jason A. Donenfeld --- WireGuard/Base.lproj/Main.storyboard | 30 ++++++++- WireGuard/Coordinators/AppCoordinator.swift | 10 ++- .../Models/Interface+CoreDataProperties.swift | 1 + .../Models/Tunnel+CoreDataProperties.swift | 24 ++++++- .../WireGuard.xcdatamodel/contents | 7 +- ...nnelConfigurationTableViewController.swift | 65 +++++++++++++++++-- 6 files changed, 121 insertions(+), 16 deletions(-) diff --git a/WireGuard/Base.lproj/Main.storyboard b/WireGuard/Base.lproj/Main.storyboard index 9c0b04f..56def8c 100644 --- a/WireGuard/Base.lproj/Main.storyboard +++ b/WireGuard/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -118,6 +118,7 @@ + @@ -141,6 +142,7 @@ + @@ -167,6 +169,7 @@ + @@ -190,6 +193,7 @@ + @@ -210,6 +214,7 @@ + @@ -235,6 +240,7 @@ + @@ -255,6 +261,7 @@ + @@ -277,6 +284,15 @@ + + + + + + + + + @@ -347,6 +363,7 @@ + @@ -367,6 +384,7 @@ + @@ -387,6 +405,7 @@ + @@ -410,6 +429,7 @@ + @@ -430,6 +450,7 @@ + @@ -450,6 +471,13 @@ + + + + + + + diff --git a/WireGuard/Coordinators/AppCoordinator.swift b/WireGuard/Coordinators/AppCoordinator.swift index f6209a1..e916a8a 100644 --- a/WireGuard/Coordinators/AppCoordinator.swift +++ b/WireGuard/Coordinators/AppCoordinator.swift @@ -97,12 +97,10 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate { } func configure(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) { - // TODO implement print("configure tunnel \(tunnel)") let editContext = persistentContainer.newBackgroundContext() var backgroundTunnel: Tunnel? editContext.performAndWait { - backgroundTunnel = editContext.object(with: tunnel.objectID) as? Tunnel } @@ -118,8 +116,14 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate { } func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) { - // TODO implement print("delete tunnel \(tunnel)") + + if let moc = tunnel.managedObjectContext { + moc.perform { + moc.delete(tunnel) + moc.saveContextToStore() + } + } } } diff --git a/WireGuard/Models/Interface+CoreDataProperties.swift b/WireGuard/Models/Interface+CoreDataProperties.swift index 2461514..5fef193 100644 --- a/WireGuard/Models/Interface+CoreDataProperties.swift +++ b/WireGuard/Models/Interface+CoreDataProperties.swift @@ -23,6 +23,7 @@ extension Interface { @NSManaged public var dns: String? @NSManaged public var table: String? @NSManaged public var tunnel: Tunnel? + @NSManaged public var publicKey: String? @NSManaged public var adresses: NSSet? } diff --git a/WireGuard/Models/Tunnel+CoreDataProperties.swift b/WireGuard/Models/Tunnel+CoreDataProperties.swift index 2c7c741..c1a40eb 100644 --- a/WireGuard/Models/Tunnel+CoreDataProperties.swift +++ b/WireGuard/Models/Tunnel+CoreDataProperties.swift @@ -17,14 +17,32 @@ extension Tunnel { } @NSManaged public var title: String? - @NSManaged public var peers: NSSet? @NSManaged public var interface: Interface? + @NSManaged public var peers: NSOrderedSet? } // MARK: Generated accessors for peers extension Tunnel { + @objc(insertObject:inPeersAtIndex:) + @NSManaged public func insertIntoPeers(_ value: Peer, at idx: Int) + + @objc(removeObjectFromPeersAtIndex:) + @NSManaged public func removeFromPeers(at idx: Int) + + @objc(insertPeers:atIndexes:) + @NSManaged public func insertIntoPeers(_ values: [Peer], at indexes: NSIndexSet) + + @objc(removePeersAtIndexes:) + @NSManaged public func removeFromPeers(at indexes: NSIndexSet) + + @objc(replaceObjectInPeersAtIndex:withObject:) + @NSManaged public func replacePeers(at idx: Int, with value: Peer) + + @objc(replacePeersAtIndexes:withPeers:) + @NSManaged public func replacePeers(at indexes: NSIndexSet, with values: [Peer]) + @objc(addPeersObject:) @NSManaged public func addToPeers(_ value: Peer) @@ -32,9 +50,9 @@ extension Tunnel { @NSManaged public func removeFromPeers(_ value: Peer) @objc(addPeers:) - @NSManaged public func addToPeers(_ values: NSSet) + @NSManaged public func addToPeers(_ values: NSOrderedSet) @objc(removePeers:) - @NSManaged public func removeFromPeers(_ values: NSSet) + @NSManaged public func removeFromPeers(_ values: NSOrderedSet) } diff --git a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents index c8197f9..1af139b 100644 --- a/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents +++ b/WireGuard/Models/WireGuard.xcdatamodeld/WireGuard.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -10,6 +10,7 @@ + @@ -25,11 +26,11 @@ - + - + diff --git a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift index 64ad85d..9a3f10d 100644 --- a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift +++ b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift @@ -61,9 +61,19 @@ class TunnelConfigurationTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.section { case 0: - return tableView.dequeueReusableCell(type: InterfaceTableViewCell.self, for: indexPath) + let cell = tableView.dequeueReusableCell(type: InterfaceTableViewCell.self, for: indexPath) + cell.model = tunnel.interface + return cell case 1: - return tableView.dequeueReusableCell(type: PeerTableViewCell.self, for: indexPath) + let cell = tableView.dequeueReusableCell(type: PeerTableViewCell.self, for: indexPath) + if let peer = tunnel.peers?.object(at: indexPath.row) as? Peer { + cell.peer = peer + } else { + let peer = Peer(context: tunnel.managedObjectContext!) + tunnel.addToPeers(peer) + cell.peer = peer + } + return cell default: return tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath) } @@ -104,12 +114,38 @@ class InterfaceTableViewCell: UITableViewCell { } extension InterfaceTableViewCell: UITextFieldDelegate { - func textFieldDidBeginEditing(_ textField: UITextField) { - print("\(textField) \(textField.text)") + @IBAction + func textfieldDidChange(_ sender: UITextField) { + let string = sender.text + print(string) + + if sender == nameField { + model.tunnel?.title = string + } else if sender == privateKeyField { + model.privateKey = string + } else if sender == publicKeyField { + model.publicKey = string + } else if sender == addressesField { + let address = Address(context: model.managedObjectContext!) + address.address = string + model.adresses = NSSet(array: [address]) + } else if sender == listenPortField { + if let string = string, let port = Int16(string) { + model.listenPort = port + } + + } else if sender == dnsField { + model.dns = string + } else if sender == mtuField { + if let string = string, let mtu = Int32(string) { + model.mtu = mtu + } + } } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { print("\(string)") + return true } } @@ -126,12 +162,29 @@ class PeerTableViewCell: UITableViewCell { } extension PeerTableViewCell: UITextFieldDelegate { - func textFieldDidBeginEditing(_ textField: UITextField) { - print("\(textField) \(textField.text)") + @IBAction + func textfieldDidChange(_ sender: UITextField) { + let string = sender.text + print(string) + + if sender == publicKeyField { + peer.publicKey = string + } else if sender == preSharedKeyField { + peer.presharedKey = string + } else if sender == allowedIpsField { + peer.allowedIPs = string + } else if sender == endpointField { + peer.endpoint = string + } else if sender == persistentKeepaliveField { + if let string = string, let persistentKeepalive = Int16(string) { + peer.persistentKeepalive = persistentKeepalive + } + } } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { print("\(string)") + return true } }