2018-05-24 20:07:04 +00:00
|
|
|
//
|
2018-09-06 05:42:23 +00:00
|
|
|
// Copyright © 2018 WireGuard LLC. All rights reserved.
|
2018-05-24 20:07:04 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
import BNRCoreDataStack
|
2018-05-27 20:38:44 +00:00
|
|
|
import PromiseKit
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
protocol TunnelConfigurationTableViewControllerDelegate: class {
|
2018-05-27 20:38:44 +00:00
|
|
|
func didSave(tunnel: Tunnel, tunnelConfigurationTableViewController: TunnelConfigurationTableViewController)
|
2018-09-18 19:47:43 +00:00
|
|
|
func showSettings()
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
class TunnelConfigurationTableViewController: UITableViewController {
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var saveButton: UIBarButtonItem!
|
|
|
|
|
|
|
|
private var viewContext: NSManagedObjectContext!
|
|
|
|
private weak var delegate: TunnelConfigurationTableViewControllerDelegate?
|
|
|
|
private var tunnel: Tunnel!
|
|
|
|
|
2018-09-19 22:43:57 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
// Get rid of seperator lines in table.
|
|
|
|
tableView.tableFooterView = UIView(frame: CGRect.zero)
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
func configure(context: NSManagedObjectContext, delegate: TunnelConfigurationTableViewControllerDelegate? = nil, tunnel: Tunnel? = nil) {
|
|
|
|
viewContext = context
|
|
|
|
self.delegate = delegate
|
|
|
|
self.tunnel = tunnel ?? generateNewTunnelConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func generateNewTunnelConfig() -> Tunnel {
|
|
|
|
var tunnel: Tunnel! = nil
|
|
|
|
|
|
|
|
viewContext.performAndWait {
|
|
|
|
tunnel = Tunnel(context: viewContext)
|
2018-08-08 04:59:56 +00:00
|
|
|
tunnel.tunnelIdentifier = UUID().uuidString
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
let interface = Interface(context: viewContext)
|
|
|
|
|
|
|
|
tunnel.interface = interface
|
|
|
|
}
|
|
|
|
return tunnel
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-09-18 19:47:43 +00:00
|
|
|
@IBAction func showSettings(_ sender: Any) {
|
|
|
|
delegate?.showSettings()
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:47:52 +00:00
|
|
|
@IBAction func addPeer(_ sender: Any) {
|
|
|
|
if let moc = tunnel.managedObjectContext {
|
|
|
|
tableView.beginUpdates()
|
|
|
|
let insertedAt = IndexPath(row: tunnel.peers?.count ?? 0, section: 1)
|
|
|
|
tableView.insertRows(at: [insertedAt], with: .automatic)
|
2018-08-05 13:58:01 +00:00
|
|
|
|
|
|
|
let peer = Peer(context: moc)
|
|
|
|
tunnel.addToPeers(peer)
|
|
|
|
|
2018-05-29 20:47:52 +00:00
|
|
|
tableView.endUpdates()
|
|
|
|
tableView.scrollToRow(at: insertedAt, at: .middle, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 07:41:09 +00:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2018-05-24 20:07:04 +00:00
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
2018-05-26 07:41:09 +00:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
switch section {
|
|
|
|
case 1:
|
2018-05-27 20:38:44 +00:00
|
|
|
return tunnel?.peers?.count ?? 1
|
2018-05-26 07:41:09 +00:00
|
|
|
default:
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2018-05-26 07:41:09 +00:00
|
|
|
switch indexPath.section {
|
2018-05-24 20:07:04 +00:00
|
|
|
case 0:
|
2018-05-29 20:21:44 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(type: InterfaceTableViewCell.self, for: indexPath)
|
|
|
|
cell.model = tunnel.interface
|
2018-08-22 06:47:20 +00:00
|
|
|
cell.delegate = self
|
2018-05-29 20:21:44 +00:00
|
|
|
return cell
|
2018-05-24 20:07:04 +00:00
|
|
|
case 1:
|
2018-05-29 20:21:44 +00:00
|
|
|
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
|
|
|
|
}
|
2018-05-29 20:47:52 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2018-05-24 20:07:04 +00:00
|
|
|
default:
|
2018-05-29 20:47:52 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath)
|
|
|
|
cell.tunnel = tunnel
|
|
|
|
return cell
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
@IBAction func saveTunnelConfiguration(_ sender: Any) {
|
|
|
|
Promise<Void>(resolver: { (seal) in
|
2018-08-15 22:34:16 +00:00
|
|
|
do {
|
|
|
|
try tunnel.validate()
|
|
|
|
} catch {
|
|
|
|
seal.reject(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
viewContext.perform({
|
|
|
|
self.viewContext.saveContext({ (result) in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
seal.fulfill(())
|
|
|
|
case .failure(let error):
|
|
|
|
seal.reject(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}).then { () -> Promise<Void> in
|
|
|
|
self.delegate?.didSave(tunnel: self.tunnel, tunnelConfigurationTableViewController: self)
|
|
|
|
return Promise.value(())
|
|
|
|
}.catch { error in
|
2018-08-15 22:34:16 +00:00
|
|
|
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
2018-05-27 20:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 20:47:52 +00:00
|
|
|
extension TunnelConfigurationTableViewController: PeerTableViewCellDelegate {
|
|
|
|
func delete(peer: Peer) {
|
|
|
|
if let moc = tunnel.managedObjectContext {
|
|
|
|
tableView.beginUpdates()
|
|
|
|
let deletedAt = IndexPath(row: tunnel.peers?.index(of: peer) ?? 0, section: 1)
|
|
|
|
tableView.deleteRows(at: [deletedAt], with: .automatic)
|
|
|
|
tunnel.removeFromPeers(peer)
|
|
|
|
moc.delete(peer)
|
|
|
|
tableView.endUpdates()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:47:20 +00:00
|
|
|
extension TunnelConfigurationTableViewController: InterfaceTableViewCellDelegate {
|
|
|
|
func generateKeys() {
|
|
|
|
if let moc = tunnel.managedObjectContext {
|
|
|
|
moc.perform {
|
|
|
|
var privateKey = Data(count: 32)
|
|
|
|
privateKey.withUnsafeMutableBytes { (mutableBytes) -> Void in
|
|
|
|
curve25519_generate_private_key(mutableBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tunnel.interface?.privateKey = privateKey.base64EncodedString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol InterfaceTableViewCellDelegate: class {
|
|
|
|
func generateKeys()
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
class InterfaceTableViewCell: UITableViewCell {
|
2018-08-05 13:58:01 +00:00
|
|
|
var model: Interface! {
|
|
|
|
didSet {
|
|
|
|
nameField.text = model.tunnel?.title
|
2018-08-12 19:37:56 +00:00
|
|
|
addressesField.text = model.addresses
|
2018-08-05 13:58:01 +00:00
|
|
|
privateKeyField.text = model.privateKey
|
2018-09-16 20:38:32 +00:00
|
|
|
publicKeyField.text = model.publicKey
|
|
|
|
|
2018-09-26 09:43:13 +00:00
|
|
|
listenPortField.text = model.listenPort > 0 ? String(model.listenPort) : nil
|
2018-08-05 13:58:01 +00:00
|
|
|
dnsField.text = model.dns
|
2018-09-26 09:43:13 +00:00
|
|
|
mtuField.text = model.mtu > 0 ? String(model.mtu) : nil
|
2018-08-05 13:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
|
2018-08-22 06:47:20 +00:00
|
|
|
weak var delegate: InterfaceTableViewCellDelegate?
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
@IBOutlet weak var nameField: UITextField!
|
2018-08-12 19:37:56 +00:00
|
|
|
@IBOutlet weak var addressesField: UITextField!
|
2018-05-27 20:38:44 +00:00
|
|
|
@IBOutlet weak var privateKeyField: UITextField!
|
2018-10-02 17:39:03 +00:00
|
|
|
@IBOutlet weak var publicKeyField: CopyableLabel!
|
2018-05-27 20:38:44 +00:00
|
|
|
@IBOutlet weak var listenPortField: UITextField!
|
|
|
|
@IBOutlet weak var dnsField: UITextField!
|
|
|
|
@IBOutlet weak var mtuField: UITextField!
|
2018-08-22 06:47:20 +00:00
|
|
|
|
|
|
|
@IBAction func generateTapped(_ sender: Any) {
|
|
|
|
delegate?.generateKeys()
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
extension InterfaceTableViewCell: UITextFieldDelegate {
|
2018-08-22 06:47:20 +00:00
|
|
|
|
2018-05-29 20:21:44 +00:00
|
|
|
@IBAction
|
|
|
|
func textfieldDidChange(_ sender: UITextField) {
|
|
|
|
let string = sender.text
|
|
|
|
|
|
|
|
if sender == nameField {
|
|
|
|
model.tunnel?.title = string
|
|
|
|
} else if sender == privateKeyField {
|
|
|
|
model.privateKey = string
|
2018-09-16 20:38:32 +00:00
|
|
|
publicKeyField.text = model.publicKey
|
2018-08-12 19:37:56 +00:00
|
|
|
} else if sender == addressesField {
|
|
|
|
model.addresses = string
|
2018-05-29 20:21:44 +00:00
|
|
|
} else if sender == listenPortField {
|
|
|
|
if let string = string, let port = Int16(string) {
|
|
|
|
model.listenPort = port
|
2018-09-26 09:43:13 +00:00
|
|
|
} else {
|
|
|
|
model.listenPort = 0
|
2018-05-29 20:21:44 +00:00
|
|
|
}
|
|
|
|
} else if sender == dnsField {
|
|
|
|
model.dns = string
|
|
|
|
} else if sender == mtuField {
|
|
|
|
if let string = string, let mtu = Int32(string) {
|
|
|
|
model.mtu = mtu
|
2018-09-26 09:43:13 +00:00
|
|
|
} else {
|
|
|
|
model.mtu = 0
|
2018-05-29 20:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
}
|
2018-05-29 20:47:52 +00:00
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
|
2018-05-29 20:47:52 +00:00
|
|
|
protocol PeerTableViewCellDelegate: class {
|
|
|
|
func delete(peer: Peer)
|
2018-05-27 20:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
class PeerTableViewCell: UITableViewCell {
|
2018-08-05 13:58:01 +00:00
|
|
|
var peer: Peer! {
|
|
|
|
didSet {
|
|
|
|
publicKeyField.text = peer.publicKey
|
|
|
|
preSharedKeyField.text = peer.presharedKey
|
|
|
|
allowedIpsField.text = peer.allowedIPs
|
|
|
|
endpointField.text = peer.endpoint
|
2018-10-02 19:19:27 +00:00
|
|
|
persistentKeepaliveField.text = peer.persistentKeepalive > 0 ? String(peer.persistentKeepalive) : nil
|
2018-08-05 13:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 20:47:52 +00:00
|
|
|
weak var delegate: PeerTableViewCellDelegate?
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var publicKeyField: UITextField!
|
|
|
|
@IBOutlet weak var preSharedKeyField: UITextField!
|
|
|
|
@IBOutlet weak var allowedIpsField: UITextField!
|
|
|
|
@IBOutlet weak var endpointField: UITextField!
|
|
|
|
@IBOutlet weak var persistentKeepaliveField: UITextField!
|
2018-05-24 20:07:04 +00:00
|
|
|
|
2018-05-29 20:47:52 +00:00
|
|
|
@IBAction func deletePeer(_ sender: Any) {
|
|
|
|
delegate?.delete(peer: peer)
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
extension PeerTableViewCell: UITextFieldDelegate {
|
2018-05-29 20:21:44 +00:00
|
|
|
@IBAction
|
|
|
|
func textfieldDidChange(_ sender: UITextField) {
|
|
|
|
let string = sender.text
|
|
|
|
|
|
|
|
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 {
|
2018-08-15 22:34:16 +00:00
|
|
|
if let string = string, let persistentKeepalive = Int32(string) {
|
2018-05-29 20:21:44 +00:00
|
|
|
peer.persistentKeepalive = persistentKeepalive
|
2018-10-02 19:19:27 +00:00
|
|
|
} else {
|
|
|
|
peer.persistentKeepalive = 0
|
2018-05-29 20:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
class AddPeerTableViewCell: UITableViewCell {
|
2018-05-29 20:47:52 +00:00
|
|
|
var tunnel: Tunnel!
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
@IBAction func addPeer(_ sender: Any) {
|
2018-05-29 20:47:52 +00:00
|
|
|
if let moc = tunnel.managedObjectContext {
|
|
|
|
tunnel.addToPeers(Peer(context: moc))
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 07:17:36 +00:00
|
|
|
extension TunnelConfigurationTableViewController: Identifyable {}
|
2018-05-24 20:07:04 +00:00
|
|
|
extension InterfaceTableViewCell: Identifyable {}
|
|
|
|
extension PeerTableViewCell: Identifyable {}
|
|
|
|
extension AddPeerTableViewCell: Identifyable {}
|