Skip to content

Commit

Permalink
Add smart routing to daita settings data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson authored and buggmagnet committed Oct 2, 2024
1 parent 9e5b9c3 commit 11bd868
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 28 deletions.
4 changes: 2 additions & 2 deletions ios/MullvadREST/Relay/RelayPicking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct SinglehopPicker: RelayPicking {
by: constraints.exitLocations,
in: relays,
filterConstraint: constraints.filter,
daitaEnabled: daitaSettings.state.isEnabled
daitaEnabled: daitaSettings.daitaState.isEnabled
)
} catch let error as NoRelaysSatisfyingConstraintsError where error.reason == .noDaitaRelaysFound {
#if DEBUG
Expand Down Expand Up @@ -83,7 +83,7 @@ struct MultihopPicker: RelayPicking {
by: constraints.entryLocations,
in: relays,
filterConstraint: constraints.filter,
daitaEnabled: daitaSettings.state.isEnabled
daitaEnabled: daitaSettings.daitaState.isEnabled
)

let exitCandidates = try RelaySelector.WireGuard.findCandidates(
Expand Down
34 changes: 30 additions & 4 deletions ios/MullvadSettings/DAITASettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,41 @@ public enum DAITAState: Codable {
}
}

/// Selected relay is incompatible with Daita, either through singlehop or multihop.
/// Whether smart routing is enabled
public enum SmartRoutingState: Codable {
case on
case off

public var isEnabled: Bool {
self == .on
}
}

/// Selected relay is incompatible with DAITA, either through singlehop or multihop.
public enum DAITASettingsCompatibilityError {
case singlehop, multihop
}

public struct DAITASettings: Codable, Equatable {
public let state: DAITAState
@available(*, deprecated, renamed: "daitaState")
public let state: DAITAState = .off

public let daitaState: DAITAState
public let smartRoutingState: SmartRoutingState

public init(daitaState: DAITAState = .off, smartRoutingState: SmartRoutingState = .off) {
self.daitaState = daitaState
self.smartRoutingState = smartRoutingState
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

daitaState = try container.decodeIfPresent(DAITAState.self, forKey: .daitaState)
?? container.decodeIfPresent(DAITAState.self, forKey: .state)
?? .off

public init(state: DAITAState = .off) {
self.state = state
smartRoutingState = try container.decodeIfPresent(SmartRoutingState.self, forKey: .smartRoutingState)
?? .off
}
}
2 changes: 1 addition & 1 deletion ios/MullvadVPN/Coordinators/LocationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class LocationCoordinator: Coordinator, Presentable, Presenting {
customListRepository: customListRepository,
constraints: tunnelManager.settings.relayConstraints,
multihopEnabled: tunnelManager.settings.tunnelMultihopState.isEnabled,
daitaEnabled: tunnelManager.settings.daita.state.isEnabled,
daitaEnabled: tunnelManager.settings.daita.daitaState.isEnabled,
startContext: startContext
)
locationViewControllerWrapper.delegate = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ final class SimulatorTunnelProviderHost: SimulatorTunnelProviderDelegate {
remotePort: selectedRelays.entry?.endpoint.ipv4Relay.port ?? selectedRelays.exit.endpoint.ipv4Relay
.port,
isPostQuantum: settings.tunnelQuantumResistance.isEnabled,
isDaitaEnabled: settings.daita.state.isEnabled
isDaitaEnabled: settings.daita.daitaState.isEnabled
)
)
} catch {
Expand Down
2 changes: 1 addition & 1 deletion ios/MullvadVPN/TunnelManager/StartTunnelOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class StartTunnelOperation: ResultOperation<Void> {
tunnelStatus.state = .connecting(
selectedRelays,
isPostQuantum: interactor.settings.tunnelQuantumResistance.isEnabled,
isDaita: interactor.settings.daita.state.isEnabled
isDaita: interactor.settings.daita.daitaState.isEnabled
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ final class VPNSettingsCellFactory: CellFactoryProtocol {
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.setOn(viewModel.daitaSettings.state.isEnabled, animated: false)
cell.setOn(viewModel.daitaSettings.daitaState.isEnabled, animated: false)

cell.infoButtonHandler = { [weak self] in
self?.delegate?.showInfo(for: .daita)
}

cell.action = { [weak self] isEnabled in
let state: DAITAState = isEnabled ? .on : .off
self?.delegate?.switchDaitaState(DAITASettings(state: state))
self?.delegate?.switchDaitaState(DAITASettings(daitaState: state))
}

case .multihopSwitch:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class VPNSettingsInteractor {
}

func evaluateDaitaSettingsCompatibility(_ settings: DAITASettings) -> DAITASettingsCompatibilityError? {
guard settings.state.isEnabled else { return nil }
guard settings.daitaState.isEnabled else { return nil }

var tunnelSettings = tunnelSettings
tunnelSettings.daita = settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ extension MultihopDecisionFlowTests {

return MultihopPicker(
constraints: constraints,
daitaSettings: DAITASettings(state: .off),
daitaSettings: DAITASettings(daitaState: .off),
relays: sampleRelays,
connectionAttemptCount: 0
)
Expand Down
6 changes: 3 additions & 3 deletions ios/MullvadVPNTests/MullvadREST/Relay/RelayPickingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RelayPickingTests: XCTestCase {

let picker = SinglehopPicker(
constraints: constraints,
daitaSettings: DAITASettings(state: .off),
daitaSettings: DAITASettings(daitaState: .off),
relays: sampleRelays,
connectionAttemptCount: 0
)
Expand All @@ -43,7 +43,7 @@ class RelayPickingTests: XCTestCase {

let picker = MultihopPicker(
constraints: constraints,
daitaSettings: DAITASettings(state: .off),
daitaSettings: DAITASettings(daitaState: .off),
relays: sampleRelays,
connectionAttemptCount: 0
)
Expand All @@ -62,7 +62,7 @@ class RelayPickingTests: XCTestCase {

let picker = MultihopPicker(
constraints: constraints,
daitaSettings: DAITASettings(state: .off),
daitaSettings: DAITASettings(daitaState: .off),
relays: sampleRelays,
connectionAttemptCount: 0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: singlehopWithoutDaitaConstraints,
tunnelMultihopState: .off,
daita: DAITASettings(state: .off)
daita: DAITASettings(daitaState: .off)
)

let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
Expand All @@ -61,7 +61,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: multihopWithDaitaConstraints,
tunnelMultihopState: .on,
daita: DAITASettings(state: .off)
daita: DAITASettings(daitaState: .off)
)

let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
Expand All @@ -74,7 +74,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: multihopWithDaitaConstraints,
tunnelMultihopState: .on,
daita: DAITASettings(state: .on)
daita: DAITASettings(daitaState: .on)
)

XCTAssertNoThrow(try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0))
Expand All @@ -86,7 +86,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: multihopWithoutDaitaConstraints,
tunnelMultihopState: .on,
daita: DAITASettings(state: .on)
daita: DAITASettings(daitaState: .on)
)

XCTAssertThrowsError(try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0))
Expand All @@ -98,7 +98,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: singlehopWithDaitaConstraints,
tunnelMultihopState: .off,
daita: DAITASettings(state: .on)
daita: DAITASettings(daitaState: .on)
)

let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
Expand All @@ -113,7 +113,7 @@ class RelaySelectorWrapperTests: XCTestCase {
let settings = LatestTunnelSettings(
relayConstraints: singlehopWithoutDaitaConstraints,
tunnelMultihopState: .off,
daita: DAITASettings(state: .on)
daita: DAITASettings(daitaState: .on)
)

let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class TunnelSettingsUpdateTests: XCTestCase {

func testApplyDAITA() {
// Given:
let daitaSettings = DAITASettings(state: .on)
let daitaSettings = DAITASettings(daitaState: .on)
var settings = LatestTunnelSettings()

// When:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension PacketTunnelActor {
}

var daitaConfiguration: DaitaConfiguration?
if settings.daita.state.isEnabled {
if settings.daita.daitaState.isEnabled {
let maybeNot = Maybenot()
daitaConfiguration = DaitaConfiguration(
machines: maybeNot.machines,
Expand Down
8 changes: 4 additions & 4 deletions ios/PacketTunnelCore/Actor/PacketTunnelActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ extension PacketTunnelActor {
) async throws {
let settings: Settings = try settingsReader.read()

if settings.quantumResistance.isEnabled || settings.daita.state.isEnabled {
if settings.quantumResistance.isEnabled || settings.daita.daitaState.isEnabled {
try await tryStartEphemeralPeerNegotiation(withSettings: settings, nextRelays: nextRelays, reason: reason)
} else {
try await tryStartConnection(withSettings: settings, nextRelays: nextRelays, reason: reason)
Expand Down Expand Up @@ -307,7 +307,7 @@ extension PacketTunnelActor {
}

var daitaConfiguration: DaitaConfiguration?
if settings.daita.state.isEnabled {
if settings.daita.daitaState.isEnabled {
let maybeNot = Maybenot()
daitaConfiguration = DaitaConfiguration(
machines: maybeNot.machines,
Expand Down Expand Up @@ -417,7 +417,7 @@ extension PacketTunnelActor {
transportLayer: .udp,
remotePort: connectedRelay.endpoint.ipv4Relay.port,
isPostQuantum: settings.quantumResistance.isEnabled,
isDaitaEnabled: settings.daita.state.isEnabled
isDaitaEnabled: settings.daita.daitaState.isEnabled
)
case .disconnecting, .disconnected:
return nil
Expand Down Expand Up @@ -460,7 +460,7 @@ extension PacketTunnelActor {
transportLayer: transportLayer,
remotePort: protocolObfuscator.remotePort,
isPostQuantum: settings.quantumResistance.isEnabled,
isDaitaEnabled: settings.daita.state.isEnabled
isDaitaEnabled: settings.daita.daitaState.isEnabled
)
}

Expand Down

0 comments on commit 11bd868

Please sign in to comment.