From b38a5df0c29efbf4a9391c1845bbb645a43bfbac Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 24 Jul 2020 16:47:51 +1000 Subject: [PATCH] Debug --- SignalServiceKit/src/Loki/API/DotNetAPI.swift | 6 ++++-- SignalServiceKit/src/Loki/API/FileServerAPI.swift | 2 +- .../src/Loki/API/Onion Requests/OnionRequestAPI.swift | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/DotNetAPI.swift b/SignalServiceKit/src/Loki/API/DotNetAPI.swift index 7a3bd0cee..cda061895 100644 --- a/SignalServiceKit/src/Loki/API/DotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/DotNetAPI.swift @@ -68,7 +68,8 @@ public class DotNetAPI : NSObject { let queryParameters = "pubKey=\(getUserHexEncodedPublicKey())" let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")! let request = TSRequest(url: url) - let serverPublicKeyPromise = (server == FileServerAPI.server) ? Promise { $0.fulfill(server) } : PublicChatAPI.getOpenGroupServerPublicKey(for: server) + let serverPublicKeyPromise = (server == FileServerAPI.server) ? Promise { $0.fulfill(FileServerAPI.fileServerPublicKey) } + : PublicChatAPI.getOpenGroupServerPublicKey(for: server) return serverPublicKeyPromise.then2 { serverPublicKey in OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey) }.map2 { rawResponse in @@ -95,7 +96,8 @@ public class DotNetAPI : NSObject { let url = URL(string: "\(server)/loki/v1/submit_challenge")! let parameters = [ "pubKey" : getUserHexEncodedPublicKey(), "token" : token ] let request = TSRequest(url: url, method: "POST", parameters: parameters) - let serverPublicKeyPromise = (server == FileServerAPI.server) ? Promise { $0.fulfill(server) } : PublicChatAPI.getOpenGroupServerPublicKey(for: server) + let serverPublicKeyPromise = (server == FileServerAPI.server) ? Promise { $0.fulfill(FileServerAPI.fileServerPublicKey) } + : PublicChatAPI.getOpenGroupServerPublicKey(for: server) return serverPublicKeyPromise.then2 { serverPublicKey in OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey) }.map2 { _ in token } diff --git a/SignalServiceKit/src/Loki/API/FileServerAPI.swift b/SignalServiceKit/src/Loki/API/FileServerAPI.swift index e2652f7dd..288ee5712 100644 --- a/SignalServiceKit/src/Loki/API/FileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/FileServerAPI.swift @@ -112,7 +112,7 @@ public final class FileServerAPI : DotNetAPI { return attempt(maxRetryCount: 8, recoveringOn: SnodeAPI.workQueue) { OnionRequestAPI.sendOnionRequest(request, to: server, using: fileServerPublicKey).map2 { _ in } }.handlingInvalidAuthTokenIfNeeded(for: server).recover2 { error in - print("Couldn't update device links due to error: \(error).") + print("[Loki] Couldn't update device links due to error: \(error).") throw error } } diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift index 2a8e8fe09..f41c2b7b8 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift @@ -307,11 +307,11 @@ public enum OnionRequestAPI { let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .noPadding) let data = Data(try aes.decrypt(ciphertext.bytes)) guard let json = try JSONSerialization.jsonObject(with: data, options: [ .fragmentsAllowed ]) as? JSON, - let bodyAsString = json["body"] as? String, let statusCode = json["status"] as? Int else { return seal.reject(HTTP.Error.invalidJSON) } + let statusCode = json["status"] as? Int else { return seal.reject(HTTP.Error.invalidJSON) } if statusCode == 406 { // Clock out of sync print("[Loki] The user's clock is out of sync with the service node network.") seal.reject(SnodeAPI.SnodeAPIError.clockOutOfSync) - } else { + } else if let bodyAsString = json["body"] as? String { let body: JSON if !isJSONRequired { body = [ "result" : bodyAsString ] @@ -322,6 +322,9 @@ public enum OnionRequestAPI { } guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: body)) } seal.fulfill(body) + } else { + guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: json)) } + seal.fulfill(json) } } catch (let error) { seal.reject(error)