summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/Transport/Shadowsocks/ShadowsocksRelaySelector.swift
blob: 673ad675e55d68899116f04802fdc87cdaa51374 (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
//
//  ShadowsocksRelaySelector.swift
//  MullvadREST
//
//  Created by Mojgan on 2024-05-23.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadSettings
import MullvadTypes

public protocol ShadowsocksRelaySelectorProtocol: Sendable {
    func selectRelay(with settings: LatestTunnelSettings) throws -> REST.BridgeRelay?

    func getBridges() throws -> REST.ServerShadowsocks?
}

final public class ShadowsocksRelaySelector: ShadowsocksRelaySelectorProtocol {
    let relayCache: RelayCacheProtocol

    public init(
        relayCache: RelayCacheProtocol
    ) {
        self.relayCache = relayCache
    }

    public func selectRelay(with settings: LatestTunnelSettings) throws -> REST.BridgeRelay? {
        let cachedRelays = try relayCache.read().relays

        let locationConstraint =
            switch settings.tunnelMultihopState {
            case .on: settings.relayConstraints.entryLocations
            case .off: settings.relayConstraints.exitLocations
            }

        return RelaySelector.Shadowsocks.closestRelay(
            location: locationConstraint,
            port: settings.relayConstraints.port,
            filter: settings.relayConstraints.filter,
            in: cachedRelays
        )
    }

    public func getBridges() throws -> REST.ServerShadowsocks? {
        let cachedRelays = try relayCache.read()
        return RelaySelector.Shadowsocks.tcpBridge(from: cachedRelays.relays)
    }
}