summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/Relay/ObfuscationMethodSelector.swift
blob: 4803d38139eea4de253e103185a4d4a3c2b6b874 (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
//
//  ObfuscationMethodSelector.swift
//  MullvadREST
//
//  Created by Jon Petersson on 2024-11-01.
//  Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings

public struct ObfuscationMethodSelector {
    /// This retry logic used is explained at the following link:
    /// https://github.com/mullvad/mullvadvpn-app/blob/main/docs/relay-selector.md#default-constraints-for-tunnel-endpoints
    public static func obfuscationMethodBy(
        connectionAttemptCount: UInt,
        tunnelSettings: LatestTunnelSettings
    ) -> WireGuardObfuscationState {
        if tunnelSettings.wireGuardObfuscation.state == .automatic {
            if connectionAttemptCount.isOrdered(nth: 3, forEverySetOf: 4) {
                .shadowsocks
            } else if connectionAttemptCount.isOrdered(nth: 4, forEverySetOf: 4) {
                .udpOverTcp
            } else {
                .off
            }
        } else {
            tunnelSettings.wireGuardObfuscation.state
        }
    }
}