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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
//
// ConnectViewController.swift
// MullvadVPN
//
// Created by pronebird on 20/03/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import UIKit
import NetworkExtension
import Logging
class ConnectViewController: UIViewController, RootContainment, TunnelObserver
{
private lazy var mainContentView: ConnectMainContentView = {
let view = ConnectMainContentView(frame: UIScreen.main.bounds)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private var relayConstraints: RelayConstraints?
private let logger = Logger(label: "ConnectViewController")
private let alertPresenter = AlertPresenter()
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
var preferredHeaderBarStyle: HeaderBarStyle {
switch tunnelState {
case .connecting, .reconnecting, .connected:
return .secured
case .disconnecting, .disconnected:
return .unsecured
}
}
var prefersHeaderBarHidden: Bool {
return false
}
private var tunnelState: TunnelState = .disconnected {
didSet {
setNeedsHeaderBarStyleAppearanceUpdate()
updateTunnelConnectionInfo()
updateUserInterfaceForTunnelStateChange()
}
}
private var showedAccountView = false
override func viewDidLoad() {
super.viewDidLoad()
mainContentView.connectionPanel.collapseButton.addTarget(self, action: #selector(handleConnectionPanelButton(_:)), for: .touchUpInside)
mainContentView.connectButton.addTarget(self, action: #selector(handleConnect(_:)), for: .touchUpInside)
mainContentView.splitDisconnectButton.primaryButton.addTarget(self, action: #selector(handleDisconnect(_:)), for: .touchUpInside)
mainContentView.splitDisconnectButton.secondaryButton.addTarget(self, action: #selector(handleReconnect(_:)), for: .touchUpInside)
mainContentView.selectLocationButton.addTarget(self, action: #selector(handleSelectLocation(_:)), for: .touchUpInside)
view.addSubview(mainContentView)
NSLayoutConstraint.activate([
mainContentView.topAnchor.constraint(equalTo: view.topAnchor),
mainContentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mainContentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mainContentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
TunnelManager.shared.addObserver(self)
self.tunnelState = TunnelManager.shared.tunnelState
fetchRelayConstraints()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
showAccountViewForExpiredAccount()
}
// MARK: - TunnelObserver
func tunnelStateDidChange(tunnelState: TunnelState) {
DispatchQueue.main.async {
self.tunnelState = tunnelState
}
}
func tunnelPublicKeyDidChange(publicKeyWithMetadata: PublicKeyWithMetadata?) {
// no-op
}
// MARK: - Private
private func updateUserInterfaceForTunnelStateChange() {
mainContentView.secureLabel.text = tunnelState.localizedTitleForSecureLabel.uppercased()
mainContentView.secureLabel.textColor = tunnelState.textColorForSecureLabel
mainContentView.connectButton.setTitle(tunnelState.localizedTitleForConnectButton, for: .normal)
mainContentView.selectLocationButton.setTitle(tunnelState.localizedTitleForSelectLocationButton, for: .normal)
mainContentView.splitDisconnectButton.primaryButton.setTitle(tunnelState.localizedTitleForDisconnectButton, for: .normal)
mainContentView.setActionButtons(tunnelState.actionButtons)
}
private func attributedStringForLocation(string: String) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 0
paragraphStyle.lineHeightMultiple = 0.80
return NSAttributedString(string: string, attributes: [
.paragraphStyle: paragraphStyle])
}
private func updateTunnelConnectionInfo() {
switch tunnelState {
case .connected(let connectionInfo),
.reconnecting(let connectionInfo):
mainContentView.cityLabel.attributedText = attributedStringForLocation(string: connectionInfo.location.city)
mainContentView.countryLabel.attributedText = attributedStringForLocation(string: connectionInfo.location.country)
mainContentView.connectionPanel.dataSource = ConnectionPanelData(
inAddress: "\(connectionInfo.ipv4Relay) UDP",
outAddress: nil
)
mainContentView.connectionPanel.isHidden = false
mainContentView.connectionPanel.collapseButton.setTitle(connectionInfo.hostname, for: .normal)
case .connecting, .disconnected, .disconnecting:
mainContentView.cityLabel.attributedText = attributedStringForLocation(string: " ")
mainContentView.countryLabel.attributedText = attributedStringForLocation(string: " ")
mainContentView.connectionPanel.dataSource = nil
mainContentView.connectionPanel.isHidden = true
}
}
private func connectTunnel() {
TunnelManager.shared.startTunnel { (result) in
DispatchQueue.main.async {
switch result {
case .success:
break
case .failure(let error):
self.logger.error(chainedError: error, message: "Failed to start the VPN tunnel")
let alertController = UIAlertController(
title: NSLocalizedString("Failed to start the VPN tunnel", comment: ""),
message: error.errorChainDescription,
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .cancel)
)
self.alertPresenter.enqueue(alertController, presentingController: self)
}
}
}
}
private func disconnectTunnel() {
TunnelManager.shared.stopTunnel { (result) in
if case .failure(let error) = result {
self.logger.error(chainedError: error, message: "Failed to stop the VPN tunnel")
let alertController = UIAlertController(
title: NSLocalizedString("Failed to stop the VPN tunnel", comment: ""),
message: error.errorChainDescription,
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .cancel)
)
self.alertPresenter.enqueue(alertController, presentingController: self)
}
}
}
private func reconnectTunnel() {
TunnelManager.shared.reconnectTunnel(completionHandler: nil)
}
private func showAccountViewForExpiredAccount() {
guard !showedAccountView else { return }
showedAccountView = true
if let accountExpiry = Account.shared.expiry, AccountExpiry(date: accountExpiry).isExpired {
rootContainerController?.showSettings(navigateTo: .account, animated: true)
}
}
private func showSelectLocationModal() {
let contentController = SelectLocationViewController()
contentController.navigationItem.title = NSLocalizedString("Select location", comment: "Navigation title")
contentController.navigationItem.largeTitleDisplayMode = .never
contentController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDismissSelectLocationController(_:)))
contentController.didSelectRelayLocation = { [weak self] (controller, relayLocation) in
controller.view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250)) {
controller.view.isUserInteractionEnabled = true
controller.dismiss(animated: true) {
self?.selectLocationControllerDidSelectRelayLocation(relayLocation)
}
}
}
let navController = SelectLocationNavigationController(contentController: contentController)
view.isUserInteractionEnabled = false
contentController.setSelectedRelayLocation(self.relayConstraints?.location.value, animated: false, scrollPosition: .none)
contentController.prefetchData { (error) in
if let error = error {
self.logger.error(chainedError: error, message: "Failed to prefetch the relays for SelectLocationViewController")
}
self.present(navController, animated: true) {
self.view.isUserInteractionEnabled = true
}
}
}
private func fetchRelayConstraints() {
TunnelManager.shared.getRelayConstraints { (result) in
DispatchQueue.main.async {
switch result {
case .success(let relayConstraints):
self.relayConstraints = relayConstraints
case .failure(let error):
self.logger.error(chainedError: error)
}
}
}
}
private func selectLocationControllerDidSelectRelayLocation(_ relayLocation: RelayLocation) {
let relayConstraints = makeRelayConstraints(relayLocation)
self.setTunnelRelayConstraints(relayConstraints)
self.relayConstraints = relayConstraints
}
private func makeRelayConstraints(_ location: RelayLocation) -> RelayConstraints {
return RelayConstraints(location: .only(location))
}
private func setTunnelRelayConstraints(_ relayConstraints: RelayConstraints) {
TunnelManager.shared.setRelayConstraints(relayConstraints) { [weak self] (result) in
guard let self = self else { return }
DispatchQueue.main.async {
switch result {
case .success:
self.logger.debug("Updated relay constraints: \(relayConstraints)")
self.connectTunnel()
case .failure(let error):
self.logger.error(chainedError: error, message: "Failed to update relay constraints")
}
}
}
}
// MARK: - Actions
@objc func handleConnectionPanelButton(_ sender: Any) {
mainContentView.connectionPanel.toggleConnectionInfoVisibility()
}
@objc func handleConnect(_ sender: Any) {
connectTunnel()
}
@objc func handleDisconnect(_ sender: Any) {
disconnectTunnel()
}
@objc func handleReconnect(_ sender: Any) {
reconnectTunnel()
}
@objc func handleSelectLocation(_ sender: Any) {
showSelectLocationModal()
}
@objc func handleDismissSelectLocationController(_ sender: Any) {
self.presentedViewController?.dismiss(animated: true)
}
}
private extension TunnelState {
var textColorForSecureLabel: UIColor {
switch self {
case .connecting, .reconnecting:
return .white
case .connected:
return .successColor
case .disconnecting, .disconnected:
return .dangerColor
}
}
var localizedTitleForSecureLabel: String {
switch self {
case .connecting, .reconnecting:
return NSLocalizedString("Creating secure connection", comment: "")
case .connected:
return NSLocalizedString("Secure connection", comment: "")
case .disconnecting, .disconnected:
return NSLocalizedString("Unsecured connection", comment: "")
}
}
var localizedTitleForSelectLocationButton: String? {
switch self {
case .disconnected, .disconnecting:
return NSLocalizedString("Select location", comment: "")
case .connecting, .connected, .reconnecting:
return NSLocalizedString("Switch location", comment: "")
}
}
var localizedTitleForConnectButton: String? {
return NSLocalizedString("Secure connection", comment: "")
}
var localizedTitleForDisconnectButton: String? {
switch self {
case .connecting:
return NSLocalizedString("Cancel", comment: "")
case .connected, .reconnecting:
return NSLocalizedString("Disconnect", comment: "")
case .disconnecting, .disconnected:
return nil
}
}
var actionButtons: [ConnectMainContentView.ActionButton] {
switch self {
case .disconnected, .disconnecting:
return [.selectLocation, .connect]
case .connecting, .connected, .reconnecting:
return [.selectLocation, .disconnect]
}
}
}
|