mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Merge pull request #145 from loki-project/swarm-api
Use Only Updated Snodes for File Server Proxying
This commit is contained in:
commit
e55e0784e3
|
@ -219,12 +219,12 @@ private extension DeviceLinksVC {
|
|||
stackView.axis = .vertical
|
||||
stackView.distribution = .equalCentering
|
||||
stackView.spacing = Values.verySmallSpacing
|
||||
stackView.set(.height, to: 36)
|
||||
stackView.set(.height, to: 44)
|
||||
contentView.addSubview(stackView)
|
||||
stackView.pin(.leading, to: .leading, of: contentView, withInset: Values.largeSpacing)
|
||||
stackView.pin(.top, to: .top, of: contentView, withInset: Values.mediumSpacing)
|
||||
stackView.pin(.top, to: .top, of: contentView, withInset: 12)
|
||||
contentView.pin(.trailing, to: .trailing, of: stackView, withInset: Values.largeSpacing)
|
||||
contentView.pin(.bottom, to: .bottom, of: stackView, withInset: Values.mediumSpacing)
|
||||
contentView.pin(.bottom, to: .bottom, of: stackView, withInset: 12)
|
||||
stackView.set(.width, to: UIScreen.main.bounds.width - 2 * Values.largeSpacing)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import PromiseKit
|
||||
|
||||
public extension LokiAPI {
|
||||
private static var snodeVersion: [LokiAPITarget:String] = [:]
|
||||
|
||||
/// Only ever accessed from `LokiAPI.errorHandlingQueue` to avoid race conditions.
|
||||
fileprivate static var failureCount: [LokiAPITarget:UInt] = [:]
|
||||
|
||||
|
||||
// MARK: Settings
|
||||
private static let minimumSnodeCount = 2
|
||||
private static let targetSnodeCount = 3
|
||||
private static let maxRandomSnodePoolSize = 1024
|
||||
fileprivate static let failureThreshold = 2
|
||||
|
||||
// MARK: Caching
|
||||
|
@ -37,10 +37,9 @@ public extension LokiAPI {
|
|||
let target = seedNodePool.randomElement()!
|
||||
let url = URL(string: "\(target)/json_rpc")!
|
||||
let request = TSRequest(url: url, method: "POST", parameters: [
|
||||
"method" : "get_n_service_nodes",
|
||||
"method" : "get_service_nodes",
|
||||
"params" : [
|
||||
"active_only" : true,
|
||||
"limit" : maxRandomSnodePoolSize,
|
||||
"fields" : [
|
||||
"public_ip" : true,
|
||||
"storage_port" : true,
|
||||
|
@ -84,11 +83,45 @@ public extension LokiAPI {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: Public API
|
||||
internal static func getTargetSnodes(for hexEncodedPublicKey: String) -> Promise<[LokiAPITarget]> {
|
||||
// shuffled() uses the system's default random generator, which is cryptographically secure
|
||||
return getSwarm(for: hexEncodedPublicKey).map { Array($0.shuffled().prefix(targetSnodeCount)) }
|
||||
}
|
||||
|
||||
internal static func getFileServerProxy() -> Promise<LokiAPITarget> {
|
||||
// All of this has to happen on DispatchQueue.global() due to the way OWSMessageManager works
|
||||
let (promise, seal) = Promise<LokiAPITarget>.pending()
|
||||
func getVersion(for snode: LokiAPITarget) -> Promise<String> {
|
||||
let url = URL(string: "\(snode.address):\(snode.port)/get_stats/v1")!
|
||||
let request = TSRequest(url: url)
|
||||
if let version = snodeVersion[snode] {
|
||||
return Promise { $0.fulfill(version) }
|
||||
} else {
|
||||
return TSNetworkManager.shared().perform(request, withCompletionQueue: DispatchQueue.global()).map(on: DispatchQueue.global()) { intermediate in
|
||||
let rawResponse = intermediate.responseObject
|
||||
guard let json = rawResponse as? JSON, let version = json["version"] as? String else { throw LokiAPIError.missingSnodeVersion }
|
||||
snodeVersion[snode] = version
|
||||
return version
|
||||
}
|
||||
}
|
||||
}
|
||||
getRandomSnode().done(on: DispatchQueue.global()) { snode in
|
||||
getVersion(for: snode).then(on: DispatchQueue.global()) { version -> Promise<LokiAPITarget> in
|
||||
if version >= "2.0.2" {
|
||||
print("[Loki] Using file server proxy with version number \(version).")
|
||||
return Promise { $0.fulfill(snode) }
|
||||
} else {
|
||||
print("[Loki] Rejecting file server proxy with version number \(version).")
|
||||
return getFileServerProxy()
|
||||
}
|
||||
}.recover(on: DispatchQueue.global()) { error in
|
||||
return getFileServerProxy()
|
||||
}
|
||||
}.catch(on: DispatchQueue.global()) { error in
|
||||
seal.reject(error)
|
||||
}
|
||||
return promise
|
||||
}
|
||||
|
||||
// MARK: Parsing
|
||||
private static func parseTargets(from rawResponse: Any) -> [LokiAPITarget] {
|
||||
|
|
|
@ -47,6 +47,7 @@ public final class LokiAPI : NSObject {
|
|||
@objc public static let messageConversionFailed = LokiAPIError(domain: "LokiAPIErrorDomain", code: 2, userInfo: [ NSLocalizedDescriptionKey : "Failed to construct message." ])
|
||||
@objc public static let clockOutOfSync = LokiAPIError(domain: "LokiAPIErrorDomain", code: 3, userInfo: [ NSLocalizedDescriptionKey : "Your clock is out of sync with the service node network." ])
|
||||
@objc public static let randomSnodePoolUpdatingFailed = LokiAPIError(domain: "LokiAPIErrorDomain", code: 4, userInfo: [ NSLocalizedDescriptionKey : "Failed to update random service node pool." ])
|
||||
@objc public static let missingSnodeVersion = LokiAPIError(domain: "LokiAPIErrorDomain", code: 5, userInfo: [ NSLocalizedDescriptionKey : "Missing service node version." ])
|
||||
}
|
||||
|
||||
@objc(LKDestination)
|
||||
|
|
|
@ -50,7 +50,7 @@ internal class LokiFileServerProxy : LokiHTTPClient {
|
|||
DispatchQueue.global().async {
|
||||
let uncheckedSymmetricKey = try? Curve25519.generateSharedSecret(fromPublicKey: LokiFileServerProxy.fileServerPublicKey, privateKey: keyPair.privateKey)
|
||||
guard let symmetricKey = uncheckedSymmetricKey else { return seal.reject(Error.symmetricKeyGenerationFailed) }
|
||||
LokiAPI.getRandomSnode().then(on: DispatchQueue.global()) { proxy -> Promise<Any> in
|
||||
LokiAPI.getFileServerProxy().then(on: DispatchQueue.global()) { proxy -> Promise<Any> in
|
||||
let url = "\(proxy.address):\(proxy.port)/file_proxy"
|
||||
guard let urlAsString = request.url?.absoluteString, let serverURLEndIndex = urlAsString.range(of: server)?.upperBound,
|
||||
serverURLEndIndex < urlAsString.endIndex else { throw Error.endpointParsingFailed }
|
||||
|
|
|
@ -194,6 +194,9 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
|
|||
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
|
||||
sessionManager.requestSerializer.HTTPShouldHandleCookies = NO;
|
||||
sessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
|
||||
NSMutableSet<NSString *> *acceptableContentTypes = sessionManager.responseSerializer.acceptableContentTypes.mutableCopy;
|
||||
[acceptableContentTypes addObject:@"text/plain"];
|
||||
sessionManager.responseSerializer.acceptableContentTypes = acceptableContentTypes;
|
||||
return sessionManager;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue