-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
629 additions
and
16 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
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
105 changes: 105 additions & 0 deletions
105
ios/MullvadVPN/GeneralAPIs/OutgoingConnectionProxy.swift
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,105 @@ | ||
// | ||
// OutgoingConnectionProxy.swift | ||
// MullvadREST | ||
// | ||
// Created by Mojgan on 2023-10-24. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadREST | ||
import MullvadTypes | ||
import Network | ||
|
||
protocol OutgoingConnectionHandling { | ||
func getIPV6(retryStrategy: REST.RetryStrategy) async throws -> OutgoingConnectionProxy.IPV6ConnectionData | ||
func getIPV4(retryStrategy: REST.RetryStrategy) async throws -> OutgoingConnectionProxy.IPV4ConnectionData | ||
} | ||
|
||
final class OutgoingConnectionProxy: OutgoingConnectionHandling { | ||
let urlSession: URLSession | ||
|
||
init(urlSession: URLSession) { | ||
self.urlSession = urlSession | ||
} | ||
|
||
func getIPV6(retryStrategy: REST.RetryStrategy) async throws -> IPV6ConnectionData { | ||
try await perform(retryStrategy: retryStrategy, path: "https://ipv6.am.i.mullvad.net/json") | ||
} | ||
|
||
func getIPV4(retryStrategy: REST.RetryStrategy) async throws -> IPV4ConnectionData { | ||
try await perform(retryStrategy: retryStrategy, path: "https://ipv4.am.i.mullvad.net/json") | ||
} | ||
|
||
private func perform<T: Decodable>(retryStrategy: REST.RetryStrategy, path: String) async throws -> T { | ||
let delayIterator = retryStrategy.makeDelayIterator() | ||
for _ in 0 ..< retryStrategy.maxRetryCount { | ||
do { | ||
return try await perform(path: path) | ||
} catch { | ||
// ignore if request is cancelled | ||
if case URLError.cancelled = error { | ||
throw error | ||
} else { | ||
// retry with the delay | ||
guard let delay = delayIterator.next() else { throw error } | ||
let mills = UInt64(max(0, delay.milliseconds)) | ||
let nanos = mills.saturatingMultiplication(1_000_000) | ||
try await Task.sleep(nanoseconds: nanos) | ||
} | ||
} | ||
} | ||
return try await perform(path: path) | ||
} | ||
|
||
private func perform<T: Decodable>(path: String) async throws -> T { | ||
guard let url = URL(string: path) else { | ||
throw REST.Error.network(URLError(.badURL)) | ||
} | ||
do { | ||
let (data, response) = try await urlSession.data(from: url) | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw REST.Error.network(URLError(.badServerResponse)) | ||
} | ||
let decoder = JSONDecoder() | ||
guard (200 ..< 300).contains(httpResponse.statusCode) else { | ||
throw REST.Error.unhandledResponse( | ||
httpResponse.statusCode, | ||
try? decoder.decode( | ||
REST.ServerErrorResponse.self, | ||
from: data | ||
) | ||
) | ||
} | ||
let connectionData = try decoder.decode(T.self, from: data) | ||
return connectionData | ||
|
||
} catch { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
extension OutgoingConnectionProxy { | ||
typealias IPV4ConnectionData = OutgoingConnectionData<IPv4Address> | ||
typealias IPV6ConnectionData = OutgoingConnectionData<IPv6Address> | ||
typealias IPAddressType = Codable & IPAddress | ||
|
||
// MARK: - OutgoingConnectionData | ||
|
||
struct OutgoingConnectionData<T: IPAddressType>: Codable, Equatable { | ||
let ip: T? | ||
let exitIP: Bool | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case ip, exitIP = "mullvad_exit_ip" | ||
} | ||
|
||
static func == ( | ||
lhs: OutgoingConnectionProxy.OutgoingConnectionData<T>, | ||
rhs: OutgoingConnectionProxy.OutgoingConnectionData<T> | ||
) -> Bool { | ||
lhs.ip?.rawValue == rhs.ip?.rawValue && lhs.exitIP == rhs.exitIP | ||
} | ||
} | ||
} |
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.