-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRelaySelector+Wireguard.swift
70 lines (62 loc) · 2.42 KB
/
RelaySelector+Wireguard.swift
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
//
// RelaySelector+Wireguard.swift
// MullvadREST
//
// Created by Mojgan on 2024-05-17.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import MullvadTypes
extension RelaySelector {
public enum WireGuard {
/// Filters relay list using given constraints.
public static func findCandidates(
by relayConstraint: RelayConstraint<UserSelectedRelays>,
in relays: REST.ServerRelaysResponse,
filterConstraint: RelayConstraint<RelayFilter>,
daitaEnabled: Bool
) throws -> [RelayWithLocation<REST.ServerRelay>] {
let mappedRelays = mapRelays(relays: relays.wireguard.relays, locations: relays.locations)
return try applyConstraints(
relayConstraint,
filterConstraint: filterConstraint,
daitaEnabled: daitaEnabled,
relays: mappedRelays
)
}
/// Picks a random relay from a list.
public static func pickCandidate(
from relayWithLocations: [RelayWithLocation<REST.ServerRelay>],
relays: REST.ServerRelaysResponse,
portConstraint: RelayConstraint<UInt16>,
numberOfFailedAttempts: UInt
) throws -> RelaySelectorMatch {
let port = applyPortConstraint(
portConstraint,
rawPortRanges: relays.wireguard.portRanges,
numberOfFailedAttempts: numberOfFailedAttempts
)
guard let port else {
throw NoRelaysSatisfyingConstraintsError(.invalidPort)
}
guard let relayWithLocation = pickRandomRelayByWeight(relays: relayWithLocations) else {
throw NoRelaysSatisfyingConstraintsError(.relayConstraintNotMatching)
}
let endpoint = MullvadEndpoint(
ipv4Relay: IPv4Endpoint(
ip: relayWithLocation.relay.ipv4AddrIn,
port: port
),
ipv6Relay: nil,
ipv4Gateway: relays.wireguard.ipv4Gateway,
ipv6Gateway: relays.wireguard.ipv6Gateway,
publicKey: relayWithLocation.relay.publicKey
)
return RelaySelectorMatch(
endpoint: endpoint,
relay: relayWithLocation.relay,
location: relayWithLocation.serverLocation
)
}
}
}