From dcdf603a19a4d837c7bb4c82d5a6b23ac63ed153 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Tue, 10 Nov 2020 14:43:02 +1100 Subject: [PATCH] use v3/lsrpc endpoint for more compact (base64) file downloads --- SignalServiceKit/src/Loki/API/DotNetAPI.swift | 4 ++-- .../API/Onion Requests/OnionRequestAPI+Encryption.swift | 8 ++++---- .../src/Loki/API/Onion Requests/OnionRequestAPI.swift | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/DotNetAPI.swift b/SignalServiceKit/src/Loki/API/DotNetAPI.swift index 7fc906bfd..10ac7ce12 100644 --- a/SignalServiceKit/src/Loki/API/DotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/DotNetAPI.swift @@ -127,11 +127,11 @@ public class DotNetAPI : NSObject { return attempt(maxRetryCount: maxRetryCount, recoveringOn: SnodeAPI.workQueue) { serverPublicKeyPromise.then2 { serverPublicKey in return OnionRequestAPI.sendOnionRequest(request, to: host, using: serverPublicKey, isJSONRequired: false).map2 { json in - guard let body = json["body"] as? JSON, let data = body["data"] as? [UInt8] else { + guard let body = json["result"] as? String, let data = Data(base64Encoded: body) else { print("[Loki] Couldn't parse attachment from: \(json).") throw DotNetAPIError.parsingFailed } - return Data(data) + return data } } } diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI+Encryption.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI+Encryption.swift index 84a8da122..04ac04160 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI+Encryption.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI+Encryption.swift @@ -26,7 +26,7 @@ extension OnionRequestAPI { let plaintext = try encode(ciphertext: payloadAsData, json: [ "headers" : "" ]) let result = try EncryptionUtilities.encrypt(plaintext, using: snodeX25519PublicKey) seal.fulfill(result) - case .server(_, let serverX25519PublicKey): + case .server(_, _, let serverX25519PublicKey): let plaintext = try JSONSerialization.data(withJSONObject: payload, options: [ .fragmentsAllowed ]) let result = try EncryptionUtilities.encrypt(plaintext, using: serverX25519PublicKey) seal.fulfill(result) @@ -47,8 +47,8 @@ extension OnionRequestAPI { case .snode(let snode): guard let snodeED25519PublicKey = snode.publicKeySet?.ed25519Key else { return seal.reject(Error.snodePublicKeySetMissing) } parameters = [ "destination" : snodeED25519PublicKey ] - case .server(let host, _): - parameters = [ "host" : host, "target" : "/loki/v2/lsrpc", "method" : "POST" ] + case .server(let host, let target, _): + parameters = [ "host" : host, "target" : target, "method" : "POST" ] } parameters["ephemeral_key"] = previousEncryptionResult.ephemeralPublicKey.toHexString() let x25519PublicKey: String @@ -56,7 +56,7 @@ extension OnionRequestAPI { case .snode(let snode): guard let snodeX25519PublicKey = snode.publicKeySet?.x25519Key else { return seal.reject(Error.snodePublicKeySetMissing) } x25519PublicKey = snodeX25519PublicKey - case .server(_, let serverX25519PublicKey): + case .server(_, _, let serverX25519PublicKey): x25519PublicKey = serverX25519PublicKey } do { diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift index 66432ef61..4b16a3b33 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift @@ -24,7 +24,7 @@ public enum OnionRequestAPI { // MARK: Destination internal enum Destination { case snode(Snode) - case server(host: String, x25519PublicKey: String) + case server(host: String, target: String, x25519PublicKey: String) } // MARK: Error @@ -280,7 +280,7 @@ public enum OnionRequestAPI { } /// Sends an onion request to `server`. Builds new paths as needed. - internal static func sendOnionRequest(_ request: NSURLRequest, to server: String, using x25519PublicKey: String, isJSONRequired: Bool = true) -> Promise { + internal static func sendOnionRequest(_ request: NSURLRequest, to server: String, target: String = "/loki/v3/lsrpc", using x25519PublicKey: String, isJSONRequired: Bool = true) -> Promise { var rawHeaders = request.allHTTPHeaderFields ?? [:] rawHeaders.removeValue(forKey: "User-Agent") var headers: JSON = rawHeaders.mapValues { value in @@ -323,7 +323,7 @@ public enum OnionRequestAPI { "method" : request.httpMethod!, "headers" : headers ] - let destination = Destination.server(host: host, x25519PublicKey: x25519PublicKey) + let destination = Destination.server(host: host, target: target, x25519PublicKey: x25519PublicKey) let promise = sendOnionRequest(with: payload, to: destination, isJSONRequired: isJSONRequired) promise.catch2 { error in print("[Loki] [Onion Request API] Couldn't reach server: \(url) due to error: \(error).")