-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relay selector should use overridden IP addresses for relays
- Loading branch information
Jon Petersson
committed
Feb 7, 2024
1 parent
446ef2b
commit 35c5250
Showing
27 changed files
with
432 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// AnyRelay.swift | ||
// MullvadREST | ||
// | ||
// Created by Jon Petersson on 2024-01-31. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import MullvadTypes | ||
import Network | ||
|
||
public protocol AnyRelay { | ||
var hostname: String { get } | ||
var owned: Bool { get } | ||
var location: String { get } | ||
var provider: String { get } | ||
var weight: UInt64 { get } | ||
var active: Bool { get } | ||
var includeInCountry: Bool { get } | ||
|
||
func override(ipv4AddrIn: IPv4Address?, ipv6AddrIn: IPv6Address?) -> Self | ||
} | ||
|
||
extension REST.ServerRelay: AnyRelay {} | ||
extension REST.BridgeRelay: AnyRelay { | ||
public func override(ipv4AddrIn: IPv4Address?, ipv6AddrIn: IPv6Address?) -> REST.BridgeRelay { | ||
override(ipv4AddrIn: ipv4AddrIn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// IPOverrideWrapper.swift | ||
// MullvadREST | ||
// | ||
// Created by Jon Petersson on 2024-02-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import MullvadSettings | ||
import MullvadTypes | ||
|
||
public class IPOverrideWrapper: RelayCacheProtocol { | ||
private let relayCache: RelayCacheProtocol | ||
private let ipOverrideRepository: any IPOverrideRepositoryProtocol | ||
|
||
public init(relayCache: RelayCacheProtocol, ipOverrideRepository: any IPOverrideRepositoryProtocol) { | ||
self.relayCache = relayCache | ||
self.ipOverrideRepository = ipOverrideRepository | ||
} | ||
|
||
public func read() throws -> CachedRelays { | ||
let cache = try relayCache.read() | ||
let relayResponse = apply(overrides: ipOverrideRepository.fetchAll(), to: cache.relays) | ||
|
||
return CachedRelays(relays: relayResponse, updatedAt: cache.updatedAt) | ||
} | ||
|
||
public func write(record: CachedRelays) throws { | ||
try relayCache.write(record: record) | ||
} | ||
|
||
private func apply( | ||
overrides: [IPOverride], | ||
to relayResponse: REST.ServerRelaysResponse | ||
) -> REST.ServerRelaysResponse { | ||
let wireguard = relayResponse.wireguard | ||
let bridge = relayResponse.bridge | ||
|
||
let overridenWireguardRelays = wireguard.relays.map { relay in | ||
return apply(overrides: overrides, to: relay) | ||
} | ||
let overridenBridgeRelays = bridge.relays.map { relay in | ||
return apply(overrides: overrides, to: relay) | ||
} | ||
|
||
return REST.ServerRelaysResponse( | ||
locations: relayResponse.locations, | ||
wireguard: REST.ServerWireguardTunnels( | ||
ipv4Gateway: wireguard.ipv4Gateway, | ||
ipv6Gateway: wireguard.ipv6Gateway, | ||
portRanges: wireguard.portRanges, | ||
relays: overridenWireguardRelays | ||
), | ||
bridge: REST.ServerBridges( | ||
shadowsocks: bridge.shadowsocks, | ||
relays: overridenBridgeRelays | ||
) | ||
) | ||
} | ||
|
||
private func apply<T: AnyRelay>(overrides: [IPOverride], to relay: T) -> T { | ||
return overrides | ||
.first { $0.hostname == relay.hostname } | ||
.flatMap { | ||
relay.override( | ||
ipv4AddrIn: $0.ipv4Address, | ||
ipv6AddrIn: $0.ipv6Address | ||
) | ||
} | ||
?? relay | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.