-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using various access methods due a network error
- Loading branch information
Showing
24 changed files
with
656 additions
and
443 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,75 @@ | ||
// | ||
// 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 index = 0 | ||
private var enabledConfigurations: [PersistentAccessMethod] = [] | ||
|
||
private var lastReachableApiAccessId: UUID { | ||
lastReachableApiAccessCache.id | ||
} | ||
|
||
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] configurations in | ||
guard let self else { return } | ||
self.enabledConfigurations = configurations.filter { $0.isEnabled } | ||
self.refreshCacheIfNeeded() | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
var current: PersistentAccessMethod { | ||
if enabledConfigurations.isEmpty { | ||
/// Returning `Default` strategy when all is disabled | ||
return dataSource.directAccess | ||
} else { | ||
/// Picking the next `Enabled` configuration in order they are added | ||
/// And starting from the beginning when it reaches end | ||
let circularIndex = index % enabledConfigurations.count | ||
return enabledConfigurations[circularIndex] | ||
} | ||
} | ||
|
||
private func refreshCacheIfNeeded() { | ||
/// Validating the index of `lastReachableApiAccessCache` after any changes in `AccessMethodRepository` | ||
if let idx = enabledConfigurations.firstIndex(where: { $0.id == self.lastReachableApiAccessId }) { | ||
index = idx | ||
} else { | ||
/// When `idx` is `nil`, that means the current configuration is not valid any more | ||
/// Invalidating cache by replacing the `current` to the next enabled access method | ||
lastReachableApiAccessCache.id = current.id | ||
} | ||
} | ||
|
||
func next() { | ||
let (partial, isOverflow) = index.addingReportingOverflow(1) | ||
index = isOverflow ? 0 : partial | ||
lastReachableApiAccessCache.id = current.id | ||
} | ||
} |
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 { | ||
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 var configuration: ShadowsocksConfiguration { | ||
get throws { | ||
try load() | ||
} | ||
} | ||
|
||
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.