Skip to content

Commit

Permalink
Put app in blocked state on tunnel start if relay constraints cannot …
Browse files Browse the repository at this point in the history
…be satisfied

Signed-off-by: Bug Magnet <[email protected]>
  • Loading branch information
Jon Petersson authored and buggmagnet committed Oct 17, 2023
1 parent aee7561 commit dd18767
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
32 changes: 11 additions & 21 deletions ios/MullvadVPN/TunnelManager/StartTunnelOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,20 @@ class StartTunnelOperation: ResultOperation<Void> {
finish(result: .success(()))

case .disconnected, .pendingReconnect:
do {
let selectedRelay = try interactor.selectRelay()

makeTunnelProviderAndStartTunnel(selectedRelay: selectedRelay) { error in
self.finish(result: error.map { .failure($0) } ?? .success(()))
}
} catch {
finish(result: .failure(error))
makeTunnelProviderAndStartTunnel { error in
self.finish(result: error.map { .failure($0) } ?? .success(()))
}

default:
finish(result: .success(()))
}
}

private func makeTunnelProviderAndStartTunnel(
selectedRelay: SelectedRelay,
completionHandler: @escaping (Error?) -> Void
) {
private func makeTunnelProviderAndStartTunnel(completionHandler: @escaping (Error?) -> Void) {
makeTunnelProvider { result in
self.dispatchQueue.async {
do {
try self.startTunnel(
tunnel: try result.get(),
selectedRelay: selectedRelay
)

try self.startTunnel(tunnel: result.get())
completionHandler(nil)
} catch {
completionHandler(error)
Expand All @@ -85,11 +72,14 @@ class StartTunnelOperation: ResultOperation<Void> {
}
}

private func startTunnel(tunnel: any TunnelProtocol, selectedRelay: SelectedRelay) throws {
private func startTunnel(tunnel: any TunnelProtocol) throws {
let selectedRelay = try? interactor.selectRelay()
var tunnelOptions = PacketTunnelOptions()

do {
try tunnelOptions.setSelectedRelay(selectedRelay)
if let selectedRelay {
try tunnelOptions.setSelectedRelay(selectedRelay)
}
} catch {
logger.error(
error: error,
Expand All @@ -101,8 +91,8 @@ class StartTunnelOperation: ResultOperation<Void> {

interactor.updateTunnelStatus { tunnelStatus in
tunnelStatus = TunnelStatus()
tunnelStatus.packetTunnelStatus.tunnelRelay = selectedRelay.packetTunnelRelay
tunnelStatus.state = .connecting(selectedRelay.packetTunnelRelay)
tunnelStatus.packetTunnelStatus.tunnelRelay = selectedRelay?.packetTunnelRelay
tunnelStatus.state = .connecting(selectedRelay?.packetTunnelRelay)
}

try tunnel.start(options: tunnelOptions.rawOptions())
Expand Down
10 changes: 1 addition & 9 deletions ios/MullvadVPN/TunnelManager/TunnelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,10 @@ final class TunnelManager: StorePaymentObserver {
throw UnsetTunnelError()
}

let nextRelay: NextRelay = selectNewRelay ? .preSelected(try self.selectRelay()) : .current

return tunnel.reconnectTunnel(to: nextRelay) { result in
return tunnel.reconnectTunnel(to: selectNewRelay ? .random : .current) { result in
finish(result.error)
}
} catch {
if error is NoRelaysSatisfyingConstraintsError {
_ = self.setTunnelStatus { tunnelStatus in
tunnelStatus.state = .error(.noRelaysSatisfyingConstraints)
}
}

finish(error)

return AnyCancellable()
Expand Down

0 comments on commit dd18767

Please sign in to comment.