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
|
//
// ConnectViewController.swift
// MullvadVPN
//
// Created by pronebird on 20/03/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import Combine
import UIKit
import NetworkExtension
import os
class ConnectViewController: UIViewController, RootContainment, TunnelControlViewControllerDelegate {
@IBOutlet var secureLabel: UILabel!
@IBOutlet var countryLabel: UILabel!
private var setRelaysSubscriber: AnyCancellable?
private var startStopTunnelSubscriber: AnyCancellable?
private var tunnelStateSubscriber: AnyCancellable?
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
var preferredHeaderBarStyle: HeaderBarStyle {
switch tunnelState {
case .connecting, .reconnecting, .connected:
return .secured
case .disconnecting, .disconnected:
return .unsecured
}
}
private var tunnelState: TunnelState = .disconnected {
didSet {
setNeedsHeaderBarStyleAppearanceUpdate()
updateSecureLabel()
updateTunnelConnectionInfo()
}
}
override func viewDidLoad() {
super.viewDidLoad()
tunnelStateSubscriber = TunnelManager.shared.$tunnelState
.receive(on: DispatchQueue.main)
.assign(to: \.tunnelState, on: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if case .embedTunnelControls = SegueIdentifier.Connect.from(segue: segue) {
let tunnelControlController = segue.destination as! TunnelControlViewController
tunnelControlController.view.translatesAutoresizingMaskIntoConstraints = false
tunnelControlController.delegate = self
}
}
// MARK: - TunnelControlViewControllerDelegate
func tunnelControlViewController(_ controller: TunnelControlViewController, handleAction action: TunnelControlAction) {
switch action {
case .connect:
connectTunnel()
case .disconnect:
disconnectTunnel()
case .selectLocation:
performSegue(withIdentifier: SegueIdentifier.Connect.showRelaySelector.rawValue, sender: self)
}
}
// MARK: - Private
private func updateSecureLabel() {
secureLabel.text = tunnelState.textForSecureLabel()
secureLabel.textColor = tunnelState.textColorForSecureLabel()
}
private func updateTunnelConnectionInfo() {
switch tunnelState {
case .connected(let connectionInfo),
.reconnecting(let connectionInfo):
countryLabel.text = "\(connectionInfo.hostname)\nIn: \(connectionInfo.ipv4Relay)"
case .connecting, .disconnected, .disconnecting:
countryLabel.text = ""
}
}
private func connectTunnel() {
startStopTunnelSubscriber = TunnelManager.shared.startTunnel()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (completion) in
if case .failure(let error) = completion {
os_log(.error, "Failed to start the tunnel: %{public}s",
error.localizedDescription)
self.presentError(error, preferredStyle: .alert)
}
})
}
private func disconnectTunnel() {
startStopTunnelSubscriber = TunnelManager.shared.stopTunnel()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (completion) in
// no-op
})
}
// MARK: - Actions
@IBAction func unwindFromSelectLocation(segue: UIStoryboardSegue) {
guard let selectLocationController = segue.source as? SelectLocationController else { return }
guard let selectedLocation = selectLocationController.selectedLocation else { return }
let relayConstraints = RelayConstraints(location: .only(selectedLocation))
setRelaysSubscriber = TunnelManager.shared.setRelayConstraints(relayConstraints)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (completion) in
switch completion {
case .finished:
os_log(.debug, "Updated relay constraints: %{public}s", String(reflecting: relayConstraints))
self.connectTunnel()
case .failure(let error):
os_log(.error, "Failed to update relay constraints: %{public}s", error.localizedDescription)
}
})
}
}
private extension TunnelState {
func textColorForSecureLabel() -> UIColor {
switch self {
case .connecting, .reconnecting:
return .white
case .connected:
return .successColor
case .disconnecting, .disconnected:
return .dangerColor
}
}
func textForSecureLabel() -> 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: "")
}
}
}
|