-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathVPNSettingsViewController.swift
209 lines (178 loc) · 7.16 KB
/
VPNSettingsViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// VPNSettingsViewController.swift
// MullvadVPN
//
// Created by pronebird on 19/05/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
protocol VPNSettingsViewControllerDelegate: AnyObject {
func showIPOverrides()
}
class VPNSettingsViewController: UITableViewController {
private let interactor: VPNSettingsInteractor
private var dataSource: VPNSettingsDataSource?
private let alertPresenter: AlertPresenter
weak var delegate: VPNSettingsViewControllerDelegate?
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
init(interactor: VPNSettingsInteractor, alertPresenter: AlertPresenter) {
self.interactor = interactor
self.alertPresenter = alertPresenter
super.init(style: .grouped)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.accessibilityIdentifier = .vpnSettingsTableView
tableView.backgroundColor = .secondaryColor
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 60
tableView.estimatedSectionHeaderHeight = tableView.estimatedRowHeight
tableView.allowsMultipleSelection = true
dataSource = VPNSettingsDataSource(tableView: tableView)
dataSource?.delegate = self
navigationItem.title = NSLocalizedString(
"NAVIGATION_TITLE",
tableName: "VPNSettings",
value: "VPN settings",
comment: ""
)
interactor.tunnelSettingsDidChange = { [weak self] newSettings in
self?.dataSource?.update(from: newSettings)
}
dataSource?.update(from: interactor.tunnelSettings)
dataSource?.setAvailablePortRanges(interactor.cachedRelays?.relays.wireguard.portRanges ?? [])
interactor.cachedRelaysDidChange = { [weak self] cachedRelays in
self?.dataSource?.setAvailablePortRanges(cachedRelays.relays.wireguard.portRanges)
}
tableView.tableHeaderView = UIView(frame: CGRect(
origin: .zero,
size: CGSize(width: 0, height: UIMetrics.TableView.sectionSpacing)
))
}
private func showInfo(with message: String) {
let presentation = AlertPresentation(
id: "vpn-settings-content-blockers-alert",
icon: .info,
message: message,
buttons: [
AlertAction(
title: NSLocalizedString(
"VPN_SETTINGS_VPN_SETTINGS_OK_ACTION",
tableName: "ContentBlockers",
value: "Got it!",
comment: ""
),
style: .default
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func humanReadablePortRepresentation(_ ranges: [[UInt16]]) -> String {
ranges
.compactMap { range in
if let minPort = range.first, let maxPort = range.last {
return minPort == maxPort ? String(minPort) : "\(minPort)-\(maxPort)"
} else {
return nil
}
}
.joined(separator: ", ")
}
}
extension VPNSettingsViewController: VPNSettingsDataSourceDelegate {
func didChangeViewModel(_ viewModel: VPNSettingsViewModel) {
interactor.updateSettings(
[
.obfuscation(WireGuardObfuscationSettings(
state: viewModel.obfuscationState,
port: viewModel.obfuscationPort
)),
.quantumResistance(viewModel.quantumResistance),
.multihop(viewModel.multihopState),
]
)
}
// swiftlint:disable:next function_body_length
func showInfo(for item: VPNSettingsInfoButtonItem) {
var message = ""
switch item {
case .wireGuardPorts:
let portsString = humanReadablePortRepresentation(
interactor.cachedRelays?.relays.wireguard.portRanges ?? []
)
message = String(
format: NSLocalizedString(
"VPN_SETTINGS_WIRE_GUARD_PORTS_GENERAL",
tableName: "WireGuardPorts",
value: """
The automatic setting will randomly choose from the valid port ranges shown below.
The custom port can be any value inside the valid ranges:
%@
""",
comment: ""
),
portsString
)
case .wireGuardObfuscation:
message = NSLocalizedString(
"VPN_SETTINGS_WIRE_GUARD_OBFUSCATION_GENERAL",
tableName: "WireGuardObfuscation",
value: """
Obfuscation hides the WireGuard traffic inside another protocol. \
It can be used to help circumvent censorship and other types of filtering, \
where a plain WireGuard connect would be blocked.
""",
comment: ""
)
case .wireGuardObfuscationPort:
message = NSLocalizedString(
"VPN_SETTINGS_WIRE_GUARD_OBFUSCATION_PORT_GENERAL",
tableName: "WireGuardObfuscation",
value: "Which TCP port the UDP-over-TCP obfuscation protocol should connect to on the VPN server.",
comment: ""
)
case .quantumResistance:
message = NSLocalizedString(
"VPN_SETTINGS_QUANTUM_RESISTANCE_GENERAL",
tableName: "QuantumResistance",
value: """
This feature makes the WireGuard tunnel resistant to potential attacks from quantum computers.
It does this by performing an extra key exchange using a quantum safe algorithm and mixing \
the result into WireGuard’s regular encryption.
This extra step uses approximately 500 kiB of traffic every time a new tunnel is established.
""",
comment: ""
)
case .multihop:
message = NSLocalizedString(
"MULTIHOP_INFORMATION_TEXT",
tableName: "Multihop",
value: """
Multihop routes your traffic into one WireGuard server and out another, making it harder to trace.
This results in increased latency but increases anonymity online.
""",
comment: ""
)
default:
assertionFailure("No matching InfoButtonItem")
}
showInfo(with: message)
}
func showDNSSettings() {
let viewController = CustomDNSViewController(interactor: interactor, alertPresenter: alertPresenter)
navigationController?.pushViewController(viewController, animated: true)
}
func showIPOverrides() {
delegate?.showIPOverrides()
}
func didSelectWireGuardPort(_ port: UInt16?) {
interactor.setPort(port)
}
}