-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using various api access methods during a network error
- Loading branch information
Showing
24 changed files
with
641 additions
and
438 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// AccessMethodIterator.swift | ||
// MullvadREST | ||
// | ||
// Created by Mojgan on 2024-01-10. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import MullvadSettings | ||
|
||
class AccessMethodIterator { | ||
private var lastReachableApiAccessCache: LastReachableApiAccessCache | ||
private let dataSource: AccessMethodRepositoryDataSource | ||
|
||
private var cancellables = Set<Combine.AnyCancellable>() | ||
|
||
/// `UserDefaults` key shared by both processes. Used to cache and synchronize last reachable api access method between them. | ||
private let lastReachableConfigurationCacheKey = "LastReachableConfigurationCacheKey" | ||
|
||
private var lastReachableApiAccessId: UUID { | ||
lastReachableApiAccessCache.id | ||
} | ||
|
||
private var index = 0 | ||
private var configurations: [PersistentAccessMethod] = [] | ||
|
||
init(_ userDefaults: UserDefaults, dataSource: AccessMethodRepositoryDataSource) { | ||
self.dataSource = dataSource | ||
self.lastReachableApiAccessCache = LastReachableApiAccessCache( | ||
key: lastReachableConfigurationCacheKey, | ||
defaultValue: dataSource.directAccess.id, | ||
container: userDefaults | ||
) | ||
|
||
self.dataSource | ||
.publisher | ||
.sink { [weak self] newValue in | ||
guard let self else { return } | ||
self.configurations = newValue | ||
self.refreshCacheIfNeeded() | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
var current: PersistentAccessMethod { | ||
if enabledConfigurations.isEmpty { | ||
return dataSource.directAccess | ||
} else { | ||
let circularIndex = index % enabledConfigurations.count | ||
return enabledConfigurations[circularIndex] | ||
} | ||
} | ||
|
||
private var enabledConfigurations: [PersistentAccessMethod] { | ||
return configurations.filter { $0.isEnabled } | ||
} | ||
|
||
private func refreshCacheIfNeeded() { | ||
/// updating the cursor whenever the enabled configurations are updated | ||
guard let idx = self.enabledConfigurations.firstIndex(where: { | ||
$0.id == self.lastReachableApiAccessId | ||
}) else { | ||
self.lastReachableApiAccessCache.id = self.current.id | ||
return | ||
} | ||
self.index = idx | ||
} | ||
|
||
/// Picking the next `Enabled` configuration in order they are added | ||
func next() { | ||
if !enabledConfigurations.isEmpty { | ||
index += 1 | ||
lastReachableApiAccessCache.id = current.id | ||
} else { | ||
index = 0 | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
ios/MullvadREST/Transport/LastReachableApiAccessCache.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,32 @@ | ||
// | ||
// LastReachableApiAccessStorage.swift | ||
// MullvadREST | ||
// | ||
// Created by Mojgan on 2024-01-08. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadSettings | ||
|
||
struct LastReachableApiAccessCache: Identifiable { | ||
private var appStorage: AppStorage<String> | ||
|
||
init(key: String, defaultValue: UUID, container: UserDefaults) { | ||
self.appStorage = AppStorage( | ||
wrappedValue: defaultValue.uuidString, | ||
key: key, | ||
container: container | ||
) | ||
} | ||
|
||
var id: UUID { | ||
get { | ||
let value = appStorage.wrappedValue | ||
return UUID(uuidString: value)! | ||
} | ||
set { | ||
appStorage.wrappedValue = newValue.uuidString | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
ios/MullvadREST/Transport/Shadowsocks/ShadowsocksLoader.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,79 @@ | ||
// | ||
// LocalShadowsocksLoader.swift | ||
// MullvadREST | ||
// | ||
// Created by Mojgan on 2024-01-08. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadTypes | ||
|
||
public protocol ShadowsocksLoaderProtocol { | ||
var configuration: ShadowsocksConfiguration { get throws } | ||
func preloadNewConfiguration() throws | ||
} | ||
|
||
public class ShadowsocksLoader: ShadowsocksLoaderProtocol { | ||
public var configuration: ShadowsocksConfiguration { | ||
get throws { | ||
try load() | ||
} | ||
} | ||
|
||
private let shadowsocksCache: ShadowsocksConfigurationCache | ||
private let relayCache: RelayCacheProtocol | ||
private var relayConstraints = RelayConstraints() | ||
private let constraintsUpdater: RelayConstraintsUpdater | ||
|
||
public init( | ||
shadowsocksCache: ShadowsocksConfigurationCache, | ||
relayCache: RelayCacheProtocol, | ||
constraintsUpdater: RelayConstraintsUpdater | ||
) { | ||
self.shadowsocksCache = shadowsocksCache | ||
self.relayCache = relayCache | ||
self.constraintsUpdater = constraintsUpdater | ||
constraintsUpdater.onNewConstraints = { [weak self] newConstraints in | ||
self?.relayConstraints = newConstraints | ||
} | ||
} | ||
|
||
public func preloadNewConfiguration() throws { | ||
// because the previous shadowsocks configuration was invalid, therefore generate a new one. | ||
let newConfiguration = try create() | ||
try shadowsocksCache.write(newConfiguration) | ||
} | ||
|
||
/// Returns the last used shadowsocks configuration, otherwise a new randomized configuration. | ||
private func load() throws -> ShadowsocksConfiguration { | ||
do { | ||
// If a previous shadowsocks configuration was in cache, return it directly. | ||
return try shadowsocksCache.read() | ||
} catch { | ||
// There is no previous configuration either if this is the first time this code ran | ||
let newConfiguration = try create() | ||
try shadowsocksCache.write(newConfiguration) | ||
return newConfiguration | ||
} | ||
} | ||
|
||
/// Returns a randomly selected shadowsocks configuration. | ||
private func create() throws -> ShadowsocksConfiguration { | ||
let cachedRelays = try relayCache.read() | ||
let bridgeConfiguration = RelaySelector.shadowsocksTCPBridge(from: cachedRelays.relays) | ||
let closestRelay = RelaySelector.closestShadowsocksRelayConstrained( | ||
by: relayConstraints, | ||
in: cachedRelays.relays | ||
) | ||
|
||
guard let bridgeAddress = closestRelay?.ipv4AddrIn, let bridgeConfiguration else { throw POSIXError(.ENOENT) } | ||
|
||
return ShadowsocksConfiguration( | ||
address: .ipv4(bridgeAddress), | ||
port: bridgeConfiguration.port, | ||
password: bridgeConfiguration.password, | ||
cipher: bridgeConfiguration.cipher | ||
) | ||
} | ||
} |
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.