2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-10-20 13:45:53 +00:00
|
|
|
|
|
|
|
import UIKit
|
2020-11-26 16:23:50 +00:00
|
|
|
import WireGuardKit
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-10-24 11:39:34 +00:00
|
|
|
protocol TunnelEditTableViewControllerDelegate: class {
|
2018-10-25 05:44:38 +00:00
|
|
|
func tunnelSaved(tunnel: TunnelContainer)
|
|
|
|
func tunnelEditingCancelled()
|
2018-10-24 11:39:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 13:45:53 +00:00
|
|
|
class TunnelEditTableViewController: UITableViewController {
|
2018-12-12 21:33:14 +00:00
|
|
|
private enum Section {
|
|
|
|
case interface
|
|
|
|
case peer(_ peer: TunnelViewModel.PeerData)
|
|
|
|
case addPeer
|
|
|
|
case onDemand
|
2018-12-22 04:30:35 +00:00
|
|
|
|
2018-12-22 01:52:51 +00:00
|
|
|
static func == (lhs: Section, rhs: Section) -> Bool {
|
|
|
|
switch (lhs, rhs) {
|
|
|
|
case (.interface, .interface),
|
|
|
|
(.addPeer, .addPeer),
|
|
|
|
(.onDemand, .onDemand):
|
|
|
|
return true
|
|
|
|
case let (.peer(peerA), .peer(peerB)):
|
|
|
|
return peerA.index == peerB.index
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
weak var delegate: TunnelEditTableViewControllerDelegate?
|
2018-10-24 11:39:34 +00:00
|
|
|
|
2018-10-23 12:32:10 +00:00
|
|
|
let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [
|
2018-10-20 13:45:53 +00:00
|
|
|
[.name],
|
|
|
|
[.privateKey, .publicKey, .generateKeyPair],
|
|
|
|
[.addresses, .listenPort, .mtu, .dns]
|
|
|
|
]
|
|
|
|
|
2018-10-29 07:16:54 +00:00
|
|
|
let peerFields: [TunnelViewModel.PeerField] = [
|
|
|
|
.publicKey, .preSharedKey, .endpoint,
|
2018-10-29 11:08:32 +00:00
|
|
|
.allowedIPs, .excludePrivateIPs, .persistentKeepAlive,
|
2018-10-29 07:16:54 +00:00
|
|
|
.deletePeer
|
2018-10-20 13:45:53 +00:00
|
|
|
]
|
|
|
|
|
2019-02-27 08:00:57 +00:00
|
|
|
let onDemandFields: [ActivateOnDemandViewModel.OnDemandField] = [
|
|
|
|
.nonWiFiInterface,
|
|
|
|
.wiFiInterface,
|
2019-03-05 13:55:17 +00:00
|
|
|
.ssid
|
2018-11-10 13:20:09 +00:00
|
|
|
]
|
|
|
|
|
2018-10-23 11:53:46 +00:00
|
|
|
let tunnelsManager: TunnelsManager
|
2018-10-24 11:39:34 +00:00
|
|
|
let tunnel: TunnelContainer?
|
2018-10-23 09:53:18 +00:00
|
|
|
let tunnelViewModel: TunnelViewModel
|
2019-02-27 08:00:57 +00:00
|
|
|
var onDemandViewModel: ActivateOnDemandViewModel
|
2018-12-12 21:33:14 +00:00
|
|
|
private var sections = [Section]()
|
2018-12-12 17:40:57 +00:00
|
|
|
|
2018-12-22 01:52:51 +00:00
|
|
|
// Use this initializer to edit an existing tunnel.
|
2018-12-12 17:40:57 +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-10-25 02:30:12 +00:00
|
|
|
super.init(style: .grouped)
|
2018-12-12 21:33:14 +00:00
|
|
|
loadSections()
|
2018-10-25 02:30:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 01:52:51 +00:00
|
|
|
// Use this initializer to create a new tunnel.
|
2018-12-19 11:00:34 +00:00
|
|
|
init(tunnelsManager: TunnelsManager) {
|
2018-12-12 17:40:57 +00:00
|
|
|
self.tunnelsManager = tunnelsManager
|
2018-10-25 02:30:12 +00:00
|
|
|
tunnel = nil
|
2018-12-19 11:00:34 +00:00
|
|
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil)
|
2019-02-27 08:00:57 +00:00
|
|
|
onDemandViewModel = ActivateOnDemandViewModel()
|
2018-10-20 13:45:53 +00:00
|
|
|
super.init(style: .grouped)
|
2018-12-12 21:33:14 +00:00
|
|
|
loadSections()
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2018-12-18 11:00:16 +00:00
|
|
|
title = tunnel == nil ? tr("newTunnelViewTitle") : tr("editTunnelViewTitle")
|
2018-12-14 23:12:59 +00:00
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveTapped))
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelTapped))
|
|
|
|
|
|
|
|
tableView.estimatedRowHeight = 44
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
|
2018-12-15 02:02:37 +00:00
|
|
|
tableView.register(TunnelEditKeyValueCell.self)
|
|
|
|
tableView.register(TunnelEditEditableKeyValueCell.self)
|
2018-12-14 23:12:59 +00:00
|
|
|
tableView.register(ButtonCell.self)
|
|
|
|
tableView.register(SwitchCell.self)
|
2019-02-27 08:00:57 +00:00
|
|
|
tableView.register(ChevronCell.self)
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func loadSections() {
|
|
|
|
sections.removeAll()
|
|
|
|
interfaceFieldsBySection.forEach { _ in sections.append(.interface) }
|
|
|
|
tunnelViewModel.peersData.forEach { sections.append(.peer($0)) }
|
|
|
|
sections.append(.addPeer)
|
|
|
|
sections.append(.onDemand)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func saveTapped() {
|
2018-12-14 23:12:59 +00:00
|
|
|
tableView.endEditing(false)
|
2018-10-23 11:53:46 +00:00
|
|
|
let tunnelSaveResult = tunnelViewModel.save()
|
2018-12-12 18:28:27 +00:00
|
|
|
switch tunnelSaveResult {
|
2018-10-23 11:53:46 +00:00
|
|
|
case .error(let errorMessage):
|
2018-12-21 23:28:18 +00:00
|
|
|
let alertTitle = (tunnelViewModel.interfaceData.validatedConfiguration == nil || tunnelViewModel.interfaceData.validatedName == nil) ?
|
2018-12-18 11:00:16 +00:00
|
|
|
tr("alertInvalidInterfaceTitle") : tr("alertInvalidPeerTitle")
|
|
|
|
ErrorPresenter.showErrorAlert(title: alertTitle, message: errorMessage, from: self)
|
2018-12-14 23:12:59 +00:00
|
|
|
tableView.reloadData() // Highlight erroring fields
|
2018-10-23 11:53:46 +00:00
|
|
|
case .saved(let tunnelConfiguration):
|
2019-03-08 10:42:54 +00:00
|
|
|
let onDemandOption = onDemandViewModel.toOnDemandOption()
|
2018-10-24 11:49:14 +00:00
|
|
|
if let tunnel = tunnel {
|
|
|
|
// We're modifying an existing tunnel
|
2019-03-08 10:42:54 +00:00
|
|
|
tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] error in
|
2018-10-24 11:49:14 +00:00
|
|
|
if let error = error {
|
2018-10-31 19:15:09 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
2018-10-24 11:49:14 +00:00
|
|
|
} else {
|
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-10-25 05:44:38 +00:00
|
|
|
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
2018-10-24 11:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We're adding a new tunnel
|
2019-03-08 10:42:54 +00:00
|
|
|
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in
|
2019-04-08 07:52:06 +00:00
|
|
|
switch result {
|
|
|
|
case .failure(let error):
|
2018-10-31 19:15:09 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
2019-04-08 07:52:06 +00:00
|
|
|
case .success(let tunnel):
|
2018-10-24 11:49:14 +00:00
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-12-06 10:28:27 +00:00
|
|
|
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
2018-10-24 11:49:14 +00:00
|
|
|
}
|
2018-10-23 11:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func cancelTapped() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
2018-12-14 23:12:59 +00:00
|
|
|
delegate?.tunnelEditingCancelled()
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UITableViewDataSource
|
|
|
|
|
|
|
|
extension TunnelEditTableViewController {
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2018-12-12 21:33:14 +00:00
|
|
|
return sections.count
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2018-12-12 21:33:14 +00:00
|
|
|
switch sections[section] {
|
|
|
|
case .interface:
|
2018-10-23 12:32:10 +00:00
|
|
|
return interfaceFieldsBySection[section].count
|
2018-12-12 21:33:14 +00:00
|
|
|
case .peer(let peerData):
|
2018-11-02 07:42:10 +00:00
|
|
|
let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs }
|
|
|
|
return peerFieldsToShow.count
|
2018-12-12 21:33:14 +00:00
|
|
|
case .addPeer:
|
2018-10-20 13:45:53 +00:00
|
|
|
return 1
|
2018-12-12 21:33:14 +00:00
|
|
|
case .onDemand:
|
2019-02-27 08:00:57 +00:00
|
|
|
if onDemandViewModel.isWiFiInterfaceEnabled {
|
|
|
|
return 3
|
2018-11-12 10:23:18 +00:00
|
|
|
} else {
|
2019-02-27 08:00:57 +00:00
|
|
|
return 2
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2018-12-12 21:33:14 +00:00
|
|
|
switch sections[section] {
|
|
|
|
case .interface:
|
2018-12-18 11:00:16 +00:00
|
|
|
return section == 0 ? tr("tunnelSectionTitleInterface") : nil
|
2018-12-12 21:33:14 +00:00
|
|
|
case .peer:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitlePeer")
|
2018-12-12 21:33:14 +00:00
|
|
|
case .addPeer:
|
2018-10-20 13:45:53 +00:00
|
|
|
return nil
|
2018-12-12 21:33:14 +00:00
|
|
|
case .onDemand:
|
2018-12-18 11:00:16 +00:00
|
|
|
return tr("tunnelSectionTitleOnDemand")
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2018-12-12 21:33:14 +00:00
|
|
|
switch sections[indexPath.section] {
|
|
|
|
case .interface:
|
2018-12-12 17:40:57 +00:00
|
|
|
return interfaceFieldCell(for: tableView, at: indexPath)
|
2018-12-12 21:33:14 +00:00
|
|
|
case .peer(let peerData):
|
|
|
|
return peerCell(for: tableView, at: indexPath, with: peerData)
|
|
|
|
case .addPeer:
|
2018-12-12 17:40:57 +00:00
|
|
|
return addPeerCell(for: tableView, at: indexPath)
|
2018-12-12 21:33:14 +00:00
|
|
|
case .onDemand:
|
2018-12-12 17:40:57 +00:00
|
|
|
return onDemandCell(for: tableView, at: indexPath)
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
private func interfaceFieldCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let field = interfaceFieldsBySection[indexPath.section][indexPath.row]
|
2018-12-12 21:33:14 +00:00
|
|
|
switch field {
|
|
|
|
case .generateKeyPair:
|
|
|
|
return generateKeyPairCell(for: tableView, at: indexPath, with: field)
|
|
|
|
case .publicKey:
|
|
|
|
return publicKeyCell(for: tableView, at: indexPath, with: field)
|
|
|
|
default:
|
|
|
|
return interfaceFieldKeyValueCell(for: tableView, at: indexPath, with: field)
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
private func generateKeyPairCell(for tableView: UITableView, at indexPath: IndexPath, with field: TunnelViewModel.InterfaceField) -> 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 = field.localizedUIString
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.onTapped = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2020-11-26 16:23:50 +00:00
|
|
|
self.tunnelViewModel.interfaceData[.privateKey] = PrivateKey().base64Key
|
2018-12-12 21:33:14 +00:00
|
|
|
if let privateKeyRow = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .privateKey),
|
|
|
|
let publicKeyRow = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .publicKey) {
|
|
|
|
let privateKeyIndex = IndexPath(row: privateKeyRow, section: indexPath.section)
|
|
|
|
let publicKeyIndex = IndexPath(row: publicKeyRow, section: indexPath.section)
|
|
|
|
self.tableView.reloadRows(at: [privateKeyIndex, publicKeyIndex], with: .fade)
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func publicKeyCell(for tableView: UITableView, at indexPath: IndexPath, with field: TunnelViewModel.InterfaceField) -> UITableViewCell {
|
2018-12-15 02:02:37 +00:00
|
|
|
let cell: TunnelEditKeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = field.localizedUIString
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.value = tunnelViewModel.interfaceData[field]
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func interfaceFieldKeyValueCell(for tableView: UITableView, at indexPath: IndexPath, with field: TunnelViewModel.InterfaceField) -> UITableViewCell {
|
2018-12-15 02:02:37 +00:00
|
|
|
let cell: TunnelEditEditableKeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = field.localizedUIString
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
switch field {
|
|
|
|
case .name, .privateKey:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextRequired")
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.keyboardType = .default
|
2018-12-22 00:55:42 +00:00
|
|
|
case .addresses:
|
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextStronglyRecommended")
|
|
|
|
cell.keyboardType = .numbersAndPunctuation
|
|
|
|
case .dns:
|
2019-01-21 20:14:07 +00:00
|
|
|
cell.placeholderText = tunnelViewModel.peersData.contains(where: { $0.shouldStronglyRecommendDNS }) ? tr("tunnelEditPlaceholderTextStronglyRecommended") : tr("tunnelEditPlaceholderTextOptional")
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.keyboardType = .numbersAndPunctuation
|
|
|
|
case .listenPort, .mtu:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextAutomatic")
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.keyboardType = .numberPad
|
|
|
|
case .publicKey, .generateKeyPair:
|
|
|
|
cell.keyboardType = .default
|
2019-03-18 08:32:54 +00:00
|
|
|
case .status, .toggleStatus:
|
|
|
|
fatalError("Unexpected interface field")
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cell.isValueValid = (!tunnelViewModel.interfaceData.fieldsWithError.contains(field))
|
|
|
|
// Bind values to view model
|
|
|
|
cell.value = tunnelViewModel.interfaceData[field]
|
|
|
|
if field == .dns { // While editing DNS, you might directly set exclude private IPs
|
|
|
|
cell.onValueBeingEdited = { [weak self] value in
|
|
|
|
self?.tunnelViewModel.interfaceData[field] = value
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2019-02-21 12:26:52 +00:00
|
|
|
cell.onValueChanged = { [weak self] oldValue, newValue in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let isAllowedIPsChanged = self.tunnelViewModel.updateDNSServersInAllowedIPsIfRequired(oldDNSServers: oldValue, newDNSServers: newValue)
|
|
|
|
if isAllowedIPsChanged {
|
|
|
|
let section = self.sections.firstIndex { if case .peer(_) = $0 { return true } else { return false } }
|
|
|
|
if let section = section, let row = self.peerFields.firstIndex(of: .allowedIPs) {
|
|
|
|
self.tableView.reloadRows(at: [IndexPath(row: row, section: section)], with: .none)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
} else {
|
2019-02-21 12:26:52 +00:00
|
|
|
cell.onValueChanged = { [weak self] _, value in
|
2018-12-12 21:33:14 +00:00
|
|
|
self?.tunnelViewModel.interfaceData[field] = value
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
// Compute public key live
|
|
|
|
if field == .privateKey {
|
|
|
|
cell.onValueBeingEdited = { [weak self] value in
|
|
|
|
guard let self = self else { return }
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
self.tunnelViewModel.interfaceData[.privateKey] = value
|
|
|
|
if let row = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .publicKey) {
|
|
|
|
self.tableView.reloadRows(at: [IndexPath(row: row, section: indexPath.section)], with: .none)
|
2018-10-24 08:41:34 +00:00
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
return cell
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
private func peerCell(for tableView: UITableView, at indexPath: IndexPath, with peerData: TunnelViewModel.PeerData) -> UITableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs }
|
|
|
|
let field = peerFieldsToShow[indexPath.row]
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
switch field {
|
|
|
|
case .deletePeer:
|
|
|
|
return deletePeerCell(for: tableView, at: indexPath, peerData: peerData, field: field)
|
|
|
|
case .excludePrivateIPs:
|
|
|
|
return excludePrivateIPsCell(for: tableView, at: indexPath, peerData: peerData, field: field)
|
|
|
|
default:
|
|
|
|
return peerFieldKeyValueCell(for: tableView, at: indexPath, peerData: peerData, field: field)
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
private func deletePeerCell(for tableView: UITableView, at indexPath: IndexPath, peerData: TunnelViewModel.PeerData, field: TunnelViewModel.PeerField) -> 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 = field.localizedUIString
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.hasDestructiveAction = true
|
|
|
|
cell.onTapped = { [weak self, weak peerData] in
|
2018-12-22 01:52:51 +00:00
|
|
|
guard let self = self, let peerData = peerData else { return }
|
2019-03-18 19:54:06 +00:00
|
|
|
ConfirmationAlertPresenter.showConfirmationAlert(message: tr("deletePeerConfirmationAlertMessage"),
|
|
|
|
buttonTitle: tr("deletePeerConfirmationAlertButtonTitle"),
|
|
|
|
from: cell, presentingVC: self) { [weak self] in
|
2018-12-12 17:40:57 +00:00
|
|
|
guard let self = self else { return }
|
2018-12-12 21:33:14 +00:00
|
|
|
let removedSectionIndices = self.deletePeer(peer: peerData)
|
|
|
|
let shouldShowExcludePrivateIPs = (self.tunnelViewModel.peersData.count == 1 && self.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl)
|
2019-02-08 16:05:58 +00:00
|
|
|
|
|
|
|
//swiftlint:disable:next trailing_closure
|
2018-12-12 21:33:14 +00:00
|
|
|
tableView.performBatchUpdates({
|
|
|
|
self.tableView.deleteSections(removedSectionIndices, with: .fade)
|
|
|
|
if shouldShowExcludePrivateIPs {
|
|
|
|
if let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
let rowIndexPath = IndexPath(row: row, section: self.interfaceFieldsBySection.count /* First peer section */)
|
|
|
|
self.tableView.insertRows(at: [rowIndexPath], with: .fade)
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
})
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
private func excludePrivateIPsCell(for tableView: UITableView, at indexPath: IndexPath, peerData: TunnelViewModel.PeerData, field: TunnelViewModel.PeerField) -> UITableViewCell {
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.message = field.localizedUIString
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.isEnabled = peerData.shouldAllowExcludePrivateIPsControl
|
|
|
|
cell.isOn = peerData.excludePrivateIPsValue
|
|
|
|
cell.onSwitchToggled = { [weak self] isOn in
|
|
|
|
guard let self = self else { return }
|
|
|
|
peerData.excludePrivateIPsValueChanged(isOn: isOn, dnsServers: self.tunnelViewModel.interfaceData[.dns])
|
|
|
|
if let row = self.peerFields.firstIndex(of: .allowedIPs) {
|
|
|
|
self.tableView.reloadRows(at: [IndexPath(row: row, section: indexPath.section)], with: .none)
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
private func peerFieldKeyValueCell(for tableView: UITableView, at indexPath: IndexPath, peerData: TunnelViewModel.PeerData, field: TunnelViewModel.PeerField) -> UITableViewCell {
|
2018-12-15 02:02:37 +00:00
|
|
|
let cell: TunnelEditEditableKeyValueCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.key = field.localizedUIString
|
2018-12-12 21:33:14 +00:00
|
|
|
|
|
|
|
switch field {
|
|
|
|
case .publicKey:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextRequired")
|
2018-12-13 03:09:52 +00:00
|
|
|
cell.keyboardType = .default
|
|
|
|
case .preSharedKey, .endpoint:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextOptional")
|
2018-12-13 03:09:52 +00:00
|
|
|
cell.keyboardType = .default
|
|
|
|
case .allowedIPs:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextOptional")
|
2018-12-13 03:09:52 +00:00
|
|
|
cell.keyboardType = .numbersAndPunctuation
|
2018-12-12 21:33:14 +00:00
|
|
|
case .persistentKeepAlive:
|
2018-12-18 11:00:16 +00:00
|
|
|
cell.placeholderText = tr("tunnelEditPlaceholderTextOff")
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.keyboardType = .numberPad
|
2018-12-13 03:09:52 +00:00
|
|
|
case .excludePrivateIPs, .deletePeer:
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.keyboardType = .default
|
2019-01-31 12:47:04 +00:00
|
|
|
case .rxBytes, .txBytes, .lastHandshakeTime:
|
|
|
|
fatalError()
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
cell.isValueValid = !peerData.fieldsWithError.contains(field)
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.value = peerData[field]
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
if field == .allowedIPs {
|
2019-02-08 16:05:58 +00:00
|
|
|
let firstInterfaceSection = sections.firstIndex { $0 == .interface }!
|
|
|
|
let interfaceSubSection = interfaceFieldsBySection.firstIndex { $0.contains(.dns) }!
|
|
|
|
let dnsRow = interfaceFieldsBySection[interfaceSubSection].firstIndex { $0 == .dns }!
|
2018-12-22 04:30:35 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.onValueBeingEdited = { [weak self, weak peerData] value in
|
2018-12-13 03:09:52 +00:00
|
|
|
guard let self = self, let peerData = peerData else { return }
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
let oldValue = peerData.shouldAllowExcludePrivateIPsControl
|
|
|
|
peerData[.allowedIPs] = value
|
2018-12-22 01:52:51 +00:00
|
|
|
if oldValue != peerData.shouldAllowExcludePrivateIPsControl, let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
if peerData.shouldAllowExcludePrivateIPsControl {
|
|
|
|
self.tableView.insertRows(at: [IndexPath(row: row, section: indexPath.section)], with: .fade)
|
|
|
|
} else {
|
|
|
|
self.tableView.deleteRows(at: [IndexPath(row: row, section: indexPath.section)], with: .fade)
|
2018-10-29 11:08:32 +00:00
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-22 04:30:35 +00:00
|
|
|
|
2018-12-22 01:52:51 +00:00
|
|
|
tableView.reloadRows(at: [IndexPath(row: dnsRow, section: firstInterfaceSection + interfaceSubSection)], with: .none)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
} else {
|
2019-02-21 12:26:52 +00:00
|
|
|
cell.onValueChanged = { [weak peerData] _, value in
|
2018-12-13 03:09:52 +00:00
|
|
|
peerData?[field] = value
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
2018-12-13 04:26:04 +00:00
|
|
|
|
2018-12-12 21:33:14 +00:00
|
|
|
return cell
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addPeerCell(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("addPeerButtonTitle")
|
2018-12-12 17:40:57 +00:00
|
|
|
cell.onTapped = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let shouldHideExcludePrivateIPs = (self.tunnelViewModel.peersData.count == 1 && self.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl)
|
|
|
|
let addedSectionIndices = self.appendEmptyPeer()
|
|
|
|
tableView.performBatchUpdates({
|
2018-12-12 21:33:14 +00:00
|
|
|
tableView.insertSections(addedSectionIndices, with: .fade)
|
2018-12-12 17:40:57 +00:00
|
|
|
if shouldHideExcludePrivateIPs {
|
|
|
|
if let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
let rowIndexPath = IndexPath(row: row, section: self.interfaceFieldsBySection.count /* First peer section */)
|
2018-12-12 21:33:14 +00:00
|
|
|
self.tableView.deleteRows(at: [rowIndexPath], with: .fade)
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}, completion: nil)
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
2019-02-27 08:00:57 +00:00
|
|
|
let field = onDemandFields[indexPath.row]
|
|
|
|
if indexPath.row < 2 {
|
2018-12-14 23:12:59 +00:00
|
|
|
let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath)
|
2019-02-27 08:00:57 +00:00
|
|
|
cell.message = field.localizedUIString
|
|
|
|
cell.isOn = onDemandViewModel.isEnabled(field: field)
|
2018-12-12 21:33:14 +00:00
|
|
|
cell.onSwitchToggled = { [weak self] isOn in
|
2018-12-12 17:40:57 +00:00
|
|
|
guard let self = self else { return }
|
2019-02-27 08:00:57 +00:00
|
|
|
self.onDemandViewModel.setEnabled(field: field, isEnabled: isOn)
|
2019-02-08 16:05:58 +00:00
|
|
|
let section = self.sections.firstIndex { $0 == .onDemand }!
|
2019-02-27 08:00:57 +00:00
|
|
|
let indexPath = IndexPath(row: 2, section: section)
|
|
|
|
if field == .wiFiInterface {
|
|
|
|
if isOn {
|
|
|
|
tableView.insertRows(at: [indexPath], with: .fade)
|
|
|
|
} else {
|
|
|
|
tableView.deleteRows(at: [indexPath], with: .fade)
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
return cell
|
|
|
|
} else {
|
2019-02-27 08:00:57 +00:00
|
|
|
let cell: ChevronCell = tableView.dequeueReusableCell(for: indexPath)
|
|
|
|
cell.message = field.localizedUIString
|
2019-03-09 10:05:49 +00:00
|
|
|
cell.detailMessage = onDemandViewModel.localizedSSIDDescription
|
2018-12-12 17:40:57 +00:00
|
|
|
return cell
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEmptyPeer() -> IndexSet {
|
2018-10-23 09:53:18 +00:00
|
|
|
tunnelViewModel.appendEmptyPeer()
|
2018-12-12 21:33:14 +00:00
|
|
|
loadSections()
|
2018-10-23 09:53:18 +00:00
|
|
|
let addedPeerIndex = tunnelViewModel.peersData.count - 1
|
2018-12-12 21:33:14 +00:00
|
|
|
return IndexSet(integer: interfaceFieldsBySection.count + addedPeerIndex)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
func deletePeer(peer: TunnelViewModel.PeerData) -> IndexSet {
|
|
|
|
tunnelViewModel.deletePeer(peer: peer)
|
2018-12-12 21:33:14 +00:00
|
|
|
loadSections()
|
|
|
|
return IndexSet(integer: interfaceFieldsBySection.count + peer.index)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 13:20:09 +00:00
|
|
|
extension TunnelEditTableViewController {
|
|
|
|
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
2019-02-27 08:00:57 +00:00
|
|
|
if case .onDemand = sections[indexPath.section], indexPath.row == 2 {
|
2018-12-12 21:33:14 +00:00
|
|
|
return indexPath
|
2018-11-10 13:20:09 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2018-12-12 21:33:14 +00:00
|
|
|
switch sections[indexPath.section] {
|
|
|
|
case .onDemand:
|
2019-02-27 08:00:57 +00:00
|
|
|
assert(indexPath.row == 2)
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
let ssidOptionVC = SSIDOptionEditTableViewController(option: onDemandViewModel.ssidOption, ssids: onDemandViewModel.selectedSSIDs)
|
|
|
|
ssidOptionVC.delegate = self
|
|
|
|
navigationController?.pushViewController(ssidOptionVC, animated: true)
|
2018-12-12 21:33:14 +00:00
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
}
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 08:00:57 +00:00
|
|
|
|
|
|
|
extension TunnelEditTableViewController: SSIDOptionEditTableViewControllerDelegate {
|
|
|
|
func ssidOptionSaved(option: ActivateOnDemandViewModel.OnDemandSSIDOption, ssids: [String]) {
|
2019-03-08 09:25:08 +00:00
|
|
|
onDemandViewModel.selectedSSIDs = ssids
|
|
|
|
onDemandViewModel.ssidOption = option
|
|
|
|
onDemandViewModel.fixSSIDOption()
|
|
|
|
if let onDemandSection = sections.firstIndex(where: { $0 == .onDemand }) {
|
|
|
|
if let ssidRowIndex = onDemandFields.firstIndex(of: .ssid) {
|
|
|
|
let indexPath = IndexPath(row: ssidRowIndex, section: onDemandSection)
|
|
|
|
tableView.reloadRows(at: [indexPath], with: .none)
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 08:00:57 +00:00
|
|
|
}
|
|
|
|
}
|