2018-12-13 18:58:50 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-12-13 18:58:50 +00:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class TunnelDetailTableViewController: UITableViewController {
|
|
|
|
|
|
|
|
private enum Section {
|
|
|
|
case status
|
|
|
|
case interface
|
|
|
|
case peer(_ peer: TunnelViewModel.PeerData)
|
|
|
|
case onDemand
|
|
|
|
case delete
|
|
|
|
}
|
|
|
|
|
|
|
|
let interfaceFields: [TunnelViewModel.InterfaceField] = [
|
|
|
|
.name, .publicKey, .addresses,
|
|
|
|
.listenPort, .mtu, .dns
|
|
|
|
]
|
|
|
|
|
|
|
|
let peerFields: [TunnelViewModel.PeerField] = [
|
|
|
|
.publicKey, .preSharedKey, .endpoint,
|
|
|
|
.allowedIPs, .persistentKeepAlive
|
|
|
|
]
|
|
|
|
|
|
|
|
let tunnelsManager: TunnelsManager
|
|
|
|
let tunnel: TunnelContainer
|
|
|
|
var tunnelViewModel: TunnelViewModel
|
|
|
|
private var sections = [Section]()
|
2018-12-18 14:30:01 +00:00
|
|
|
private var onDemandStatusObservationToken: AnyObject?
|
|
|
|
private var statusObservationToken: AnyObject?
|
2018-12-13 18:58:50 +00:00
|
|
|
|
|
|
|
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) {
|
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
self.tunnel = tunnel
|
2018-12-21 04:52:45 +00:00
|
|
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration)
|
2018-12-13 18:58:50 +00:00
|
|
|
super.init(style: .grouped)
|
|
|
|
loadSections()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2018-12-14 23:12:59 +00:00
|
|
|
title = tunnelViewModel.interfaceData[.name]
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(editTapped))
|
2018-12-13 18:58:50 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
tableView.estimatedRowHeight = 44
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.allowsSelection = false
|
|
|
|
tableView.register(SwitchCell.self)
|
|
|
|
tableView.register(KeyValueCell.self)
|
|
|
|
tableView.register(ButtonCell.self)
|
2018-12-13 18:58:50 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
restorationIdentifier = "TunnelDetailVC:\(tunnel.name)"
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func loadSections() {
|
|
|
|
sections.removeAll()
|
|
|
|
sections.append(.status)
|
|
|
|
sections.append(.interface)
|
|
|
|
tunnelViewModel.peersData.forEach { sections.append(.peer($0)) }
|
|
|
|
sections.append(.onDemand)
|
|
|
|
sections.append(.delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func editTapped() {
|
|
|
|
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel)
|
|
|
|
editVC.delegate = self
|
|
|
|
let editNC = UINavigationController(rootViewController: editVC)
|
|
|
|
editNC.modalPresentationStyle = .formSheet
|
|
|
|
present(editNC, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) {
|
2018-12-13 18:58:50 +00:00
|
|
|
let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { _ in
|
|
|
|
onConfirmed()
|
|
|
|
}
|
2018-12-18 11:00:16 +00:00
|
|
|
let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel)
|
2018-12-13 18:58:50 +00:00
|
|
|
let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet)
|
|
|
|
alert.addAction(destroyAction)
|
|
|
|
alert.addAction(cancelAction)
|
|
|
|
|
|
|
|
alert.popoverPresentationController?.sourceView = sourceView
|
|
|
|
alert.popoverPresentationController?.sourceRect = sourceView.bounds
|
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
present(alert, animated: true, completion: nil)
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate {
|
|
|
|
func tunnelSaved(tunnel: TunnelContainer) {
|
2018-12-21 04:52:45 +00:00
|
|
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration)
|
2018-12-13 18:58:50 +00:00
|
|
|
loadSections()
|
2018-12-14 23:12:59 +00:00
|
|
|
title = tunnel.name
|
2018-12-18 13:57:27 +00:00
|
|
|
restorationIdentifier = "TunnelDetailVC:\(tunnel.name)"
|
2018-12-14 23:12:59 +00:00
|
|
|
tableView.reloadData()
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
func tunnelEditingCancelled() {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TunnelDetailTableViewController {
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
return sections.count
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
switch sections[section] {
|
|
|
|
case .status:
|
|
|
|
return 1
|
|
|
|
case .interface:
|
|
|
|
return tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFields).count
|
|
|
|
case .peer(let peerData):
|
|
|
|
return peerData.filterFieldsWithValueOrControl(peerFields: peerFields).count
|
|
|
|
case .onDemand:
|
|
|
|
return 1
|
|
|
|
case .delete:
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
switch sections[section] {
|
|
|
|
case .status:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitleStatus")
|
2018-12-13 18:58:50 +00:00
|
|
|
case .interface:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitleInterface")
|
2018-12-13 18:58:50 +00:00
|
|
|
case .peer:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitlePeer")
|
2018-12-13 18:58:50 +00:00
|
|
|
case .onDemand:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitleOnDemand")
|
2018-12-13 18:58:50 +00:00
|
|
|
case .delete:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
switch sections[indexPath.section] {
|
|
|
|
case .status:
|
|
|
|
return statusCell(for: tableView, at: indexPath)
|
|
|
|
case .interface:
|
|
|
|
return interfaceCell(for: tableView, at: indexPath)
|
|
|
|
case .peer(let peer):
|
|
|
|
return peerCell(for: tableView, at: indexPath, with: peer)
|
|
|
|
case .onDemand:
|
|
|
|
return onDemandCell(for: tableView, at: indexPath)
|
|
|
|
case .delete:
|
|
|
|
return deleteConfigurationCell(for: tableView, at: indexPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func statusCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
let statusUpdate: (SwitchCell, TunnelStatus) -> Void = { cell, status in
|
|
|
|
let text: String
|
|
|
|
switch status {
|
|
|
|
case .inactive:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusInactive")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .activating:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusActivating")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .active:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusActive")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .deactivating:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusDeactivating")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .reasserting:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusReasserting")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .restarting:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusRestarting")
|
2018-12-14 23:12:59 +00:00
|
|
|
case .waiting:
|
2018-12-18 11:00:16 +00:00
|
|
|
text = tr("tunnelStatusWaiting")
|
2018-12-14 23:12:59 +00:00
|
|
|
}
|
|
|
|
cell.textLabel?.text = text
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak cell] in
|
|
|
|
cell?.switchView.isOn = !(status == .deactivating || status == .inactive)
|
|
|
|
cell?.switchView.isUserInteractionEnabled = (status == .inactive || status == .active)
|
|
|
|
}
|
|
|
|
cell.isEnabled = status == .active || status == .inactive
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
statusUpdate(cell, tunnel.status)
|
2018-12-18 14:30:01 +00:00
|
|
|
statusObservationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
|
2018-12-14 23:12:59 +00:00
|
|
|
guard let cell = cell else { return }
|
|
|
|
statusUpdate(cell, tunnel.status)
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
cell.onSwitchToggled = { [weak self] isOn in
|
|
|
|
guard let self = self else { return }
|
|
|
|
if isOn {
|
|
|
|
self.tunnelsManager.startActivation(of: self.tunnel)
|
|
|
|
} else {
|
|
|
|
self.tunnelsManager.startDeactivation(of: self.tunnel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func interfaceCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let field = tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFields)[indexPath.row]
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = field.localizedUIString
|
2018-12-13 18:58:50 +00:00
|
|
|
cell.value = tunnelViewModel.interfaceData[field]
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func peerCell(for tableView: UITableView, at indexPath: IndexPath, with peerData: TunnelViewModel.PeerData) -> UITableViewCell {
|
|
|
|
let field = peerData.filterFieldsWithValueOrControl(peerFields: peerFields)[indexPath.row]
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = field.localizedUIString
|
2018-12-13 18:58:50 +00:00
|
|
|
cell.value = peerData[field]
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = tr("tunnelOnDemandKey")
|
2018-12-21 04:52:45 +00:00
|
|
|
cell.value = TunnelViewModel.activateOnDemandDetailText(for: tunnel.activateOnDemandSetting)
|
2018-12-18 14:30:01 +00:00
|
|
|
onDemandStatusObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak cell] tunnel, _ in
|
2018-12-21 04:52:45 +00:00
|
|
|
cell?.value = TunnelViewModel.activateOnDemandDetailText(for: tunnel.activateOnDemandSetting)
|
2018-12-14 23:12:59 +00:00
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func deleteConfigurationCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: ButtonCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.buttonText = tr("deleteTunnelButtonTitle")
|
2018-12-13 18:58:50 +00:00
|
|
|
cell.hasDestructiveAction = true
|
|
|
|
cell.onTapped = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
2018-12-18 11:00:16 +00:00
|
|
|
self.showConfirmationAlert(message: tr("deleteTunnelConfirmationAlertMessage"), buttonTitle: tr("deleteTunnelConfirmationAlertButtonTitle"), from: cell) { [weak self] in
|
2018-12-21 12:32:18 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
self.tunnelsManager.remove(tunnel: self.tunnel) { error in
|
2018-12-13 18:58:50 +00:00
|
|
|
if error != nil {
|
|
|
|
print("Error removing tunnel: \(String(describing: error))")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|