pull/81/head
Niels Andriesse 5 years ago
parent 340c30e2b5
commit 2dc1a44087

@ -24,7 +24,7 @@ internal class LokiFileServerProxy : LokiHTTPClient {
case .symmetricKeyGenerationFailed: return "Couldn't generate symmetric key." case .symmetricKeyGenerationFailed: return "Couldn't generate symmetric key."
case .endpointParsingFailed: return "Couldn't parse endpoint." case .endpointParsingFailed: return "Couldn't parse endpoint."
case .proxyResponseParsingFailed: return "Couldn't parse proxy response." case .proxyResponseParsingFailed: return "Couldn't parse proxy response."
case .fileServerHTTPError(let httpStatusCode, let message): return "File server returned error \(httpStatusCode) with description: \(message ?? "no description provided.")." case .fileServerHTTPError(let httpStatusCode, let message): return "File server returned \(httpStatusCode) with description: \(message ?? "no description provided.")."
} }
} }
} }
@ -87,42 +87,17 @@ internal class LokiFileServerProxy : LokiHTTPClient {
task.resume() task.resume()
return promise return promise
}.map { rawResponse in }.map { rawResponse in
guard let data = rawResponse as? Data, !data.isEmpty else { guard let responseAsData = rawResponse as? Data, let responseAsJSON = try? JSONSerialization.jsonObject(with: responseAsData, options: .allowFragments) as? JSON, let base64EncodedCipherText = responseAsJSON["data"] as? String,
print("[Loki] Received an empty response.") let meta = responseAsJSON["meta"] as? JSON, let statusCode = meta["code"] as? Int, let cipherText = Data(base64Encoded: base64EncodedCipherText) else {
return rawResponse
}
var uncheckedStatusCode: Int? = nil
let uncheckedCipherText: Data?
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON, let base64EncodedData = json["data"] as? String {
if let meta = json["meta"] as? JSON { uncheckedStatusCode = meta["code"] as? Int }
uncheckedCipherText = Data(base64Encoded: base64EncodedData)
} else {
uncheckedCipherText = data
}
guard let cipherText = uncheckedCipherText else {
print("[Loki] Received an invalid response.")
throw Error.proxyResponseParsingFailed
}
let response = try DiffieHellman.decrypt(cipherText, using: symmetricKey)
let uncheckedJSON = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? JSON
guard let json = uncheckedJSON else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) }
if uncheckedStatusCode == nil {
uncheckedStatusCode = json["status"] as? Int
}
guard let statusCode = uncheckedStatusCode else {
print("[Loki] Received an invalid response.") print("[Loki] Received an invalid response.")
throw Error.proxyResponseParsingFailed throw Error.proxyResponseParsingFailed
} }
let isSuccess = (200..<300).contains(statusCode) let isSuccess = (200..<300).contains(statusCode)
var body: Any? = nil guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: nil, underlyingError: Error.fileServerHTTPError(code: statusCode, message: nil)) }
if let bodyAsString = json["body"] as? String { let uncheckedJSONAsData = try DiffieHellman.decrypt(cipherText, using: symmetricKey)
body = bodyAsString let uncheckedJSON = try? JSONSerialization.jsonObject(with: uncheckedJSONAsData, options: .allowFragments) as? JSON
if let bodyAsJSON = try? JSONSerialization.jsonObject(with: bodyAsString.data(using: .utf8)!, options: .allowFragments) as? JSON { guard let json = uncheckedJSON else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) }
body = bodyAsJSON return json
}
}
guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: body, underlyingError: Error.fileServerHTTPError(code: statusCode, message: body)) }
return body ?? response
}.recover { error -> Promise<Any> in }.recover { error -> Promise<Any> in
print("[Loki] File server proxy request failed with error: \(error.localizedDescription).") print("[Loki] File server proxy request failed with error: \(error.localizedDescription).")
throw HTTPError.from(error: error) ?? error throw HTTPError.from(error: error) ?? error

@ -67,14 +67,14 @@ internal class LokiSnodeProxy : LokiHTTPClient {
task.resume() task.resume()
return promise return promise
}.map { rawResponse in }.map { rawResponse in
guard let data = rawResponse as? Data, !data.isEmpty, let cipherText = Data(base64Encoded: data) else { guard let responseAsData = rawResponse as? Data, let cipherText = Data(base64Encoded: responseAsData) else {
print("[Loki] Received a non-string encoded response.") print("[Loki] Received a non-string encoded response.")
return rawResponse return rawResponse
} }
let response = try DiffieHellman.decrypt(cipherText, using: symmetricKey) let response = try DiffieHellman.decrypt(cipherText, using: symmetricKey)
let uncheckedJSON = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? JSON let uncheckedJSON = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? JSON
guard let json = uncheckedJSON, let httpStatusCode = json["status"] as? Int else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) } guard let json = uncheckedJSON, let statusCode = json["status"] as? Int else { throw HTTPError.networkError(code: -1, response: nil, underlyingError: Error.proxyResponseParsingFailed) }
let isSuccess = (200..<300).contains(httpStatusCode) let isSuccess = (200..<300).contains(statusCode)
var body: Any? = nil var body: Any? = nil
if let bodyAsString = json["body"] as? String { if let bodyAsString = json["body"] as? String {
body = bodyAsString body = bodyAsString
@ -82,7 +82,7 @@ internal class LokiSnodeProxy : LokiHTTPClient {
body = bodyAsJSON body = bodyAsJSON
} }
} }
guard isSuccess else { throw HTTPError.networkError(code: httpStatusCode, response: body, underlyingError: Error.targetSnodeHTTPError(code: httpStatusCode, message: body)) } guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: body, underlyingError: Error.targetSnodeHTTPError(code: statusCode, message: body)) }
return body return body
}.recover { error -> Promise<Any> in }.recover { error -> Promise<Any> in
print("[Loki] Proxy request failed with error: \(error.localizedDescription).") print("[Loki] Proxy request failed with error: \(error.localizedDescription).")

Loading…
Cancel
Save