diff options
| author | Bug Magnet <marco.nikic@mullvad.net> | 2024-04-08 13:25:59 +0200 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-04-08 13:25:59 +0200 |
| commit | ede40a2c3ba4a021133051ef605864d8ffffd14d (patch) | |
| tree | 21787f4bd3fc114a7d0521cc871191dbbeb3a65b | |
| parent | 8242e289256afd142c21cdc2e5fca6aad5749d90 (diff) | |
| parent | 36a2a2790e2684309c2551ba62170146d56ea9e5 (diff) | |
| download | mullvadvpn-ede40a2c3ba4a021133051ef605864d8ffffd14d.tar.xz mullvadvpn-ede40a2c3ba4a021133051ef605864d8ffffd14d.zip | |
Merge branch 'PacketTunnel-State-refactor-2'
5 files changed, 37 insertions, 35 deletions
diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift index 40d0431d1a..b450f44819 100644 --- a/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift @@ -95,7 +95,7 @@ extension PacketTunnelActor { private func mapConnectionState( _ connState: State.ConnectionData, reason: BlockedStateReason, - priorState: StatePriorToBlockedState + priorState: State.BlockingData.PriorState ) -> State.BlockingData { State.BlockingData( reason: reason, diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift index 1636e84542..9181a73edf 100644 --- a/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift @@ -90,7 +90,7 @@ extension PacketTunnelActor { - Parameter keyPolicy: a reference to key policy held either in connection state or blocked state struct. - Returns: `true` when the policy was modified, otherwise `false`. */ - private func setCurrentKeyPolicy(_ keyPolicy: inout KeyPolicy) { + private func setCurrentKeyPolicy(_ keyPolicy: inout State.KeyPolicy) { if case .usePrior = keyPolicy { keyPolicy = .useCurrent } diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift index 360bfb8f55..81b3bfaff1 100644 --- a/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift @@ -124,6 +124,16 @@ public actor PacketTunnelActor { // MARK: - extension PacketTunnelActor { + /// Describes the reason for reconnection request. + enum ReconnectReason { + /// Initiated by user. + case userInitiated + + /// Initiated by tunnel monitor due to loss of connectivity. + /// Actor will increment the connection attempt counter before picking next relay. + case connectionLoss + } + /** Start the tunnel. @@ -295,7 +305,7 @@ extension PacketTunnelActor { settings: Settings, reason: ReconnectReason ) throws -> State.ConnectionData? { - var keyPolicy: KeyPolicy = .useCurrent + var keyPolicy: State.KeyPolicy = .useCurrent var networkReachability = defaultPathObserver.defaultPath?.networkReachability ?? .undetermined var lastKeyRotation: Date? diff --git a/ios/PacketTunnelCore/Actor/State+Extensions.swift b/ios/PacketTunnelCore/Actor/State+Extensions.swift index 97d2deb388..45b28a0c9c 100644 --- a/ios/PacketTunnelCore/Actor/State+Extensions.swift +++ b/ios/PacketTunnelCore/Actor/State+Extensions.swift @@ -11,6 +11,11 @@ import MullvadTypes import WireGuardKitTypes extension State { + /// Target state the actor should transition into upon request to either start (connect) or reconnect. + enum TargetStateForReconnect { + case reconnecting, connecting + } + /// Returns the target state to which the actor state should transition when requested to reconnect. /// It returns `nil` when reconnection is not supported such as when already `.disconnecting` or `.disconnected` states. var targetStateForReconnect: TargetStateForReconnect? { @@ -149,7 +154,7 @@ extension State { } } -extension KeyPolicy { +extension State.KeyPolicy { func logFormat() -> String { switch self { case .useCurrent: @@ -160,8 +165,8 @@ extension KeyPolicy { } } -extension KeyPolicy: Equatable { - static func == (lhs: KeyPolicy, rhs: KeyPolicy) -> Bool { +extension State.KeyPolicy: Equatable { + static func == (lhs: State.KeyPolicy, rhs: State.KeyPolicy) -> Bool { switch (lhs, rhs) { case (.useCurrent, .useCurrent): true case let (.usePrior(priorA, _), .usePrior(priorB, _)): priorA == priorB diff --git a/ios/PacketTunnelCore/Actor/State.swift b/ios/PacketTunnelCore/Actor/State.swift index 198ac45c96..ab3fe767c9 100644 --- a/ios/PacketTunnelCore/Actor/State.swift +++ b/ios/PacketTunnelCore/Actor/State.swift @@ -82,15 +82,6 @@ enum State: Equatable { case error(BlockingData) } -/// Policy describing what WG key to use for tunnel communication. -enum KeyPolicy { - /// Use current key stored in device data. - case useCurrent - - /// Use prior key until timer fires. - case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask) -} - /// Enum describing network availability. public enum NetworkReachability: Equatable, Codable { case undetermined, reachable, unreachable @@ -98,12 +89,21 @@ public enum NetworkReachability: Equatable, Codable { protocol StateAssociatedData { var currentKey: PrivateKey? { get set } - var keyPolicy: KeyPolicy { get set } + var keyPolicy: State.KeyPolicy { get set } var networkReachability: NetworkReachability { get set } var lastKeyRotation: Date? { get set } } extension State { + /// Policy describing what WG key to use for tunnel communication. + enum KeyPolicy { + /// Use current key stored in device data. + case useCurrent + + /// Use prior key until timer fires. + case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask) + } + /// Data associated with states that hold connection data. struct ConnectionData: Equatable, StateAssociatedData { /// Current selected relay. @@ -173,7 +173,7 @@ extension State { public var recoveryTask: AutoCancellingTask? /// Prior state of the actor before entering blocked state - public var priorState: StatePriorToBlockedState + public var priorState: PriorState } } @@ -214,14 +214,11 @@ public enum BlockedStateReason: String, Codable, Equatable { case unknown } -/// Legal states that can precede error state. -enum StatePriorToBlockedState: Equatable { - case initial, connecting, connected, reconnecting -} - -/// Target state the actor should transition into upon request to either start (connect) or reconnect. -enum TargetStateForReconnect { - case reconnecting, connecting +extension State.BlockingData { + /// Legal states that can precede error state. + enum PriorState: Equatable { + case initial, connecting, connected, reconnecting + } } /// Describes which relay the tunnel should connect to next. @@ -235,13 +232,3 @@ public enum NextRelay: Equatable, Codable { /// Use pre-selected relay. case preSelected(SelectedRelay) } - -/// Describes the reason for reconnection request. -enum ReconnectReason { - /// Initiated by user. - case userInitiated - - /// Initiated by tunnel monitor due to loss of connectivity. - /// Actor will increment the connection attempt counter before picking next relay. - case connectionLoss -} |
