2018-12-13 18:58:50 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 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
|
2019-02-01 10:34:42 +00:00
|
|
|
case peer(index: Int, peer: TunnelViewModel.PeerData)
|
2018-12-13 18:58:50 +00:00
|
|
|
case onDemand
|
|
|
|
case delete
|
|
|
|
}
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
static let interfaceFields: [TunnelViewModel.InterfaceField] = [
|
2018-12-13 18:58:50 +00:00
|
|
|
.name, .publicKey, .addresses,
|
|
|
|
.listenPort, .mtu, .dns
|
|
|
|
]
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
static let peerFields: [TunnelViewModel.PeerField] = [
|
2018-12-13 18:58:50 +00:00
|
|
|
.publicKey, .preSharedKey, .endpoint,
|
2019-01-31 14:04:44 +00:00
|
|
|
.allowedIPs, .persistentKeepAlive,
|
|
|
|
.rxBytes, .txBytes, .lastHandshakeTime
|
2018-12-13 18:58:50 +00:00
|
|
|
]
|
|
|
|
|
2019-03-05 13:55:17 +00:00
|
|
|
static let onDemandFields: [ActivateOnDemandViewModel.OnDemandField] = [
|
|
|
|
.onDemand, .ssid
|
|
|
|
]
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let tunnelsManager: TunnelsManager
|
|
|
|
let tunnel: TunnelContainer
|
|
|
|
var tunnelViewModel: TunnelViewModel
|
2019-03-05 13:55:17 +00:00
|
|
|
var onDemandViewModel: ActivateOnDemandViewModel
|
2019-02-01 10:34:42 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
private var sections = [Section]()
|
2019-02-01 10:34:42 +00:00
|
|
|
private var interfaceFieldIsVisible = [Bool]()
|
|
|
|
private var peerFieldIsVisible = [[Bool]]()
|
|
|
|
|
2018-12-18 14:30:01 +00:00
|
|
|
private var statusObservationToken: AnyObject?
|
2019-03-05 13:55:17 +00:00
|
|
|
private var onDemandObservationToken: AnyObject?
|
2019-01-31 14:04:44 +00:00
|
|
|
private var reloadRuntimeConfigurationTimer: Timer?
|
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)
|
2019-03-11 12:39:48 +00:00
|
|
|
onDemandViewModel = ActivateOnDemandViewModel(tunnel: tunnel)
|
2018-12-13 18:58:50 +00:00
|
|
|
super.init(style: .grouped)
|
|
|
|
loadSections()
|
2019-02-01 10:34:42 +00:00
|
|
|
loadVisibleFields()
|
2019-01-31 13:09:24 +00:00
|
|
|
statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in
|
2019-01-31 14:04:44 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
if tunnel.status == .active {
|
|
|
|
self.startUpdatingRuntimeConfiguration()
|
|
|
|
} else if tunnel.status == .inactive {
|
|
|
|
self.reloadRuntimeConfiguration()
|
|
|
|
self.stopUpdatingRuntimeConfiguration()
|
2019-01-31 13:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 13:55:17 +00:00
|
|
|
onDemandObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak self] tunnel, _ in
|
|
|
|
// Handle On-Demand getting turned on/off outside of the app
|
2019-03-11 12:39:48 +00:00
|
|
|
self?.onDemandViewModel = ActivateOnDemandViewModel(tunnel: tunnel)
|
2019-03-05 13:55:17 +00:00
|
|
|
self?.updateActivateOnDemandFields()
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.register(SwitchCell.self)
|
|
|
|
tableView.register(KeyValueCell.self)
|
|
|
|
tableView.register(ButtonCell.self)
|
2019-03-09 07:23:14 +00:00
|
|
|
tableView.register(ChevronCell.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)
|
2019-02-01 10:34:42 +00:00
|
|
|
for (index, peer) in tunnelViewModel.peersData.enumerated() {
|
|
|
|
sections.append(.peer(index: index, peer: peer))
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
sections.append(.onDemand)
|
|
|
|
sections.append(.delete)
|
|
|
|
}
|
|
|
|
|
2019-02-01 10:34:42 +00:00
|
|
|
private func loadVisibleFields() {
|
2019-02-01 11:36:42 +00:00
|
|
|
let visibleInterfaceFields = tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: TunnelDetailTableViewController.interfaceFields)
|
|
|
|
interfaceFieldIsVisible = TunnelDetailTableViewController.interfaceFields.map { visibleInterfaceFields.contains($0) }
|
2019-02-01 10:34:42 +00:00
|
|
|
peerFieldIsVisible = tunnelViewModel.peersData.map { peer in
|
2019-02-01 11:36:42 +00:00
|
|
|
let visiblePeerFields = peer.filterFieldsWithValueOrControl(peerFields: TunnelDetailTableViewController.peerFields)
|
|
|
|
return TunnelDetailTableViewController.peerFields.map { visiblePeerFields.contains($0) }
|
2019-02-01 10:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:04:44 +00:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
if tunnel.status == .active {
|
|
|
|
self.startUpdatingRuntimeConfiguration()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
stopUpdatingRuntimeConfiguration()
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
@objc func editTapped() {
|
2019-02-06 02:23:51 +00:00
|
|
|
PrivateDataConfirmation.confirmAccess(to: tr("iosViewPrivateData")) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let editVC = TunnelEditTableViewController(tunnelsManager: self.tunnelsManager, tunnel: self.tunnel)
|
|
|
|
editVC.delegate = self
|
|
|
|
let editNC = UINavigationController(rootViewController: editVC)
|
2019-10-15 12:51:04 +00:00
|
|
|
editNC.modalPresentationStyle = .fullScreen
|
2019-02-06 02:23:51 +00:00
|
|
|
self.present(editNC, animated: true)
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 14:04:44 +00:00
|
|
|
func startUpdatingRuntimeConfiguration() {
|
|
|
|
reloadRuntimeConfiguration()
|
|
|
|
reloadRuntimeConfigurationTimer?.invalidate()
|
|
|
|
let reloadTimer = Timer(timeInterval: 1 /* second */, repeats: true) { [weak self] _ in
|
|
|
|
self?.reloadRuntimeConfiguration()
|
|
|
|
}
|
|
|
|
reloadRuntimeConfigurationTimer = reloadTimer
|
|
|
|
RunLoop.main.add(reloadTimer, forMode: .common)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopUpdatingRuntimeConfiguration() {
|
|
|
|
reloadRuntimeConfigurationTimer?.invalidate()
|
|
|
|
reloadRuntimeConfigurationTimer = nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 11:36:42 +00:00
|
|
|
func applyTunnelConfiguration(tunnelConfiguration: TunnelConfiguration) {
|
|
|
|
// Incorporates changes from tunnelConfiguation. Ignores any changes in peer ordering.
|
|
|
|
guard let tableView = self.tableView else { return }
|
|
|
|
let sections = self.sections
|
2019-02-08 16:05:58 +00:00
|
|
|
let interfaceSectionIndex = sections.firstIndex {
|
|
|
|
if case .interface = $0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}!
|
2019-02-01 11:36:42 +00:00
|
|
|
let firstPeerSectionIndex = interfaceSectionIndex + 1
|
2020-12-02 14:08:45 +00:00
|
|
|
let interfaceFieldIsVisible = self.interfaceFieldIsVisible
|
|
|
|
let peerFieldIsVisible = self.peerFieldIsVisible
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
func handleSectionFieldsModified<T>(fields: [T], fieldIsVisible: [Bool], section: Int, changes: [T: TunnelViewModel.Changes.FieldChange]) {
|
|
|
|
for (index, field) in fields.enumerated() {
|
|
|
|
guard let change = changes[field] else { continue }
|
|
|
|
if case .modified(let newValue) = change {
|
|
|
|
let row = fieldIsVisible[0 ..< index].filter { $0 }.count
|
|
|
|
let indexPath = IndexPath(row: row, section: section)
|
|
|
|
if let cell = tableView.cellForRow(at: indexPath) as? KeyValueCell {
|
|
|
|
cell.value = newValue
|
|
|
|
}
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
2019-02-09 21:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSectionRowsInsertedOrRemoved<T>(fields: [T], fieldIsVisible fieldIsVisibleInput: [Bool], section: Int, changes: [T: TunnelViewModel.Changes.FieldChange]) {
|
|
|
|
var fieldIsVisible = fieldIsVisibleInput
|
2019-02-01 11:36:42 +00:00
|
|
|
|
|
|
|
var removedIndexPaths = [IndexPath]()
|
|
|
|
for (index, field) in fields.enumerated().reversed() where changes[field] == .removed {
|
|
|
|
let row = fieldIsVisible[0 ..< index].filter { $0 }.count
|
|
|
|
removedIndexPaths.append(IndexPath(row: row, section: section))
|
|
|
|
fieldIsVisible[index] = false
|
|
|
|
}
|
|
|
|
if !removedIndexPaths.isEmpty {
|
|
|
|
tableView.deleteRows(at: removedIndexPaths, with: .automatic)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addedIndexPaths = [IndexPath]()
|
|
|
|
for (index, field) in fields.enumerated() where changes[field] == .added {
|
|
|
|
let row = fieldIsVisible[0 ..< index].filter { $0 }.count
|
|
|
|
addedIndexPaths.append(IndexPath(row: row, section: section))
|
|
|
|
fieldIsVisible[index] = true
|
|
|
|
}
|
|
|
|
if !addedIndexPaths.isEmpty {
|
|
|
|
tableView.insertRows(at: addedIndexPaths, with: .automatic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
let changes = self.tunnelViewModel.applyConfiguration(other: tunnelConfiguration)
|
|
|
|
|
|
|
|
if !changes.interfaceChanges.isEmpty {
|
|
|
|
handleSectionFieldsModified(fields: TunnelDetailTableViewController.interfaceFields, fieldIsVisible: interfaceFieldIsVisible,
|
|
|
|
section: interfaceSectionIndex, changes: changes.interfaceChanges)
|
|
|
|
}
|
|
|
|
for (peerIndex, peerChanges) in changes.peerChanges {
|
|
|
|
handleSectionFieldsModified(fields: TunnelDetailTableViewController.peerFields, fieldIsVisible: peerFieldIsVisible[peerIndex], section: firstPeerSectionIndex + peerIndex, changes: peerChanges)
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
|
2019-02-09 21:08:23 +00:00
|
|
|
let isAnyInterfaceFieldAddedOrRemoved = changes.interfaceChanges.contains { $0.value == .added || $0.value == .removed }
|
|
|
|
let isAnyPeerFieldAddedOrRemoved = changes.peerChanges.contains { $0.changes.contains { $0.value == .added || $0.value == .removed } }
|
|
|
|
let peersRemovedSectionIndices = changes.peersRemovedIndices.map { firstPeerSectionIndex + $0 }
|
|
|
|
let peersInsertedSectionIndices = changes.peersInsertedIndices.map { firstPeerSectionIndex + $0 }
|
|
|
|
|
|
|
|
if isAnyInterfaceFieldAddedOrRemoved || isAnyPeerFieldAddedOrRemoved || !peersRemovedSectionIndices.isEmpty || !peersInsertedSectionIndices.isEmpty {
|
|
|
|
tableView.beginUpdates()
|
|
|
|
if isAnyInterfaceFieldAddedOrRemoved {
|
|
|
|
handleSectionRowsInsertedOrRemoved(fields: TunnelDetailTableViewController.interfaceFields, fieldIsVisible: interfaceFieldIsVisible, section: interfaceSectionIndex, changes: changes.interfaceChanges)
|
|
|
|
}
|
|
|
|
if isAnyPeerFieldAddedOrRemoved {
|
|
|
|
for (peerIndex, peerChanges) in changes.peerChanges {
|
|
|
|
handleSectionRowsInsertedOrRemoved(fields: TunnelDetailTableViewController.peerFields, fieldIsVisible: peerFieldIsVisible[peerIndex], section: firstPeerSectionIndex + peerIndex, changes: peerChanges)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !peersRemovedSectionIndices.isEmpty {
|
|
|
|
tableView.deleteSections(IndexSet(peersRemovedSectionIndices), with: .automatic)
|
|
|
|
}
|
|
|
|
if !peersInsertedSectionIndices.isEmpty {
|
|
|
|
tableView.insertSections(IndexSet(peersInsertedSectionIndices), with: .automatic)
|
|
|
|
}
|
|
|
|
self.loadSections()
|
|
|
|
self.loadVisibleFields()
|
|
|
|
tableView.endUpdates()
|
|
|
|
} else {
|
|
|
|
self.loadSections()
|
|
|
|
self.loadVisibleFields()
|
|
|
|
}
|
2019-02-01 11:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 14:04:44 +00:00
|
|
|
private func reloadRuntimeConfiguration() {
|
2019-02-01 11:36:42 +00:00
|
|
|
tunnel.getRuntimeTunnelConfiguration { [weak self] tunnelConfiguration in
|
|
|
|
guard let tunnelConfiguration = tunnelConfiguration else { return }
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.applyTunnelConfiguration(tunnelConfiguration: tunnelConfiguration)
|
|
|
|
}
|
2019-01-31 14:04:44 +00:00
|
|
|
}
|
2019-03-05 13:55:17 +00:00
|
|
|
|
|
|
|
private func updateActivateOnDemandFields() {
|
|
|
|
guard let onDemandSection = sections.firstIndex(where: { if case .onDemand = $0 { return true } else { return false } }) else { return }
|
|
|
|
let numberOfTableViewOnDemandRows = tableView.numberOfRows(inSection: onDemandSection)
|
|
|
|
let ssidRowIndexPath = IndexPath(row: 1, section: onDemandSection)
|
|
|
|
switch (numberOfTableViewOnDemandRows, onDemandViewModel.isWiFiInterfaceEnabled) {
|
|
|
|
case (1, true):
|
|
|
|
tableView.insertRows(at: [ssidRowIndexPath], with: .automatic)
|
|
|
|
case (2, false):
|
|
|
|
tableView.deleteRows(at: [ssidRowIndexPath], with: .automatic)
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tableView.reloadSections(IndexSet(integer: onDemandSection), with: .automatic)
|
|
|
|
}
|
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)
|
2019-03-11 12:39:48 +00:00
|
|
|
onDemandViewModel = ActivateOnDemandViewModel(tunnel: tunnel)
|
2018-12-13 18:58:50 +00:00
|
|
|
loadSections()
|
2019-02-05 10:24:23 +00:00
|
|
|
loadVisibleFields()
|
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:
|
2019-02-01 10:34:42 +00:00
|
|
|
return interfaceFieldIsVisible.filter { $0 }.count
|
|
|
|
case .peer(let peerIndex, _):
|
|
|
|
return peerFieldIsVisible[peerIndex].filter { $0 }.count
|
2018-12-13 18:58:50 +00:00
|
|
|
case .onDemand:
|
2019-03-05 13:55:17 +00:00
|
|
|
return onDemandViewModel.isWiFiInterfaceEnabled ? 2 : 1
|
2018-12-13 18:58:50 +00:00
|
|
|
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)
|
2019-02-01 10:34:42 +00:00
|
|
|
case .peer(let index, let peer):
|
|
|
|
return peerCell(for: tableView, at: indexPath, with: peer, peerIndex: index)
|
2018-12-13 18:58:50 +00:00
|
|
|
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)
|
2019-02-03 07:07:57 +00:00
|
|
|
|
2021-07-26 11:26:03 +00:00
|
|
|
func update(cell: SwitchCell?, with tunnel: TunnelContainer) {
|
|
|
|
guard let cell = cell else { return }
|
|
|
|
|
|
|
|
let status = tunnel.status
|
|
|
|
let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled
|
|
|
|
|
|
|
|
let isSwitchOn = (status == .activating || status == .active || isOnDemandEngaged)
|
|
|
|
cell.switchView.setOn(isSwitchOn, animated: true)
|
|
|
|
|
|
|
|
if isOnDemandEngaged && !(status == .activating || status == .active) {
|
|
|
|
cell.switchView.onTintColor = UIColor.systemYellow
|
|
|
|
} else {
|
|
|
|
cell.switchView.onTintColor = UIColor.systemGreen
|
|
|
|
}
|
|
|
|
|
|
|
|
var text: String
|
2019-02-03 07:07:57 +00:00
|
|
|
switch status {
|
|
|
|
case .inactive:
|
|
|
|
text = tr("tunnelStatusInactive")
|
|
|
|
case .activating:
|
|
|
|
text = tr("tunnelStatusActivating")
|
|
|
|
case .active:
|
|
|
|
text = tr("tunnelStatusActive")
|
|
|
|
case .deactivating:
|
|
|
|
text = tr("tunnelStatusDeactivating")
|
|
|
|
case .reasserting:
|
|
|
|
text = tr("tunnelStatusReasserting")
|
|
|
|
case .restarting:
|
|
|
|
text = tr("tunnelStatusRestarting")
|
|
|
|
case .waiting:
|
|
|
|
text = tr("tunnelStatusWaiting")
|
|
|
|
}
|
2021-07-26 11:26:03 +00:00
|
|
|
|
|
|
|
if tunnel.hasOnDemandRules {
|
|
|
|
text += isOnDemandEngaged ? tr("tunnelStatusAddendumOnDemand") : ""
|
|
|
|
cell.switchView.isUserInteractionEnabled = true
|
|
|
|
cell.isEnabled = true
|
|
|
|
} else {
|
|
|
|
cell.switchView.isUserInteractionEnabled = (status == .inactive || status == .active)
|
|
|
|
cell.isEnabled = (status == .inactive || status == .active)
|
|
|
|
}
|
2019-02-03 07:07:57 +00:00
|
|
|
cell.textLabel?.text = text
|
|
|
|
}
|
|
|
|
|
2021-07-26 11:26:03 +00:00
|
|
|
update(cell: cell, with: tunnel)
|
|
|
|
cell.statusObservationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
|
|
|
|
update(cell: cell, with: tunnel)
|
|
|
|
}
|
|
|
|
cell.isOnDemandEnabledObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak cell] tunnel, _ in
|
|
|
|
update(cell: cell, with: tunnel)
|
|
|
|
}
|
|
|
|
cell.hasOnDemandRulesObservationToken = tunnel.observe(\.hasOnDemandRules) { [weak cell] tunnel, _ in
|
|
|
|
update(cell: cell, with: tunnel)
|
2019-02-03 07:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
cell.onSwitchToggled = { [weak self] isOn in
|
|
|
|
guard let self = self else { return }
|
2021-07-26 11:26:03 +00:00
|
|
|
|
|
|
|
if self.tunnel.hasOnDemandRules {
|
|
|
|
self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in
|
|
|
|
if error == nil && !isOn {
|
|
|
|
self.tunnelsManager.startDeactivation(of: self.tunnel)
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
} else {
|
2021-07-26 11:26:03 +00:00
|
|
|
if isOn {
|
|
|
|
self.tunnelsManager.startActivation(of: self.tunnel)
|
|
|
|
} else {
|
|
|
|
self.tunnelsManager.startDeactivation(of: self.tunnel)
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func interfaceCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2019-02-01 11:36:42 +00:00
|
|
|
let visibleInterfaceFields = TunnelDetailTableViewController.interfaceFields.enumerated().filter { interfaceFieldIsVisible[$0.offset] }.map { $0.element }
|
2019-02-01 10:34:42 +00:00
|
|
|
let field = visibleInterfaceFields[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
|
|
|
|
}
|
|
|
|
|
2019-02-01 10:34:42 +00:00
|
|
|
private func peerCell(for tableView: UITableView, at indexPath: IndexPath, with peerData: TunnelViewModel.PeerData, peerIndex: Int) -> UITableViewCell {
|
2019-02-01 11:36:42 +00:00
|
|
|
let visiblePeerFields = TunnelDetailTableViewController.peerFields.enumerated().filter { peerFieldIsVisible[peerIndex][$0.offset] }.map { $0.element }
|
2019-02-01 10:34:42 +00:00
|
|
|
let field = visiblePeerFields[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
|
2019-01-31 12:47:46 +00:00
|
|
|
if field == .persistentKeepAlive {
|
|
|
|
cell.value = tr(format: "tunnelPeerPersistentKeepaliveValue (%@)", peerData[field])
|
2019-02-12 13:55:42 +00:00
|
|
|
} else if field == .preSharedKey {
|
|
|
|
cell.value = tr("tunnelPeerPresharedKeyEnabled")
|
2019-01-31 12:47:46 +00:00
|
|
|
} else {
|
|
|
|
cell.value = peerData[field]
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2019-03-05 13:55:17 +00:00
|
|
|
let field = TunnelDetailTableViewController.onDemandFields[indexPath.row]
|
|
|
|
if field == .onDemand {
|
2019-03-09 07:23:14 +00:00
|
|
|
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
|
|
|
cell.key = field.localizedUIString
|
2019-03-05 13:55:17 +00:00
|
|
|
cell.value = onDemandViewModel.localizedInterfaceDescription
|
2021-01-06 10:57:40 +00:00
|
|
|
cell.copyableGesture = false
|
2019-03-09 07:23:14 +00:00
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
assert(field == .ssid)
|
|
|
|
if onDemandViewModel.ssidOption == .anySSID {
|
|
|
|
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
|
|
|
cell.key = field.localizedUIString
|
|
|
|
cell.value = onDemandViewModel.ssidOption.localizedUIString
|
2021-01-06 10:57:40 +00:00
|
|
|
cell.copyableGesture = false
|
2019-03-09 07:23:14 +00:00
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
let cell: ChevronCell = tableView.dequeueReusableCell(for: indexPath)
|
|
|
|
cell.message = field.localizedUIString
|
2019-03-09 10:05:49 +00:00
|
|
|
cell.detailMessage = onDemandViewModel.localizedSSIDDescription
|
2019-03-09 07:23:14 +00:00
|
|
|
return cell
|
|
|
|
}
|
2018-12-14 23:12:59 +00:00
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
2019-03-18 19:54:06 +00:00
|
|
|
ConfirmationAlertPresenter.showConfirmationAlert(message: tr("deleteTunnelConfirmationAlertMessage"),
|
|
|
|
buttonTitle: tr("deleteTunnelConfirmationAlertButtonTitle"),
|
|
|
|
from: cell, presentingVC: self) { [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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-09 07:23:14 +00:00
|
|
|
|
|
|
|
extension TunnelDetailTableViewController {
|
|
|
|
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
|
|
|
if case .onDemand = sections[indexPath.section],
|
|
|
|
case .ssid = TunnelDetailTableViewController.onDemandFields[indexPath.row] {
|
|
|
|
return indexPath
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
if case .onDemand = sections[indexPath.section],
|
|
|
|
case .ssid = TunnelDetailTableViewController.onDemandFields[indexPath.row] {
|
|
|
|
let ssidDetailVC = SSIDOptionDetailTableViewController(title: onDemandViewModel.ssidOption.localizedUIString, ssids: onDemandViewModel.selectedSSIDs)
|
|
|
|
navigationController?.pushViewController(ssidDetailVC, animated: true)
|
|
|
|
}
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
}
|
|
|
|
}
|