2018-05-24 20:07:04 +00:00
|
|
|
//
|
2018-05-26 07:17:36 +00:00
|
|
|
// TunnelConfigurationTableViewController.swift
|
2018-05-24 20:07:04 +00:00
|
|
|
// WireGuard
|
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 24-05-18.
|
2018-05-26 07:41:09 +00:00
|
|
|
// Copyright © 2018 WireGuard. 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-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!
|
|
|
|
|
|
|
|
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)
|
|
|
|
tunnel.addToPeers(Peer(context: viewContext))
|
|
|
|
|
|
|
|
let interface = Interface(context: viewContext)
|
|
|
|
interface.addToAdresses(Address(context: viewContext))
|
|
|
|
|
|
|
|
tunnel.interface = interface
|
|
|
|
}
|
|
|
|
return tunnel
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
|
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:
|
|
|
|
return tableView.dequeueReusableCell(type: InterfaceTableViewCell.self, for: indexPath)
|
|
|
|
case 1:
|
|
|
|
return tableView.dequeueReusableCell(type: PeerTableViewCell.self, for: indexPath)
|
|
|
|
default:
|
|
|
|
return tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath)
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 20:38:44 +00:00
|
|
|
|
|
|
|
@IBAction func saveTunnelConfiguration(_ sender: Any) {
|
|
|
|
Promise<Void>(resolver: { (seal) in
|
|
|
|
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
|
|
|
|
print("Error saving: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 20:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class InterfaceTableViewCell: UITableViewCell {
|
2018-05-27 20:38:44 +00:00
|
|
|
var model: Interface!
|
|
|
|
|
|
|
|
@IBOutlet weak var nameField: UITextField!
|
|
|
|
@IBOutlet weak var privateKeyField: UITextField!
|
|
|
|
@IBOutlet weak var publicKeyField: UITextField!
|
|
|
|
@IBOutlet weak var addressesField: UITextField!
|
|
|
|
@IBOutlet weak var listenPortField: UITextField!
|
|
|
|
@IBOutlet weak var dnsField: UITextField!
|
|
|
|
@IBOutlet weak var mtuField: UITextField!
|
2018-05-24 20:07:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-27 20:38:44 +00:00
|
|
|
extension InterfaceTableViewCell: UITextFieldDelegate {
|
|
|
|
func textFieldDidBeginEditing(_ textField: UITextField) {
|
|
|
|
print("\(textField) \(textField.text)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
|
|
|
print("\(string)")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
class PeerTableViewCell: UITableViewCell {
|
2018-05-27 20:38:44 +00:00
|
|
|
var peer: Peer!
|
|
|
|
|
|
|
|
@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-27 20:38:44 +00:00
|
|
|
extension PeerTableViewCell: UITextFieldDelegate {
|
|
|
|
func textFieldDidBeginEditing(_ textField: UITextField) {
|
|
|
|
print("\(textField) \(textField.text)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
|
|
|
print("\(string)")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:07:04 +00:00
|
|
|
class AddPeerTableViewCell: UITableViewCell {
|
2018-05-27 20:38:44 +00:00
|
|
|
var model: Interface?
|
|
|
|
|
|
|
|
@IBAction func addPeer(_ sender: Any) {
|
|
|
|
//TODO implement
|
|
|
|
print("Implement add peer")
|
|
|
|
}
|
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 {}
|