summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/TunnelManager/TunnelState.swift
blob: 43d302a9b294a02b47da7d366615adbd1ebb6767 (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
//
//  TunnelState.swift
//  TunnelState
//
//  Created by pronebird on 11/08/2021.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadREST
import MullvadTypes
import PacketTunnelCore
@preconcurrency import WireGuardKitTypes

/// Describes the tunnel status.
///
/// The `state` property is reflected in the main view of the app, and typically shows
/// whether the VPN is connected, connecting, or disconnected.
/// On top of that, a banner might be shown in cases `state` is either `waitingForConnectivity` or `error`
///
/// The `observedState` contains metadata about the PacketTunnel, and can be used to infer various details such as
/// - A reason why the app would enter the blocked state
/// - Whether networking is available from within the `PacketTunnel` process
/// - How many times a reconnection was attempted
/// - Which protocol layer is used by the `PacketTunnel` (TCP, UDP etc...)
///
/// And so on, this is a non-exhaustive list.
struct TunnelStatus: Equatable, CustomStringConvertible, Sendable {
    /// Reflects the `PacketTunnel`'s internal state.
    var observedState: ObservedState = .disconnected

    /// Internal state used by the UI Process to manage transitions and UI updates.
    /// Directly affects the UI, what user actions are available.
    var state: TunnelState = .disconnected

    var description: String {
        var s = "\(state), network "

        if let connectionState = observedState.connectionState {
            if connectionState.isNetworkReachable {
                s += "reachable"
            } else {
                s += "unreachable"
            }
        } else {
            s += "reachability unknown"
        }

        return s
    }
}

/// An enum that describes the tunnel state.
enum TunnelState: Equatable, CustomStringConvertible, Sendable {
    enum WaitingForConnectionReason {
        /// Tunnel connection is down.
        case noConnection
        /// Network is down.
        case noNetwork
    }

    /// Pending reconnect after disconnect.
    case pendingReconnect

    /// Connecting the tunnel.
    case connecting(SelectedRelays?, isPostQuantum: Bool, isDaita: Bool)

    /// Negotiating an ephemeral peer either for post-quantum resistance or Daita
    case negotiatingEphemeralPeer(SelectedRelays, PrivateKey, isPostQuantum: Bool, isDaita: Bool)

    /// Connected the tunnel
    case connected(SelectedRelays, isPostQuantum: Bool, isDaita: Bool)

    /// Disconnecting the tunnel
    case disconnecting(ActionAfterDisconnect)

    /// Disconnected the tunnel
    case disconnected

    /// Reconnecting the tunnel.
    /// Transition to this state happens when:
    /// 1. Asking the running tunnel to reconnect to new relays via IPC.
    /// 2. Tunnel attempts to reconnect to new relays as the current relays appear to be
    ///    dysfunctional.
    case reconnecting(SelectedRelays, isPostQuantum: Bool, isDaita: Bool)

    /// Waiting for connectivity to come back up.
    case waitingForConnectivity(WaitingForConnectionReason)

    /// Error state.
    case error(BlockedStateReason)

    var description: String {
        switch self {
        case .pendingReconnect:
            "pending reconnect after disconnect"
        case let .connecting(tunnelRelays, isPostQuantum, isDaita):
            if let tunnelRelays {
                """
                connecting \(isPostQuantum ? "(PQ) " : ""), \
                daita: \(isDaita), \
                to \(tunnelRelays.exit.hostname)\
                \(tunnelRelays.entry.flatMap { " via \($0.hostname)" } ?? "")
                """
            } else {
                "connecting\(isPostQuantum ? " (PQ)" : ""), fetching relay"
            }
        case let .connected(tunnelRelays, isPostQuantum, isDaita):
            """
            connected \(isPostQuantum ? "(PQ) " : ""), \
            daita: \(isDaita), \
            to \(tunnelRelays.exit.hostname)\
            \(tunnelRelays.entry.flatMap { " via \($0.hostname)" } ?? "")
            """
        case let .disconnecting(actionAfterDisconnect):
            "disconnecting and then \(actionAfterDisconnect)"
        case .disconnected:
            "disconnected"
        case let .reconnecting(tunnelRelays, isPostQuantum, isDaita):
            """
            reconnecting \(isPostQuantum ? "(PQ) " : ""), \
            daita: \(isDaita), \
            to \(tunnelRelays.exit.hostname)\
            \(tunnelRelays.entry.flatMap { " via \($0.hostname)" } ?? "")
            """
        case .waitingForConnectivity:
            "waiting for connectivity"
        case let .error(blockedStateReason):
            "error state: \(blockedStateReason)"
        case let .negotiatingEphemeralPeer(tunnelRelays, _, isPostQuantum, isDaita):
            """
            negotiating key with exit relay: \(tunnelRelays.exit.hostname)\
            \(tunnelRelays.entry.flatMap { " via \($0.hostname)" } ?? ""), \
            isPostQuantum: \(isPostQuantum), isDaita: \(isDaita)
            """
        }
    }

    var isSecured: Bool {
        switch self {
        case .reconnecting, .connecting, .connected, .waitingForConnectivity(.noConnection), .error(.accountExpired),
            .error(.deviceRevoked), .negotiatingEphemeralPeer:
            true
        case .pendingReconnect, .disconnecting, .disconnected, .waitingForConnectivity(.noNetwork), .error:
            false
        }
    }

    var isBlockingInternet: Bool {
        switch self {
        case .connected, .disconnected:
            false
        default:
            true
        }
    }

    var relays: SelectedRelays? {
        switch self {
        case let .connected(relays, _, _),
            let .reconnecting(relays, _, _),
            let .negotiatingEphemeralPeer(relays, _, _, _):
            relays
        case let .connecting(relays, _, _):
            relays
        case .disconnecting, .disconnected, .waitingForConnectivity, .pendingReconnect, .error:
            nil
        }
    }

    // the two accessors below return a Bool?, to differentiate known
    // truth values from undefined/meaningless values, which the caller
    // may want to interpret differently
    var isPostQuantum: Bool? {
        switch self {
        case let .connecting(_, isPostQuantum: isPostQuantum, isDaita: _),
            let .connected(_, isPostQuantum: isPostQuantum, isDaita: _),
            let .reconnecting(_, isPostQuantum: isPostQuantum, isDaita: _):
            isPostQuantum
        default:
            nil
        }
    }

    var isDaita: Bool? {
        switch self {
        case let .connecting(_, isPostQuantum: _, isDaita: isDaita),
            let .connected(_, isPostQuantum: _, isDaita: isDaita),
            let .reconnecting(_, isPostQuantum: _, isDaita: isDaita):
            isDaita
        default:
            nil
        }
    }

    var isMultihop: Bool {
        relays?.entry != nil
    }
}

/// A enum that describes the action to perform after disconnect.
enum ActionAfterDisconnect: CustomStringConvertible {
    /// Do nothing after disconnecting.
    case nothing

    /// Reconnect after disconnecting.
    case reconnect

    var description: String {
        switch self {
        case .nothing:
            "do nothing"
        case .reconnect:
            "reconnect"
        }
    }
}