2019-04-29 16:13:49 +00:00
|
|
|
//
|
|
|
|
// NetworkSettingsViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/29/19.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2019-04-29 16:13:49 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-04-29 16:13:49 +00:00
|
|
|
import TunnelKit
|
|
|
|
import SwiftyBeaver
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
private enum FieldTag: Int {
|
2019-10-25 15:53:07 +00:00
|
|
|
case dnsAddress = 100
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case dnsDomain = 200
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
case proxyAddress = 301
|
|
|
|
|
|
|
|
case proxyPort = 302
|
|
|
|
|
2019-10-23 10:31:49 +00:00
|
|
|
case proxyAutoConfigurationURL = 303
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case proxyBypass = 400
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Offsets {
|
2019-10-25 15:53:07 +00:00
|
|
|
static let dnsAddress = 0
|
|
|
|
|
|
|
|
static let dnsDomain = 0
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-11-04 10:38:53 +00:00
|
|
|
static let proxyBypass = 3
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NetworkSettingsViewController: UITableViewController {
|
|
|
|
var profile: ConnectionProfile?
|
|
|
|
|
2019-06-28 08:53:24 +00:00
|
|
|
private lazy var networkChoices = ProfileNetworkChoices.with(profile: profile)
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-06-22 07:34:43 +00:00
|
|
|
private lazy var clientNetworkSettings = profile?.clientNetworkSettings
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-06-28 10:01:00 +00:00
|
|
|
private let networkSettings = ProfileNetworkSettings()
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableHost
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
func reloadModel() {
|
|
|
|
model.clear()
|
|
|
|
|
2019-10-25 16:58:25 +00:00
|
|
|
// sections (candidate)
|
|
|
|
var sections: [SectionType] = []
|
|
|
|
sections.append(.choices)
|
2019-04-29 16:13:49 +00:00
|
|
|
if networkChoices.gateway != .server {
|
2019-10-25 16:58:25 +00:00
|
|
|
sections.append(.manualGateway)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
if networkChoices.dns != .server {
|
2019-10-25 16:58:25 +00:00
|
|
|
sections.append(.manualDNSServers)
|
|
|
|
sections.append(.manualDNSDomains)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
if networkChoices.proxy != .server {
|
2019-10-25 16:58:25 +00:00
|
|
|
sections.append(.manualProxy)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
2020-12-28 20:58:06 +00:00
|
|
|
if networkChoices.mtu == .manual {
|
2020-12-28 14:04:24 +00:00
|
|
|
sections.append(.manualMTU)
|
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
// headers
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setHeader("", forSection: .choices)
|
|
|
|
model.setHeader(L10n.Core.NetworkSettings.Gateway.title, forSection: .manualGateway)
|
|
|
|
model.setHeader(L10n.Core.NetworkSettings.Proxy.title, forSection: .manualProxy)
|
2020-12-28 14:04:24 +00:00
|
|
|
model.setHeader(L10n.Core.NetworkSettings.Mtu.title, forSection: .manualMTU)
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
// footers
|
2019-06-21 21:15:43 +00:00
|
|
|
// model.setFooter(L10n.Core.Configuration.Sections.Reset.footer, for: .reset)
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
// rows
|
2020-12-28 14:04:24 +00:00
|
|
|
model.set([.gateway, .dns, .proxy, .mtu], forSection: .choices)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set([.gatewayIPv4, .gatewayIPv6], forSection: .manualGateway)
|
2020-12-28 14:04:24 +00:00
|
|
|
model.set([.mtuBytes], forSection: .manualMTU)
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-10-25 16:58:25 +00:00
|
|
|
var dnsServers: [RowType] = Array(repeating: .dnsAddress, count: networkSettings.dnsServers?.count ?? 0)
|
2019-04-29 16:13:49 +00:00
|
|
|
if networkChoices.dns == .manual {
|
2019-10-25 16:58:25 +00:00
|
|
|
dnsServers.append(.dnsAddAddress)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
2019-10-25 16:58:25 +00:00
|
|
|
model.set(dnsServers, forSection: .manualDNSServers)
|
2019-10-25 15:53:07 +00:00
|
|
|
|
2019-10-25 16:58:25 +00:00
|
|
|
var dnsDomains: [RowType] = Array(repeating: .dnsDomain, count: networkSettings.dnsSearchDomains?.count ?? 0)
|
2019-10-25 15:53:07 +00:00
|
|
|
if networkChoices.dns == .manual {
|
2019-10-25 16:58:25 +00:00
|
|
|
dnsDomains.append(.dnsAddDomain)
|
2019-10-25 15:53:07 +00:00
|
|
|
}
|
2019-10-25 16:58:25 +00:00
|
|
|
model.set(dnsDomains, forSection: .manualDNSDomains)
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
var proxyRows: [RowType] = Array(repeating: .proxyBypass, count: networkSettings.proxyBypassDomains?.count ?? 0)
|
|
|
|
proxyRows.insert(.proxyAddress, at: 0)
|
|
|
|
proxyRows.insert(.proxyPort, at: 1)
|
2019-10-23 10:31:49 +00:00
|
|
|
proxyRows.insert(.proxyAutoConfigurationURL, at: 2)
|
2019-04-29 16:13:49 +00:00
|
|
|
if networkChoices.proxy == .manual {
|
|
|
|
proxyRows.append(.proxyAddBypass)
|
|
|
|
}
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set(proxyRows, forSection: .manualProxy)
|
2019-10-25 16:58:25 +00:00
|
|
|
|
|
|
|
// refine sections before add (DNS is tricky)
|
|
|
|
if !dnsServers.isEmpty {
|
|
|
|
model.setHeader(L10n.Core.NetworkSettings.Dns.title, forSection: .manualDNSServers)
|
|
|
|
} else if !dnsDomains.isEmpty {
|
|
|
|
sections.removeAll { $0 == .manualDNSServers }
|
|
|
|
model.setHeader(L10n.Core.NetworkSettings.Dns.title, forSection: .manualDNSDomains)
|
|
|
|
} else {
|
|
|
|
sections.removeAll { $0 == .manualDNSServers }
|
|
|
|
sections.removeAll { $0 == .manualDNSDomains }
|
|
|
|
}
|
|
|
|
for s in sections {
|
|
|
|
model.add(s)
|
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
updateGateway(networkChoices.gateway)
|
|
|
|
updateDNS(networkChoices.dns)
|
|
|
|
updateProxy(networkChoices.proxy)
|
2020-12-28 14:04:24 +00:00
|
|
|
updateMTU(networkChoices.mtu)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
2019-06-28 10:01:00 +00:00
|
|
|
|
|
|
|
commitChanges()
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
private func updateGateway(_ choice: NetworkChoice) {
|
|
|
|
networkChoices.gateway = choice
|
|
|
|
switch networkChoices.gateway {
|
|
|
|
case .client:
|
|
|
|
if let settings = clientNetworkSettings {
|
|
|
|
networkSettings.copyGateway(from: settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .server:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .manual:
|
|
|
|
if let settings = profile?.manualNetworkSettings {
|
|
|
|
networkSettings.copyGateway(from: settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateDNS(_ choice: NetworkChoice) {
|
|
|
|
networkChoices.dns = choice
|
|
|
|
switch networkChoices.dns {
|
|
|
|
case .client:
|
|
|
|
if let settings = clientNetworkSettings {
|
|
|
|
networkSettings.copyDNS(from: settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .server:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .manual:
|
|
|
|
if let settings = profile?.manualNetworkSettings {
|
|
|
|
networkSettings.copyDNS(from: settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateProxy(_ choice: NetworkChoice) {
|
|
|
|
networkChoices.proxy = choice
|
|
|
|
switch networkChoices.proxy {
|
|
|
|
case .client:
|
|
|
|
if let settings = clientNetworkSettings {
|
|
|
|
networkSettings.copyProxy(from: settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .server:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .manual:
|
|
|
|
if let settings = profile?.manualNetworkSettings {
|
|
|
|
networkSettings.copyProxy(from: settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 14:04:24 +00:00
|
|
|
private func updateMTU(_ choice: NetworkChoice) {
|
|
|
|
networkChoices.mtu = choice
|
|
|
|
switch networkChoices.mtu {
|
|
|
|
case .client:
|
|
|
|
if let settings = clientNetworkSettings {
|
|
|
|
networkSettings.copyMTU(from: settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .server:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .manual:
|
|
|
|
if let settings = profile?.manualNetworkSettings {
|
|
|
|
networkSettings.copyMTU(from: settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
private func commitTextField(_ field: UITextField) {
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
// DNS: servers, domains
|
2019-10-23 10:31:49 +00:00
|
|
|
// Proxy: address, port, PAC, bypass domains
|
2019-10-25 15:53:07 +00:00
|
|
|
|
|
|
|
let text = field.text ?? ""
|
|
|
|
|
|
|
|
if field.tag >= FieldTag.dnsAddress.rawValue && field.tag < FieldTag.dnsDomain.rawValue {
|
|
|
|
let i = field.tag - FieldTag.dnsAddress.rawValue
|
|
|
|
if let _ = networkSettings.dnsServers {
|
|
|
|
networkSettings.dnsServers?[i] = text
|
|
|
|
} else {
|
|
|
|
networkSettings.dnsServers = [text]
|
|
|
|
}
|
|
|
|
} else if field.tag >= FieldTag.dnsDomain.rawValue && field.tag < FieldTag.proxyAddress.rawValue {
|
|
|
|
let i = field.tag - FieldTag.dnsDomain.rawValue
|
|
|
|
if let _ = networkSettings.dnsSearchDomains {
|
|
|
|
networkSettings.dnsSearchDomains?[i] = text
|
|
|
|
} else {
|
|
|
|
networkSettings.dnsSearchDomains = [text]
|
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
} else if field.tag == FieldTag.proxyAddress.rawValue {
|
|
|
|
networkSettings.proxyAddress = field.text
|
|
|
|
} else if field.tag == FieldTag.proxyPort.rawValue {
|
|
|
|
networkSettings.proxyPort = UInt16(field.text ?? "0")
|
2019-10-23 10:31:49 +00:00
|
|
|
} else if field.tag == FieldTag.proxyAutoConfigurationURL.rawValue {
|
|
|
|
if let string = field.text {
|
|
|
|
networkSettings.proxyAutoConfigurationURL = URL(string: string)
|
|
|
|
} else {
|
|
|
|
networkSettings.proxyAutoConfigurationURL = nil
|
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
} else if field.tag >= FieldTag.proxyBypass.rawValue {
|
|
|
|
let i = field.tag - FieldTag.proxyBypass.rawValue
|
2019-10-25 15:53:07 +00:00
|
|
|
if let _ = networkSettings.proxyBypassDomains {
|
|
|
|
networkSettings.proxyBypassDomains?[i] = text
|
|
|
|
} else {
|
|
|
|
networkSettings.proxyBypassDomains = [text]
|
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("Network settings: \(networkSettings)")
|
|
|
|
}
|
2019-06-28 10:01:00 +00:00
|
|
|
|
|
|
|
private func commitChanges() {
|
2019-07-03 15:58:14 +00:00
|
|
|
let settings = profile?.manualNetworkSettings ?? ProfileNetworkSettings()
|
2019-06-28 10:01:00 +00:00
|
|
|
profile?.networkChoices = networkChoices
|
|
|
|
if networkChoices.gateway == .manual {
|
|
|
|
settings.copyGateway(from: networkSettings)
|
|
|
|
}
|
|
|
|
if networkChoices.dns == .manual {
|
|
|
|
settings.copyDNS(from: networkSettings)
|
|
|
|
}
|
|
|
|
if networkChoices.proxy == .manual {
|
|
|
|
settings.copyProxy(from: networkSettings)
|
|
|
|
}
|
2020-12-28 14:04:24 +00:00
|
|
|
if networkChoices.mtu == .manual {
|
|
|
|
settings.copyMTU(from: networkSettings)
|
|
|
|
}
|
2019-07-03 15:58:14 +00:00
|
|
|
profile?.manualNetworkSettings = settings
|
2019-06-28 10:01:00 +00:00
|
|
|
}
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension NetworkSettingsViewController {
|
|
|
|
enum SectionType: Int {
|
|
|
|
case choices
|
|
|
|
|
|
|
|
case manualGateway
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case manualDNSServers
|
|
|
|
|
|
|
|
case manualDNSDomains
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
case manualProxy
|
2020-12-28 14:04:24 +00:00
|
|
|
|
|
|
|
case manualMTU
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType: Int {
|
|
|
|
case gateway
|
|
|
|
|
|
|
|
case dns
|
|
|
|
|
|
|
|
case proxy
|
|
|
|
|
2020-12-28 14:04:24 +00:00
|
|
|
case mtu
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case gatewayIPv4
|
|
|
|
|
|
|
|
case gatewayIPv6
|
|
|
|
|
|
|
|
case dnsAddress
|
|
|
|
|
|
|
|
case dnsAddAddress
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case dnsDomain
|
|
|
|
|
|
|
|
case dnsAddDomain
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case proxyAddress
|
|
|
|
|
|
|
|
case proxyPort
|
|
|
|
|
2019-10-23 10:31:49 +00:00
|
|
|
case proxyAutoConfigurationURL
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case proxyBypass
|
|
|
|
|
|
|
|
case proxyAddBypass
|
2020-12-28 14:04:24 +00:00
|
|
|
|
|
|
|
case mtuBytes
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.footer(forSection: section)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
return model.headerHeight(for: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let row = model.row(at: indexPath)
|
|
|
|
|
|
|
|
switch row {
|
|
|
|
case .gateway:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 16:58:25 +00:00
|
|
|
cell.leftText = L10n.Core.NetworkSettings.Gateway.title
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.rightText = networkChoices.gateway.description
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .dns:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 16:58:25 +00:00
|
|
|
cell.leftText = L10n.Core.NetworkSettings.Dns.title
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.rightText = networkChoices.dns.description
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .proxy:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 16:58:25 +00:00
|
|
|
cell.leftText = L10n.Core.NetworkSettings.Proxy.title
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.rightText = networkChoices.proxy.description
|
|
|
|
return cell
|
2020-12-28 14:04:24 +00:00
|
|
|
|
|
|
|
case .mtu:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = L10n.Core.NetworkSettings.Mtu.title
|
|
|
|
cell.rightText = networkChoices.mtu.description
|
|
|
|
return cell
|
2019-04-29 16:13:49 +00:00
|
|
|
|
2019-04-30 11:10:22 +00:00
|
|
|
case .gatewayIPv4:
|
|
|
|
let cell = Cells.toggle.dequeue(from: tableView, for: indexPath, tag: row.rawValue, delegate: self)
|
|
|
|
cell.caption = "IPv4"
|
|
|
|
cell.toggle.isEnabled = (networkChoices.gateway == .manual)
|
|
|
|
cell.isOn = networkSettings.gatewayPolicies?.contains(.IPv4) ?? false
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
|
|
|
|
2019-04-30 11:10:22 +00:00
|
|
|
case .gatewayIPv6:
|
|
|
|
let cell = Cells.toggle.dequeue(from: tableView, for: indexPath, tag: row.rawValue, delegate: self)
|
|
|
|
cell.caption = "IPv6"
|
|
|
|
cell.toggle.isEnabled = (networkChoices.gateway == .manual)
|
|
|
|
cell.isOn = networkSettings.gatewayPolicies?.contains(.IPv6) ?? false
|
|
|
|
return cell
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case .dnsAddress:
|
|
|
|
let i = indexPath.row - Offsets.dnsAddress
|
|
|
|
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.Core.Global.Captions.address
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.tag = FieldTag.dnsAddress.rawValue + i
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.text = networkSettings.dnsServers?[i]
|
|
|
|
cell.field.clearButtonMode = .always
|
2019-10-11 11:47:27 +00:00
|
|
|
cell.field.keyboardType = .numbersAndPunctuation
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
2019-05-01 21:28:03 +00:00
|
|
|
cell.field.isEnabled = (networkChoices.dns == .manual)
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
|
|
|
|
|
|
|
case .dnsAddAddress:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.App.NetworkSettings.Cells.AddDnsServer.caption
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case .dnsDomain:
|
|
|
|
let i = indexPath.row - Offsets.dnsDomain
|
|
|
|
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.caption = L10n.Core.NetworkSettings.Dns.Cells.Domain.caption
|
|
|
|
cell.field.tag = FieldTag.dnsDomain.rawValue + i
|
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
|
|
|
cell.field.text = networkSettings.dnsSearchDomains?[i]
|
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.keyboardType = .asciiCapable
|
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
|
|
|
cell.field.isEnabled = (networkChoices.dns == .manual)
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .dnsAddDomain:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-10-25 15:53:07 +00:00
|
|
|
cell.leftText = L10n.App.NetworkSettings.Cells.AddDnsDomain.caption
|
|
|
|
return cell
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case .proxyAddress:
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.Core.Global.Captions.address
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.tag = FieldTag.proxyAddress.rawValue
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.text = networkSettings.proxyAddress
|
|
|
|
cell.field.clearButtonMode = .always
|
2019-10-11 11:47:27 +00:00
|
|
|
cell.field.keyboardType = .numbersAndPunctuation
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
2019-05-01 21:28:03 +00:00
|
|
|
cell.field.isEnabled = (networkChoices.proxy == .manual)
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
|
|
|
|
|
|
|
case .proxyPort:
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.Core.Global.Captions.port
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.tag = FieldTag.proxyPort.rawValue
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.text = networkSettings.proxyPort?.description
|
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.keyboardType = .numberPad
|
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
2019-05-01 21:28:03 +00:00
|
|
|
cell.field.isEnabled = (networkChoices.proxy == .manual)
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
2019-10-23 10:31:49 +00:00
|
|
|
|
|
|
|
case .proxyAutoConfigurationURL:
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.caption = "PAC"
|
|
|
|
cell.field.tag = FieldTag.proxyAutoConfigurationURL.rawValue
|
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
|
|
|
cell.field.text = networkSettings.proxyAutoConfigurationURL?.absoluteString
|
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.keyboardType = .asciiCapable
|
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
|
|
|
cell.field.isEnabled = (networkChoices.proxy == .manual)
|
|
|
|
return cell
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
case .proxyBypass:
|
|
|
|
let i = indexPath.row - Offsets.proxyBypass
|
|
|
|
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.App.NetworkSettings.Cells.ProxyBypass.caption
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.tag = FieldTag.proxyBypass.rawValue + i
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.field.placeholder = L10n.Core.Global.Values.none
|
2019-04-29 16:13:49 +00:00
|
|
|
cell.field.text = networkSettings.proxyBypassDomains?[i]
|
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.keyboardType = .asciiCapable
|
|
|
|
cell.captionWidth = 160.0
|
|
|
|
cell.delegate = self
|
2019-05-01 21:28:03 +00:00
|
|
|
cell.field.isEnabled = (networkChoices.proxy == .manual)
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
|
|
|
|
|
|
|
case .proxyAddBypass:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.App.NetworkSettings.Cells.AddProxyBypass.caption
|
2019-04-29 16:13:49 +00:00
|
|
|
return cell
|
2020-12-28 14:04:24 +00:00
|
|
|
|
|
|
|
case .mtuBytes:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = L10n.Core.NetworkSettings.Mtu.Cells.Bytes.caption
|
|
|
|
cell.rightText = networkSettings.mtuBytes?.description ?? "Default"
|
|
|
|
return cell
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
let cell = tableView.cellForRow(at: indexPath)
|
|
|
|
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .gateway:
|
2019-10-11 08:56:23 +00:00
|
|
|
let vc = SingleOptionViewController<NetworkChoice>()
|
2019-10-25 17:30:21 +00:00
|
|
|
vc.applyTint(.current)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.title = (cell as? SettingTableViewCell)?.leftText
|
2019-06-22 07:34:43 +00:00
|
|
|
vc.options = NetworkChoice.choices(for: profile)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.descriptionBlock = { $0.description }
|
2019-10-25 17:28:49 +00:00
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.selectedOption = networkChoices.gateway
|
|
|
|
vc.selectionBlock = { [weak self] in
|
|
|
|
self?.updateGateway($0)
|
|
|
|
self?.navigationController?.popViewController(animated: true)
|
|
|
|
}
|
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
|
|
|
|
case .dns:
|
2019-10-11 08:56:23 +00:00
|
|
|
let vc = SingleOptionViewController<NetworkChoice>()
|
2019-10-25 17:30:21 +00:00
|
|
|
vc.applyTint(.current)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.title = (cell as? SettingTableViewCell)?.leftText
|
2019-06-22 07:34:43 +00:00
|
|
|
vc.options = NetworkChoice.choices(for: profile)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.descriptionBlock = { $0.description }
|
2019-10-25 17:28:49 +00:00
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.selectedOption = networkChoices.dns
|
|
|
|
vc.selectionBlock = { [weak self] in
|
|
|
|
self?.updateDNS($0)
|
|
|
|
self?.navigationController?.popViewController(animated: true)
|
|
|
|
}
|
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
|
|
|
|
case .proxy:
|
2019-10-11 08:56:23 +00:00
|
|
|
let vc = SingleOptionViewController<NetworkChoice>()
|
2019-10-25 17:30:21 +00:00
|
|
|
vc.applyTint(.current)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.title = (cell as? SettingTableViewCell)?.leftText
|
2019-06-22 07:34:43 +00:00
|
|
|
vc.options = NetworkChoice.choices(for: profile)
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.descriptionBlock = { $0.description }
|
2019-10-25 17:28:49 +00:00
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
vc.selectedOption = networkChoices.proxy
|
|
|
|
vc.selectionBlock = { [weak self] in
|
|
|
|
self?.updateProxy($0)
|
|
|
|
self?.navigationController?.popViewController(animated: true)
|
|
|
|
}
|
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
|
2020-12-28 14:04:24 +00:00
|
|
|
case .mtu:
|
|
|
|
let vc = SingleOptionViewController<NetworkChoice>()
|
|
|
|
vc.applyTint(.current)
|
|
|
|
vc.title = (cell as? SettingTableViewCell)?.leftText
|
|
|
|
vc.options = NetworkChoice.choices(for: profile)
|
|
|
|
vc.descriptionBlock = { $0.description }
|
|
|
|
|
|
|
|
vc.selectedOption = networkChoices.mtu
|
|
|
|
vc.selectionBlock = { [weak self] in
|
|
|
|
self?.updateMTU($0)
|
|
|
|
self?.navigationController?.popViewController(animated: true)
|
|
|
|
}
|
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case .dnsAddAddress:
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
|
|
|
var dnsServers = networkSettings.dnsServers ?? []
|
|
|
|
dnsServers.append("")
|
|
|
|
networkSettings.dnsServers = dnsServers
|
|
|
|
reloadModel()
|
|
|
|
tableView.insertRows(at: [indexPath], with: .automatic)
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case .dnsAddDomain:
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
|
|
|
var dnsSearchDomains = networkSettings.dnsSearchDomains ?? []
|
|
|
|
dnsSearchDomains.append("")
|
|
|
|
networkSettings.dnsSearchDomains = dnsSearchDomains
|
|
|
|
reloadModel()
|
|
|
|
tableView.insertRows(at: [indexPath], with: .automatic)
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case .proxyAddBypass:
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
|
|
|
var bypassDomains = networkSettings.proxyBypassDomains ?? []
|
|
|
|
bypassDomains.append("")
|
|
|
|
networkSettings.proxyBypassDomains = bypassDomains
|
|
|
|
reloadModel()
|
|
|
|
tableView.insertRows(at: [indexPath], with: .automatic)
|
|
|
|
|
2020-12-28 14:04:24 +00:00
|
|
|
case .mtuBytes:
|
|
|
|
guard networkChoices.mtu == .manual else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
let vc = SingleOptionViewController<Int>()
|
|
|
|
vc.applyTint(.current)
|
|
|
|
vc.title = (cell as? SettingTableViewCell)?.leftText
|
|
|
|
vc.options = ProfileNetworkSettings.mtuOptions
|
|
|
|
vc.descriptionBlock = { $0.description }
|
|
|
|
|
|
|
|
vc.selectedOption = networkSettings.mtuBytes
|
|
|
|
vc.selectionBlock = { [weak self] in
|
|
|
|
self?.networkSettings.mtuBytes = $0
|
|
|
|
self?.navigationController?.popViewController(animated: true)
|
|
|
|
}
|
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:10:22 +00:00
|
|
|
private func handle(row: RowType, cell: ToggleTableViewCell) {
|
|
|
|
switch row {
|
|
|
|
case .gatewayIPv4:
|
|
|
|
guard networkChoices.gateway == .manual else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var policies = networkSettings.gatewayPolicies ?? []
|
|
|
|
if cell.toggle.isOn {
|
|
|
|
policies.append(.IPv4)
|
|
|
|
} else {
|
|
|
|
policies.removeAll { $0 == .IPv4 }
|
|
|
|
}
|
|
|
|
policies.sort { $0.rawValue < $1.rawValue }
|
|
|
|
networkSettings.gatewayPolicies = policies
|
|
|
|
|
|
|
|
case .gatewayIPv6:
|
|
|
|
guard networkChoices.gateway == .manual else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var policies = networkSettings.gatewayPolicies ?? []
|
|
|
|
if cell.toggle.isOn {
|
|
|
|
policies.append(.IPv6)
|
|
|
|
} else {
|
|
|
|
policies.removeAll { $0 == .IPv6 }
|
|
|
|
}
|
|
|
|
policies.sort { $0.rawValue < $1.rawValue }
|
|
|
|
networkSettings.gatewayPolicies = policies
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
|
|
switch model.row(at: indexPath) {
|
2019-10-25 15:53:07 +00:00
|
|
|
case .dnsAddress, .dnsDomain, .proxyBypass:
|
2019-04-29 16:13:49 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .dnsAddress:
|
|
|
|
networkSettings.dnsServers?.remove(at: indexPath.row - Offsets.dnsAddress)
|
|
|
|
|
2019-10-25 15:53:07 +00:00
|
|
|
case .dnsDomain:
|
|
|
|
networkSettings.dnsSearchDomains?.remove(at: indexPath.row - Offsets.dnsDomain)
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
case .proxyBypass:
|
|
|
|
networkSettings.proxyBypassDomains?.remove(at: indexPath.row - Offsets.proxyBypass)
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
reloadModel()
|
|
|
|
tableView.deleteRows(at: [indexPath], with: .automatic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:10:22 +00:00
|
|
|
extension NetworkSettingsViewController: ToggleTableViewCellDelegate {
|
|
|
|
func toggleCell(_ cell: ToggleTableViewCell, didToggleToValue value: Bool) {
|
|
|
|
guard let item = RowType(rawValue: cell.tag) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handle(row: item, cell: cell)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +00:00
|
|
|
extension NetworkSettingsViewController: FieldTableViewCellDelegate {
|
|
|
|
func fieldCellDidEdit(_ cell: FieldTableViewCell) {
|
|
|
|
commitTextField(cell.field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldCellDidEnter(_: FieldTableViewCell) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NetworkChoice: CustomStringConvertible {
|
|
|
|
public var description: String {
|
|
|
|
switch self {
|
|
|
|
case .client:
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.NetworkChoice.client
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
case .server:
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.NetworkChoice.server
|
2019-04-29 16:13:49 +00:00
|
|
|
|
|
|
|
case .manual:
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.Global.Values.manual
|
2019-04-29 16:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|