summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/WireguardKeysViewController.swift
blob: 30e9957bc8e6f6dbb73ee688ba7628473e736b77 (plain)
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
//
//  WireguardKeysViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 04/12/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit
import Logging

/// A UI refresh interval for the public key creation date (in seconds)
private let kCreationDateRefreshInterval = Int(60)

/// A maximum number of characters to display out of the entire public key representation
private let kDisplayPublicKeyMaxLength = 20

private enum WireguardKeysViewState {
    case `default`
    case verifyingKey
    case verifiedKey(Bool)
    case regeneratingKey
    case regeneratedKey(Bool)
}

class WireguardKeysViewController: UIViewController, TunnelObserver {

    private let contentView: WireguardKeysContentView = {
        let contentView = WireguardKeysContentView()
        contentView.translatesAutoresizingMaskIntoConstraints = false
        return contentView
    }()

    private var publicKeyPeriodicUpdateTimer: DispatchSourceTimer?
    private var copyToPasteboardCancellationToken: PromiseCancellationToken?
    private var verifyKeyCancellationToken: PromiseCancellationToken?

    private let alertPresenter = AlertPresenter()
    private let logger = Logger(label: "WireguardKeys")

    private var state: WireguardKeysViewState = .default {
        didSet {
            updateViewState(state)
        }
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .secondaryColor

        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(contentView)
        view.addSubview(scrollView)

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            contentView.bottomAnchor.constraint(greaterThanOrEqualTo: scrollView.safeAreaLayoutGuide.bottomAnchor),
            contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
        ])

        navigationItem.title = NSLocalizedString("NAVIGATION_TITLE", tableName: "WireguardKeys", comment: "")

        contentView.publicKeyRowView.actionHandler = { [weak self] in
            self?.copyPublicKey()
        }

        contentView.regenerateKeyButton.addTarget(self, action: #selector(handleRegenerateKey(_:)), for: .touchUpInside)
        contentView.verifyKeyButton.addTarget(self, action: #selector(handleVerifyKey(_:)), for: .touchUpInside)

        TunnelManager.shared.addObserver(self)
        updatePublicKey(tunnelSettings: TunnelManager.shared.tunnelInfo?.tunnelSettings, animated: false)

        startPublicKeyPeriodicUpdate()
    }

    private func startPublicKeyPeriodicUpdate() {
        let interval = DispatchTimeInterval.seconds(kCreationDateRefreshInterval)
        let timerSource = DispatchSource.makeTimerSource(queue: .main)
        timerSource.setEventHandler { [weak self] () -> Void in
            self?.updatePublicKey(tunnelSettings: TunnelManager.shared.tunnelInfo?.tunnelSettings, animated: true)
        }
        timerSource.schedule(deadline: .now() + interval, repeating: interval)
        timerSource.activate()

        self.publicKeyPeriodicUpdateTimer = timerSource
    }

    // MARK: - TunnelObserver

    func tunnelManager(_ manager: TunnelManager, didUpdateTunnelState tunnelState: TunnelState) {
        // no-op
    }

    func tunnelManager(_ manager: TunnelManager, didUpdateTunnelSettings tunnelInfo: TunnelInfo?) {
        self.updatePublicKey(tunnelSettings: tunnelInfo?.tunnelSettings, animated: true)
    }

    func tunnelManager(_ manager: TunnelManager, didFailWithError error: TunnelManager.Error) {
        // no-op
    }

    // MARK: - Actions

    private func copyPublicKey() {
        guard let tunnelInfo = TunnelManager.shared.tunnelInfo else { return }

        let metadata = tunnelInfo.tunnelSettings.interface.privateKey.publicKeyWithMetadata

        UIPasteboard.general.string = metadata.stringRepresentation()

        setPublicKeyTitle(
            string: NSLocalizedString("COPIED_TO_PASTEBOARD_LABEL", tableName: "WireguardKeys", comment: ""),
            animated: true)

        Promise.deferred { TunnelManager.shared.tunnelInfo?.tunnelSettings }
            .delay(by: .seconds(3), timerType: .walltime, queue: .main)
            .storeCancellationToken(in: &copyToPasteboardCancellationToken)
            .observe { [weak self] completion in
                guard let tunnelSettings = completion.unwrappedValue else { return }

                self?.updatePublicKey(tunnelSettings: tunnelSettings, animated: true)
            }
    }

    @objc private func handleRegenerateKey(_ sender: Any) {
        regeneratePrivateKey()
    }

    @objc private func handleVerifyKey(_ sender: Any) {
        verifyKey()
    }

    // MARK: - Private

    private func formatKeyGenerationElapsedTime(with creationDate: Date) -> String? {
        return CustomDateComponentsFormatting.localizedString(
            from: creationDate,
            to: Date(),
            unitsStyle: .full
        ).map { (formattedInterval) -> String in
            return String(format: NSLocalizedString("KEY_GENERATED_SINCE_FORMAT", tableName: "WireguardKeys", comment: ""), formattedInterval)
        }
    }

    private func updateCreationDateLabel(with creationDate: Date) {
        contentView.creationRowView.value = formatKeyGenerationElapsedTime(with: creationDate) ?? "-"
    }

    private func updatePublicKey(tunnelSettings: TunnelSettings?, animated: Bool) {
        if let publicKey = tunnelSettings?.interface.privateKey.publicKeyWithMetadata {
            let displayKey = publicKey
                .stringRepresentation(maxLength: kDisplayPublicKeyMaxLength)

            setPublicKeyTitle(string: displayKey, animated: animated)
            updateCreationDateLabel(with: publicKey.creationDate)
        } else {
            setPublicKeyTitle(string: "-", animated: animated)
            contentView.creationRowView.value = "-"
        }
    }

    private func updateViewState(_ state: WireguardKeysViewState) {
        switch state {
        case .default:
            setKeyActionButtonsEnabled(true)
            contentView.publicKeyRowView.status = .default

        case .verifyingKey:
            setKeyActionButtonsEnabled(false)
            contentView.publicKeyRowView.status = .verifying

        case .verifiedKey(let isValid):
            setKeyActionButtonsEnabled(true)
            contentView.publicKeyRowView.status = .verified(isValid)
            announceKeyVerificationResult(isValid: isValid)

        case .regeneratingKey:
            setKeyActionButtonsEnabled(false)
            contentView.publicKeyRowView.status = .regenerating

        case .regeneratedKey(let success):
            setKeyActionButtonsEnabled(true)
            contentView.publicKeyRowView.status = .default
            if success {
                announceKeyRegenerated()
            }

        }
    }

    private func setKeyActionButtonsEnabled(_ enabled: Bool) {
        contentView.regenerateKeyButton.isEnabled = enabled
        contentView.verifyKeyButton.isEnabled = enabled
    }

    private func verifyKey() {
        guard let tunnelInfo = TunnelManager.shared.tunnelInfo else { return }

        self.updateViewState(.verifyingKey)

        REST.Client.shared.getWireguardKey(token: tunnelInfo.token, publicKey: tunnelInfo.tunnelSettings.interface.publicKey)
            .execute(retryStrategy: .default)
            .map { _ in
                return true
            }
            .flatMapError { error -> Result<Bool, REST.Error> in
                if case .server(.pubKeyNotFound) = error {
                    return .success(false)
                } else {
                    return .failure(error)
                }
            }
            .receive(on: .main)
            .storeCancellationToken(in: &verifyKeyCancellationToken)
            .onSuccess { [weak self] isValid in
                self?.updateViewState(.verifiedKey(isValid))
            }
            .onFailure { [weak self] error in
                self?.showKeyVerificationFailureAlert(error)
                self?.updateViewState(.default)
            }
            .observe { _ in }
    }

    private func regeneratePrivateKey() {
        self.updateViewState(.regeneratingKey)

        TunnelManager.shared.regeneratePrivateKey()
            .receive(on: .main)
            .onSuccess { [weak self] _ in
                self?.updateViewState(.regeneratedKey(true))
            }
            .onFailure { [weak self] error in
                self?.logger.error(chainedError: error, message: "Failed to regenerate the private key")

                self?.showKeyRegenerationFailureAlert(error)
                self?.updateViewState(.regeneratedKey(false))
            }
            .observe { _ in }
    }

    private func showKeyVerificationFailureAlert(_ error: REST.Error) {
        let reason = error.errorChainDescription ?? ""
        let errorDescription = String(
            format: NSLocalizedString(
                "VERIFY_KEY_FAILURE_ALERT_MESSAGE",
                tableName: "WireguardKeys",
                value: "Failed to verify the WireGuard key on server: %@",
                comment: ""
            ),
            reason
        )

        let alertController = UIAlertController(
            title: NSLocalizedString("VERIFY_KEY_FAILURE_ALERT_TITLE", tableName: "WireguardKeys", comment: ""),
            message: errorDescription,
            preferredStyle: .alert
        )

        alertController.addAction(
            UIAlertAction(title: NSLocalizedString("VERIFY_KEY_FAILURE_ALERT_OK_ACTION", tableName: "WireguardKeys", comment: ""), style: .cancel)
        )

        alertPresenter.enqueue(alertController, presentingController: self)
    }

    private func showKeyRegenerationFailureAlert(_ error: TunnelManager.Error) {
        let alertController = UIAlertController(
            title: NSLocalizedString("REGENERATE_KEY_FAILURE_ALERT_TITLE", tableName: "WireguardKeys", comment: ""),
            message: error.errorChainDescription,
            preferredStyle: .alert
        )
        alertController.addAction(
            UIAlertAction(title: NSLocalizedString("REGENERATE_KEY_FAILURE_ALERT_OK_ACTION", tableName: "WireguardKeys", comment: ""), style: .cancel)
        )

        alertPresenter.enqueue(alertController, presentingController: self)
    }


    private func setPublicKeyTitle(string: String, animated: Bool) {
        let updateTitle = {
            self.contentView.publicKeyRowView.value = string
        }

        if animated {
            updateTitle()
        } else {
            UIView.performWithoutAnimation {
                updateTitle()
                self.contentView.publicKeyRowView.layoutIfNeeded()
            }
        }
    }

    private func announceKeyVerificationResult(isValid: Bool) {
        let announcementString: String

        if isValid {
            announcementString = NSLocalizedString(
                "ACCESSIBILITY_ANNOUNCEMENT_VALID_KEY",
                tableName: "WireguardKeys",
                value: "Key is valid.",
                comment: ""
            )
        } else {
            announcementString = NSLocalizedString(
                "ACCESSIBILITY_ANNOUNCEMENT_INVALID_KEY",
                tableName: "WireguardKeys",
                value: "Key is invalid.",
                comment: ""
            )
        }

        UIAccessibility.post(notification: .announcement, argument: announcementString)
    }

    private func announceKeyRegenerated() {
        let announcementString = NSLocalizedString(
            "ACCESSIBILITY_ANNOUNCEMENT_REGENERATED_KEY",
            tableName: "WireguardKeys",
            value: "Key is regenerated.",
            comment: ""
        )
        UIAccessibility.post(notification: .announcement, argument: announcementString)
    }

}