summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/Relay/RelaySelectorProtocol.swift
blob: e82cf5f284fd26faa272343f56aaa4479cba7b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
//  RelaySelectorProtocol.swift
//  PacketTunnel
//
//  Created by pronebird on 08/08/2023.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import MullvadTypes

/// Protocol describing a type that can select a relay.
public protocol RelaySelectorProtocol {
    var relayCache: RelayCacheProtocol { get }
    func selectRelays(
        tunnelSettings: LatestTunnelSettings,
        connectionAttemptCount: UInt
    ) throws -> SelectedRelays

    func findCandidates(
        tunnelSettings: LatestTunnelSettings
    ) throws -> RelayCandidates
}

/// Struct describing the selected relay.
public struct SelectedRelay: Equatable, Codable, Sendable {
    /// Selected relay endpoint.
    public let endpoint: MullvadEndpoint

    /// Relay hostname.
    public let hostname: String

    /// Relay geo location.
    public let location: Location

    /// Relay features, such as `DAITA` or `QUIC`.
    public let features: REST.ServerRelay.Features?

    /// Designated initializer.
    public init(endpoint: MullvadEndpoint, hostname: String, location: Location, features: REST.ServerRelay.Features?) {
        self.endpoint = endpoint
        self.hostname = hostname
        self.location = location
        self.features = features
    }
}

extension SelectedRelay: CustomDebugStringConvertible {
    public var debugDescription: String {
        "\(hostname) -> \(endpoint.ipv4Relay.description)"
    }
}

public struct SelectedRelays: Equatable, Codable, Sendable {
    public let entry: SelectedRelay?
    public let exit: SelectedRelay
    public let retryAttempt: UInt
    public let obfuscation: WireGuardObfuscationState

    public var ingress: SelectedRelay {
        entry ?? exit
    }

    public init(
        entry: SelectedRelay?,
        exit: SelectedRelay,
        retryAttempt: UInt,
        obfuscation: WireGuardObfuscationState
    ) {
        self.entry = entry
        self.exit = exit
        self.retryAttempt = retryAttempt
        self.obfuscation = obfuscation
    }
}

extension SelectedRelays: CustomDebugStringConvertible {
    public var debugDescription: String {
        "Entry: \(entry?.hostname ?? "-") -> \(entry?.endpoint.ipv4Relay.description ?? "-"), "
            + "Exit: \(exit.hostname) -> \(exit.endpoint.ipv4Relay.description), obfuscation: \(obfuscation)"
    }
}