2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// DNSViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/21/19.
|
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 Cocoa
|
|
|
|
import PassepartoutCore
|
2021-01-22 10:03:55 +00:00
|
|
|
import TunnelKit
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
class DNSViewController: NSViewController, ProfileCustomization {
|
|
|
|
@IBOutlet private weak var popupChoice: NSPopUpButton!
|
|
|
|
|
2021-01-26 18:43:30 +00:00
|
|
|
@IBOutlet private weak var viewSettings: NSStackView!
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-01-22 10:03:55 +00:00
|
|
|
@IBOutlet private weak var textDNSCustom: NSTextField!
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@IBOutlet private weak var viewDNSAddresses: NSView!
|
|
|
|
|
2021-01-22 10:12:07 +00:00
|
|
|
@IBOutlet private weak var viewDNSDomains: NSView!
|
|
|
|
|
2021-01-22 10:03:55 +00:00
|
|
|
@IBOutlet private weak var labelDNSProtocol: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var popupDNSProtocol: NSPopUpButton!
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@IBOutlet private var constraintChoiceBottom: NSLayoutConstraint!
|
|
|
|
|
|
|
|
@IBOutlet private var constraintSettingsTop: NSLayoutConstraint!
|
|
|
|
|
|
|
|
private lazy var tableDNSDomains: TextTableView = .get()
|
|
|
|
|
|
|
|
private lazy var tableDNSAddresses: TextTableView = .get()
|
|
|
|
|
|
|
|
private lazy var currentChoice = profile?.networkChoices?.dns ?? ProfileNetworkChoices.with(profile: profile).dns
|
|
|
|
|
|
|
|
private lazy var clientNetworkSettings = profile?.clientNetworkSettings
|
|
|
|
|
|
|
|
private let networkSettings = ProfileNetworkSettings()
|
|
|
|
|
|
|
|
// MARK: ProfileCustomization
|
|
|
|
|
|
|
|
var profile: ConnectionProfile?
|
|
|
|
|
|
|
|
weak var delegate: ProfileCustomizationDelegate?
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-01-22 10:03:55 +00:00
|
|
|
labelDNSProtocol.stringValue = L10n.Core.Global.Captions.protocol.asCaption
|
|
|
|
|
2021-08-07 10:23:17 +00:00
|
|
|
tableDNSAddresses.title = L10n.Core.NetworkSettings.Dns.Cells.Addresses.title.asCaption
|
2021-01-01 10:13:41 +00:00
|
|
|
viewDNSAddresses.addSubview(tableDNSAddresses)
|
|
|
|
tableDNSAddresses.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tableDNSAddresses.topAnchor.constraint(equalTo: viewDNSAddresses.topAnchor),
|
|
|
|
tableDNSAddresses.bottomAnchor.constraint(equalTo: viewDNSAddresses.bottomAnchor),
|
|
|
|
tableDNSAddresses.leftAnchor.constraint(equalTo: viewDNSAddresses.leftAnchor),
|
|
|
|
tableDNSAddresses.rightAnchor.constraint(equalTo: viewDNSAddresses.rightAnchor),
|
|
|
|
])
|
|
|
|
|
2021-08-07 10:23:17 +00:00
|
|
|
tableDNSDomains.title = L10n.Core.NetworkSettings.Dns.Cells.Domains.title.asCaption
|
2021-01-22 10:12:07 +00:00
|
|
|
viewDNSDomains.addSubview(tableDNSDomains)
|
|
|
|
tableDNSDomains.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tableDNSDomains.topAnchor.constraint(equalTo: viewDNSDomains.topAnchor),
|
|
|
|
tableDNSDomains.bottomAnchor.constraint(equalTo: viewDNSDomains.bottomAnchor),
|
|
|
|
tableDNSDomains.leftAnchor.constraint(equalTo: viewDNSDomains.leftAnchor),
|
|
|
|
tableDNSDomains.rightAnchor.constraint(equalTo: viewDNSDomains.rightAnchor),
|
|
|
|
])
|
|
|
|
|
2021-01-22 16:49:31 +00:00
|
|
|
loadSettings(from: currentChoice)
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
popupChoice.removeAllItems()
|
2021-01-22 10:03:55 +00:00
|
|
|
popupDNSProtocol.removeAllItems()
|
|
|
|
let menuChoice = NSMenu()
|
|
|
|
var indexOfChoice = 0
|
|
|
|
for (i, choice) in NetworkChoice.choices(for: profile).enumerated() {
|
|
|
|
let item = NSMenuItem(title: choice.description, action: nil, keyEquivalent: "")
|
|
|
|
item.representedObject = choice
|
|
|
|
menuChoice.addItem(item)
|
2021-01-01 10:13:41 +00:00
|
|
|
if choice == currentChoice {
|
2021-01-22 10:03:55 +00:00
|
|
|
indexOfChoice = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
popupChoice.menu = menuChoice
|
|
|
|
tableDNSAddresses.rowTemplate = AppConstants.Placeholders.dnsAddress
|
|
|
|
tableDNSDomains.rowTemplate = AppConstants.Placeholders.dnsDomain
|
|
|
|
let menuProtocol = NSMenu()
|
|
|
|
var availableProtocols: [DNSProtocol] = [.plain]
|
|
|
|
if #available(iOS 14, macOS 11, *) {
|
|
|
|
availableProtocols.append(.https)
|
|
|
|
availableProtocols.append(.tls)
|
|
|
|
}
|
|
|
|
var indexOfDNSProtocol = 0
|
|
|
|
for (i, proto) in availableProtocols.enumerated() {
|
|
|
|
let item = NSMenuItem(title: proto.description, action: nil, keyEquivalent: "")
|
|
|
|
item.representedObject = proto
|
|
|
|
menuProtocol.addItem(item)
|
|
|
|
if proto == networkSettings.dnsProtocol {
|
|
|
|
indexOfDNSProtocol = i
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 10:03:55 +00:00
|
|
|
popupChoice.menu = menuChoice
|
|
|
|
popupChoice.selectItem(at: indexOfChoice)
|
|
|
|
popupDNSProtocol.menu = menuProtocol
|
|
|
|
popupDNSProtocol.selectItem(at: indexOfDNSProtocol)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction private func pickChoice(_ sender: Any?) {
|
2021-01-22 10:03:55 +00:00
|
|
|
guard let choice = popupChoice.selectedItem?.representedObject as? NetworkChoice else {
|
|
|
|
return
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
loadSettings(from: choice)
|
|
|
|
|
|
|
|
delegate?.profileCustomization(self, didUpdateDNS: choice, withManualSettings: networkSettings)
|
|
|
|
}
|
|
|
|
|
2021-01-22 10:03:55 +00:00
|
|
|
@IBAction private func pickProtocol(_ sender: Any?) {
|
|
|
|
guard let choice = popupChoice.selectedItem?.representedObject as? NetworkChoice else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let proto = popupDNSProtocol.selectedItem?.representedObject as? DNSProtocol else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
networkSettings.dnsProtocol = proto
|
|
|
|
updateProtocolVisibility()
|
|
|
|
|
|
|
|
delegate?.profileCustomization(self, didUpdateDNS: choice, withManualSettings: networkSettings)
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
func commitManualSettings() {
|
|
|
|
guard currentChoice == .manual else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
view.endEditing()
|
2021-01-22 10:03:55 +00:00
|
|
|
switch networkSettings.dnsProtocol {
|
|
|
|
case .https:
|
|
|
|
networkSettings.dnsHTTPSURL = URL(string: textDNSCustom.stringValue)
|
|
|
|
|
|
|
|
case .tls:
|
|
|
|
networkSettings.dnsTLSServerName = textDNSCustom.stringValue
|
|
|
|
|
|
|
|
default:
|
2021-02-11 16:39:07 +00:00
|
|
|
break
|
2021-01-22 10:03:55 +00:00
|
|
|
}
|
2021-02-11 16:39:07 +00:00
|
|
|
networkSettings.dnsServers = tableDNSAddresses.rows
|
2021-01-22 10:12:07 +00:00
|
|
|
networkSettings.dnsSearchDomains = tableDNSDomains.rows
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
delegate?.profileCustomization(self, didUpdateDNS: .manual, withManualSettings: networkSettings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func loadSettings(from choice: NetworkChoice) {
|
|
|
|
currentChoice = choice
|
|
|
|
switch currentChoice {
|
|
|
|
case .client:
|
|
|
|
if let settings = clientNetworkSettings {
|
|
|
|
networkSettings.copyDNS(from: settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .server:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .manual:
|
|
|
|
if let settings = profile?.manualNetworkSettings {
|
|
|
|
networkSettings.copyDNS(from: settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:19:45 +00:00
|
|
|
let isManual = (currentChoice == .manual)
|
|
|
|
popupDNSProtocol.isEnabled = isManual
|
|
|
|
textDNSCustom.isEnabled = isManual
|
|
|
|
tableDNSAddresses.reset(withRows: networkSettings.dnsServers ?? [], isAddEnabled: isManual)
|
|
|
|
tableDNSDomains.reset(withRows: networkSettings.dnsSearchDomains ?? [], isAddEnabled: isManual)
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
let isServer = (currentChoice == .server)
|
|
|
|
constraintChoiceBottom.priority = isServer ? .defaultHigh : .defaultLow
|
|
|
|
constraintSettingsTop.priority = isServer ? .defaultLow : .defaultHigh
|
|
|
|
viewSettings.isHidden = isServer
|
2021-01-22 10:03:55 +00:00
|
|
|
|
|
|
|
updateProtocolVisibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateProtocolVisibility() {
|
2021-01-22 19:19:45 +00:00
|
|
|
let isManual = (currentChoice == .manual)
|
2021-01-22 10:03:55 +00:00
|
|
|
switch networkSettings.dnsProtocol {
|
|
|
|
case .https:
|
2021-01-22 19:19:45 +00:00
|
|
|
textDNSCustom.placeholderString = isManual ? AppConstants.Placeholders.dohURL : ""
|
2021-01-22 10:03:55 +00:00
|
|
|
textDNSCustom.stringValue = networkSettings.dnsHTTPSURL?.absoluteString ?? ""
|
2021-01-26 09:25:04 +00:00
|
|
|
textDNSCustom.isHidden = false
|
|
|
|
|
2021-01-22 10:03:55 +00:00
|
|
|
case .tls:
|
2021-01-22 19:19:45 +00:00
|
|
|
textDNSCustom.placeholderString = isManual ? AppConstants.Placeholders.dotServerName : ""
|
2021-01-22 10:03:55 +00:00
|
|
|
textDNSCustom.stringValue = networkSettings.dnsTLSServerName ?? ""
|
2021-01-26 09:25:04 +00:00
|
|
|
textDNSCustom.isHidden = false
|
2021-01-22 10:03:55 +00:00
|
|
|
|
|
|
|
default:
|
2021-01-26 09:25:04 +00:00
|
|
|
textDNSCustom.isHidden = true
|
2021-01-22 10:03:55 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|