From f563dcbc85d2338e81fa3f739b533271f20ffa11 Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Thu, 4 Apr 2024 14:13:52 +0200 Subject: [PATCH 1/4] Rename StatePriorToBlockedState to State.BlockingData.PriorState --- .../Actor/PacketTunnelActor+ErrorState.swift | 2 +- ios/PacketTunnelCore/Actor/State.swift | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift index 40d0431d1a72..b450f44819c3 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/State.swift b/ios/PacketTunnelCore/Actor/State.swift index 198ac45c9660..6728d4b8a957 100644 --- a/ios/PacketTunnelCore/Actor/State.swift +++ b/ios/PacketTunnelCore/Actor/State.swift @@ -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,9 +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 +extension State.BlockingData { + /// Legal states that can precede error state. + enum PriorState: Equatable { + case initial, connecting, connected, reconnecting + } } /// Target state the actor should transition into upon request to either start (connect) or reconnect. From dab8a1816509a2e79844010cb3171ee87bb3f1fc Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Thu, 4 Apr 2024 14:24:15 +0200 Subject: [PATCH 2/4] =?UTF-8?q?Move=20TargetStateForReconnect=20under=20St?= =?UTF-8?q?ate=C2=B4s=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/PacketTunnelCore/Actor/State+Extensions.swift | 5 +++++ ios/PacketTunnelCore/Actor/State.swift | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ios/PacketTunnelCore/Actor/State+Extensions.swift b/ios/PacketTunnelCore/Actor/State+Extensions.swift index 97d2deb38898..d6ea62a7ab2d 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? { diff --git a/ios/PacketTunnelCore/Actor/State.swift b/ios/PacketTunnelCore/Actor/State.swift index 6728d4b8a957..d4252f0a6972 100644 --- a/ios/PacketTunnelCore/Actor/State.swift +++ b/ios/PacketTunnelCore/Actor/State.swift @@ -221,11 +221,6 @@ extension State.BlockingData { } } -/// Target state the actor should transition into upon request to either start (connect) or reconnect. -enum TargetStateForReconnect { - case reconnecting, connecting -} - /// Describes which relay the tunnel should connect to next. public enum NextRelay: Equatable, Codable { /// Select next relay randomly. From f5632e0f41e14a2530664ce4d9d86958673ebe88 Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Thu, 4 Apr 2024 14:28:15 +0200 Subject: [PATCH 3/4] Move ReconnectReason under PacketTunnelActor --- ios/PacketTunnelCore/Actor/PacketTunnelActor.swift | 10 ++++++++++ ios/PacketTunnelCore/Actor/State.swift | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift index 360bfb8f5544..4ec5b99de234 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. diff --git a/ios/PacketTunnelCore/Actor/State.swift b/ios/PacketTunnelCore/Actor/State.swift index d4252f0a6972..f65fdea515a4 100644 --- a/ios/PacketTunnelCore/Actor/State.swift +++ b/ios/PacketTunnelCore/Actor/State.swift @@ -232,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 -} From 36a2a2790e2684309c2551ba62170146d56ea9e5 Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Thu, 4 Apr 2024 14:55:16 +0200 Subject: [PATCH 4/4] Move KeyPolicy under State's namespace --- .../Actor/PacketTunnelActor+KeyPolicy.swift | 2 +- .../Actor/PacketTunnelActor.swift | 2 +- .../Actor/State+Extensions.swift | 6 +++--- ios/PacketTunnelCore/Actor/State.swift | 20 +++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift index 1636e8454292..9181a73edfd5 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 4ec5b99de234..81b3bfaff179 100644 --- a/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift @@ -305,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 d6ea62a7ab2d..45b28a0c9c32 100644 --- a/ios/PacketTunnelCore/Actor/State+Extensions.swift +++ b/ios/PacketTunnelCore/Actor/State+Extensions.swift @@ -154,7 +154,7 @@ extension State { } } -extension KeyPolicy { +extension State.KeyPolicy { func logFormat() -> String { switch self { case .useCurrent: @@ -165,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 f65fdea515a4..ab3fe767c937 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.