use v3/lsrpc endpoint for more compact (base64) file downloads

This commit is contained in:
Ryan ZHAO 2020-11-10 14:43:02 +11:00
parent a46630c192
commit dcdf603a19
3 changed files with 9 additions and 9 deletions

View File

@ -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
}
}
}

View File

@ -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 {

View File

@ -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<JSON> {
internal static func sendOnionRequest(_ request: NSURLRequest, to server: String, target: String = "/loki/v3/lsrpc", using x25519PublicKey: String, isJSONRequired: Bool = true) -> Promise<JSON> {
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).")