|
| 1 | +// |
| 2 | +// MultihopDecisionFlow.swift |
| 3 | +// MullvadREST |
| 4 | +// |
| 5 | +// Created by Jon Petersson on 2024-06-14. |
| 6 | +// Copyright © 2024 Mullvad VPN AB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +protocol MultihopDecisionFlow { |
| 12 | + typealias RelayCandidate = RelayWithLocation<REST.ServerRelay> |
| 13 | + init(next: MultihopDecisionFlow?, relayPicker: RelayPicking) |
| 14 | + func canHandle(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) -> Bool |
| 15 | + func pick(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) throws -> SelectedRelays |
| 16 | +} |
| 17 | + |
| 18 | +struct OneToOne: MultihopDecisionFlow { |
| 19 | + let next: MultihopDecisionFlow? |
| 20 | + let relayPicker: RelayPicking |
| 21 | + init(next: (any MultihopDecisionFlow)?, relayPicker: RelayPicking) { |
| 22 | + self.next = next |
| 23 | + self.relayPicker = relayPicker |
| 24 | + } |
| 25 | + |
| 26 | + func pick(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) throws -> SelectedRelays { |
| 27 | + guard canHandle(entryCandidates: entryCandidates, exitCandidates: exitCandidates) else { |
| 28 | + guard let next else { |
| 29 | + throw NoRelaysSatisfyingConstraintsError() |
| 30 | + } |
| 31 | + return try next.pick(entryCandidates: entryCandidates, exitCandidates: exitCandidates) |
| 32 | + } |
| 33 | + |
| 34 | + guard entryCandidates.first != exitCandidates.first else { |
| 35 | + throw NoRelaysSatisfyingConstraintsError() |
| 36 | + } |
| 37 | + |
| 38 | + let entryMatch = try relayPicker.findBestMatch(from: entryCandidates) |
| 39 | + let exitMatch = try relayPicker.findBestMatch(from: exitCandidates) |
| 40 | + return SelectedRelays(entry: entryMatch, exit: exitMatch) |
| 41 | + } |
| 42 | + |
| 43 | + func canHandle(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) -> Bool { |
| 44 | + entryCandidates.count == 1 && exitCandidates.count == 1 |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +struct OneToMany: MultihopDecisionFlow { |
| 49 | + let next: MultihopDecisionFlow? |
| 50 | + let relayPicker: RelayPicking |
| 51 | + |
| 52 | + init(next: (any MultihopDecisionFlow)?, relayPicker: RelayPicking) { |
| 53 | + self.next = next |
| 54 | + self.relayPicker = relayPicker |
| 55 | + } |
| 56 | + |
| 57 | + func pick(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) throws -> SelectedRelays { |
| 58 | + guard let multihopPicker = relayPicker as? MultihopPicker else { |
| 59 | + fatalError("Could not cast picker to MultihopPicker") |
| 60 | + } |
| 61 | + |
| 62 | + guard canHandle(entryCandidates: entryCandidates, exitCandidates: exitCandidates) else { |
| 63 | + guard let next else { |
| 64 | + throw NoRelaysSatisfyingConstraintsError() |
| 65 | + } |
| 66 | + return try next.pick(entryCandidates: entryCandidates, exitCandidates: exitCandidates) |
| 67 | + } |
| 68 | + |
| 69 | + switch (entryCandidates.count, exitCandidates.count) { |
| 70 | + case let (1, count) where count > 1: |
| 71 | + let entryMatch = try multihopPicker.findBestMatch(from: entryCandidates) |
| 72 | + let exitMatch = try multihopPicker.exclude(relay: entryMatch, from: exitCandidates) |
| 73 | + return SelectedRelays(entry: entryMatch, exit: exitMatch) |
| 74 | + default: |
| 75 | + let exitMatch = try multihopPicker.findBestMatch(from: exitCandidates) |
| 76 | + let entryMatch = try multihopPicker.exclude(relay: exitMatch, from: entryCandidates) |
| 77 | + return SelectedRelays(entry: entryMatch, exit: exitMatch) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func canHandle(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) -> Bool { |
| 82 | + (entryCandidates.count == 1 && exitCandidates.count > 1) || |
| 83 | + (entryCandidates.count > 1 && exitCandidates.count == 1) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +struct ManyToMany: MultihopDecisionFlow { |
| 88 | + let next: MultihopDecisionFlow? |
| 89 | + let relayPicker: RelayPicking |
| 90 | + |
| 91 | + init(next: (any MultihopDecisionFlow)?, relayPicker: RelayPicking) { |
| 92 | + self.next = next |
| 93 | + self.relayPicker = relayPicker |
| 94 | + } |
| 95 | + |
| 96 | + func pick(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) throws -> SelectedRelays { |
| 97 | + guard let multihopPicker = relayPicker as? MultihopPicker else { |
| 98 | + fatalError("Could not cast picker to MultihopPicker") |
| 99 | + } |
| 100 | + |
| 101 | + guard canHandle(entryCandidates: entryCandidates, exitCandidates: exitCandidates) else { |
| 102 | + guard let next else { |
| 103 | + throw NoRelaysSatisfyingConstraintsError() |
| 104 | + } |
| 105 | + return try next.pick(entryCandidates: entryCandidates, exitCandidates: exitCandidates) |
| 106 | + } |
| 107 | + |
| 108 | + let exitMatch = try multihopPicker.findBestMatch(from: exitCandidates) |
| 109 | + let entryMatch = try multihopPicker.exclude(relay: exitMatch, from: entryCandidates) |
| 110 | + return SelectedRelays(entry: entryMatch, exit: exitMatch) |
| 111 | + } |
| 112 | + |
| 113 | + func canHandle(entryCandidates: [RelayCandidate], exitCandidates: [RelayCandidate]) -> Bool { |
| 114 | + entryCandidates.count > 1 && exitCandidates.count > 1 |
| 115 | + } |
| 116 | +} |
0 commit comments