Skip to content

Commit

Permalink
Merge branch 'PacketTunnel-State-refactor-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Apr 8, 2024
2 parents 8242e28 + 36a2a27 commit ede40a2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 11 additions & 1 deletion ios/PacketTunnelCore/Actor/PacketTunnelActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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?

Expand Down
11 changes: 8 additions & 3 deletions ios/PacketTunnelCore/Actor/State+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -149,7 +154,7 @@ extension State {
}
}

extension KeyPolicy {
extension State.KeyPolicy {
func logFormat() -> String {
switch self {
case .useCurrent:
Expand All @@ -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
Expand Down
45 changes: 16 additions & 29 deletions ios/PacketTunnelCore/Actor/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ 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
}

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.
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit ede40a2

Please sign in to comment.