Skip to content

Commit

Permalink
reusing existing connection for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed Jan 18, 2024
1 parent 11f6628 commit 8da7d99
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 40 deletions.
8 changes: 5 additions & 3 deletions ios/MullvadREST/Transport/AccessMethodIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class AccessMethodIterator {
private let dataSource: AccessMethodRepositoryDataSource

private var index = 0
private var enabledConfigurations: [PersistentAccessMethod] = []
private var cancellables = Set<Combine.AnyCancellable>()

private var enabledConfigurations: [PersistentAccessMethod] {
dataSource.accessMethods.filter { $0.isEnabled }
}

private var lastReachableApiAccessId: UUID {
lastReachableApiAccessCache.id
}
Expand All @@ -31,9 +34,8 @@ class AccessMethodIterator {

self.dataSource
.publisher
.sink { [weak self] configurations in
.sink { [weak self] _ in
guard let self else { return }
self.enabledConfigurations = configurations.filter { $0.isEnabled }
self.refreshCacheIfNeeded()
}
.store(in: &cancellables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import MullvadTypes
import Network

public struct ShadowsocksConfiguration: Codable {
public struct ShadowsocksConfiguration: Codable, Equatable {
public let address: AnyIPAddress
public let port: UInt16
public let password: String
Expand Down
2 changes: 1 addition & 1 deletion ios/MullvadREST/Transport/Socks5/Socks5Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MullvadTypes

/// Socks5 configuration.
/// - See: ``URLSessionSocks5Transport``
public struct Socks5Configuration {
public struct Socks5Configuration: Equatable {
/// The socks proxy endpoint.
public var proxyEndpoint: AnyIPEndpoint

Expand Down
46 changes: 29 additions & 17 deletions ios/MullvadREST/Transport/TransportProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class TransportProvider: RESTTransportProvider {
private let addressCache: REST.AddressCache
private var transportStrategy: TransportStrategy
private var currentTransport: RESTTransport?
private var currentTransportType: TransportStrategy.Transport
private let parallelRequestsMutex = NSLock()

public init(
Expand All @@ -25,6 +26,7 @@ public final class TransportProvider: RESTTransportProvider {
self.urlSessionTransport = urlSessionTransport
self.addressCache = addressCache
self.transportStrategy = transportStrategy
self.currentTransportType = transportStrategy.connectionTransport()
}

public func makeTransport() -> RESTTransport? {
Expand Down Expand Up @@ -62,26 +64,36 @@ public final class TransportProvider: RESTTransportProvider {
///
/// - Returns: A `RESTTransport` object to make a connection
private func makeTransportInner() -> RESTTransport? {
switch transportStrategy.connectionTransport() {
case .direct:
currentTransport = urlSessionTransport
case let .shadowsocks(configuration):
currentTransport = ShadowsocksTransport(
urlSession: urlSessionTransport.urlSession,
configuration: configuration,
addressCache: addressCache
)
case let .socks5(configuration):
currentTransport = URLSessionSocks5Transport(
urlSession: urlSessionTransport.urlSession,
configuration: configuration,
addressCache: addressCache
)
case .none:
currentTransport = nil
if currentTransport == nil || shouldNotReuseCurrentTransport {
currentTransportType = transportStrategy.connectionTransport()
switch currentTransportType {
case .direct:
currentTransport = urlSessionTransport
case let .shadowsocks(configuration):
currentTransport = ShadowsocksTransport(
urlSession: urlSessionTransport.urlSession,
configuration: configuration,
addressCache: addressCache
)
case let .socks5(configuration):
currentTransport = URLSessionSocks5Transport(
urlSession: urlSessionTransport.urlSession,
configuration: configuration,
addressCache: addressCache
)
case .none:
currentTransport = nil
}
}
return currentTransport
}

/// The `Main` allows modifications to access methods through the UI.
/// The `TransportProvider` relies on a `CurrentTransport` value set during build time or network error.
/// To ensure both process `Packet Tunnel` and `Main` uses the latest changes, the `TransportProvider` compares the `transportType` with the latest value in the cache and reuse it if it's still valid .
private var shouldNotReuseCurrentTransport: Bool {
currentTransportType != transportStrategy.connectionTransport()
}
}

private extension URLError {
Expand Down
15 changes: 14 additions & 1 deletion ios/MullvadREST/Transport/TransportStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MullvadTypes

public class TransportStrategy: Equatable {
/// The different transports suggested by the strategy
public enum Transport {
public enum Transport: Equatable {
/// Connecting a direct connection
case direct

Expand All @@ -25,6 +25,19 @@ public class TransportStrategy: Equatable {

/// Failing to retrive transport
case none

public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case(.direct, .direct), (.none, .none):
return true
case let (.shadowsocks(lhsConfiguration), .shadowsocks(rhsConfiguration)):
return lhsConfiguration == rhsConfiguration
case let (.socks5(lhsConfiguration), .socks5(rhsConfiguration)):
return lhsConfiguration == rhsConfiguration
default:
return false
}
}
}

private let shadowsocksLoader: ShadowsocksLoaderProtocol
Expand Down
4 changes: 4 additions & 0 deletions ios/MullvadRESTTests/AccessMethodRepositoryStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ struct AccessMethodRepositoryStub: AccessMethodRepositoryDataSource {
passthroughSubject.eraseToAnyPublisher()
}

var accessMethods: [PersistentAccessMethod] {
passthroughSubject.value
}

let passthroughSubject: CurrentValueSubject<[PersistentAccessMethod], Never> = CurrentValueSubject([])

init(accessMethods: [PersistentAccessMethod]) {
Expand Down
17 changes: 0 additions & 17 deletions ios/MullvadRESTTests/TransportStrategyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,6 @@ class TransportStrategyTests: XCTestCase {
}
}

extension TransportStrategy.Transport: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case(.direct, .direct), (.none, .none):
return true
case let (.shadowsocks(config1), .shadowsocks(config2)):
return config1.port == config2.port && config1.cipher == config2.cipher && config1.password == config2
.password
case let (.socks5(config1), .socks5(config2)):
return config1.proxyEndpoint == config2.proxyEndpoint && config1.username == config2.username && config1
.password == config2.password
default:
return false
}
}
}

private enum IOError: Error {
case fileNotFound
}
4 changes: 4 additions & 0 deletions ios/MullvadSettings/AccessMethodRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class AccessMethodRepository: AccessMethodRepositoryProtocol {
direct
}

public var accessMethods: [PersistentAccessMethod] {
fetchAll()
}

public init() {
add([direct, bridge])
}
Expand Down
2 changes: 2 additions & 0 deletions ios/MullvadSettings/AccessMethodRepositoryProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public protocol AccessMethodRepositoryDataSource {
var publisher: AnyPublisher<[PersistentAccessMethod], Never> { get }

var directAccess: PersistentAccessMethod { get }

var accessMethods: [PersistentAccessMethod] { get }
}

public protocol AccessMethodRepositoryProtocol: AccessMethodRepositoryDataSource {
Expand Down

0 comments on commit 8da7d99

Please sign in to comment.