aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/ViewController
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-02-16 18:25:17 +0530
committerRoopesh Chander <roop@roopc.net>2019-02-16 18:25:17 +0530
commit5031a7db4c70d6f71ea568839b67c39ab68b4bdb (patch)
treeb46c45118752673224d873e3946879e01726cd54 /WireGuard/WireGuard/UI/macOS/ViewController
parentTunnelViewModel: Minor refactoring of exclude private IPs handling (diff)
downloadwireguard-apple-5031a7db4c70d6f71ea568839b67c39ab68b4bdb.tar.xz
wireguard-apple-5031a7db4c70d6f71ea568839b67c39ab68b4bdb.zip
macOS: Exclude private IPs
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/ViewController')
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift41
1 files changed, 41 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
index efb3fd74..772ee7f9 100644
--- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
+++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
@@ -56,6 +56,14 @@ class TunnelEditViewController: NSViewController {
return scrollView
}()
+ let excludePrivateIPsCheckbox: NSButton = {
+ let checkbox = NSButton()
+ checkbox.title = tr("tunnelPeerExcludePrivateIPs")
+ checkbox.setButtonType(.switch)
+ checkbox.state = .off
+ return checkbox
+ }()
+
let discardButton: NSButton = {
let button = NSButton()
button.title = tr("macEditDiscard")
@@ -86,6 +94,7 @@ class TunnelEditViewController: NSViewController {
var privateKeyObservationToken: AnyObject?
var hasErrorObservationToken: AnyObject?
+ var singlePeerAllowedIPsObservationToken: AnyObject?
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) {
self.tunnelsManager = tunnelsManager
@@ -111,6 +120,8 @@ class TunnelEditViewController: NSViewController {
} else {
selectedActivateOnDemandOption = .none
}
+ let singlePeer = tunnelConfiguration.peers.count == 1 ? tunnelConfiguration.peers.first : nil
+ updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: singlePeer?.allowedIPs.map { $0.stringRepresentation })
} else {
// Creating a new tunnel
let privateKey = Curve25519.generatePrivateKey()
@@ -133,6 +144,9 @@ class TunnelEditViewController: NSViewController {
hasErrorObservationToken = textView.observe(\.hasError) { [weak saveButton] textView, _ in
saveButton?.isEnabled = !textView.hasError
}
+ singlePeerAllowedIPsObservationToken = textView.observe(\.singlePeerAllowedIPs) { [weak self] textView, _ in
+ self?.updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: textView.singlePeerAllowedIPs)
+ }
onDemandRow.valueOptions = activateOnDemandOptions.map { TunnelViewModel.activateOnDemandOptionText(for: $0) }
onDemandRow.selectedOptionIndex = activateOnDemandOptions.firstIndex(of: selectedActivateOnDemandOption)!
@@ -149,6 +163,9 @@ class TunnelEditViewController: NSViewController {
discardButton.target = self
discardButton.action = #selector(handleDiscardAction)
+ excludePrivateIPsCheckbox.target = self
+ excludePrivateIPsCheckbox.action = #selector(excludePrivateIPsCheckboxToggled(sender:))
+
let margin: CGFloat = 20
let internalSpacing: CGFloat = 10
@@ -159,6 +176,7 @@ class TunnelEditViewController: NSViewController {
let buttonRowStackView = NSStackView()
buttonRowStackView.setViews([discardButton, saveButton], in: .trailing)
+ buttonRowStackView.addView(excludePrivateIPsCheckbox, in: .leading)
buttonRowStackView.orientation = .horizontal
buttonRowStackView.spacing = internalSpacing
@@ -237,4 +255,27 @@ class TunnelEditViewController: NSViewController {
delegate?.tunnelEditingCancelled()
dismiss(self)
}
+
+ func updateExcludePrivateIPsVisibility(singlePeerAllowedIPs: [String]?) {
+ let shouldAllowExcludePrivateIPsControl: Bool
+ let excludePrivateIPsValue: Bool
+ if let singlePeerAllowedIPs = singlePeerAllowedIPs {
+ (shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: true, allowedIPs: Set<String>(singlePeerAllowedIPs))
+ } else {
+ (shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: false, allowedIPs: Set<String>())
+ }
+ excludePrivateIPsCheckbox.isHidden = !shouldAllowExcludePrivateIPsControl
+ excludePrivateIPsCheckbox.state = excludePrivateIPsValue ? .on : .off
+ }
+
+ @objc func excludePrivateIPsCheckboxToggled(sender: AnyObject?) {
+ guard let excludePrivateIPsCheckbox = sender as? NSButton else { return }
+ guard let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value) else { return }
+ let isOn = excludePrivateIPsCheckbox.state == .on
+ let tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration)
+ tunnelViewModel.peersData.first?.excludePrivateIPsValueChanged(isOn: isOn, dnsServers: tunnelViewModel.interfaceData[.dns])
+ if let modifiedConfig = tunnelViewModel.asWgQuickConfig() {
+ textView.setConfText(modifiedConfig)
+ }
+ }
}