From 11e6c1175bf15ed116f33a90f4def54994e9f60a Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 20 May 2021 10:58:52 +1000 Subject: [PATCH] Switch to dedicated server --- .../messaging/file_server/FileServerAPIV2.kt | 20 +++++++++++-------- .../libsession/utilities/DownloadUtilities.kt | 5 +++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt index bb3301b8cf..57cfbb6534 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt @@ -15,8 +15,10 @@ import org.session.libsignal.utilities.Log object FileServerAPIV2 { - private const val SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69" - const val SERVER = "http://88.99.175.227" + private const val OLD_SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69" + const val OLD_SERVER = "http://88.99.175.227" + private const val SERVER_PUBLIC_KEY = "da21e1d886c6fbaea313f75298bd64aab03a97ce985b46bb2dad9f2089c8ee59" + const val SERVER = "http://filev2.getsession.org" sealed class Error(message: String) : Exception(message) { object ParsingFailed : Error("Invalid response.") @@ -42,8 +44,10 @@ object FileServerAPIV2 { return RequestBody.create(MediaType.get("application/json"), parametersAsJSON) } - private fun send(request: Request): Promise, Exception> { - val url = HttpUrl.parse(SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL) + private fun send(request: Request, useOldServer: Boolean): Promise, Exception> { + val server = if (useOldServer) OLD_SERVER else SERVER + val serverPublicKey = if (useOldServer) OLD_SERVER_PUBLIC_KEY else SERVER_PUBLIC_KEY + val url = HttpUrl.parse(server) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL) val urlBuilder = HttpUrl.Builder() .scheme(url.scheme()) .host(url.host()) @@ -64,7 +68,7 @@ object FileServerAPIV2 { HTTP.Verb.DELETE -> requestBuilder.delete(createBody(request.parameters)) } if (request.useOnionRouting) { - return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), SERVER, SERVER_PUBLIC_KEY).fail { e -> + return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), server, serverPublicKey).fail { e -> Log.e("Loki", "File server request failed.", e) } } else { @@ -76,14 +80,14 @@ object FileServerAPIV2 { val base64EncodedFile = Base64.encodeBytes(file) val parameters = mapOf( "file" to base64EncodedFile ) val request = Request(verb = HTTP.Verb.POST, endpoint = "files", parameters = parameters) - return send(request).map { json -> + return send(request, false).map { json -> json["result"] as? Long ?: throw OpenGroupAPIV2.Error.ParsingFailed } } - fun download(file: Long): Promise { + fun download(file: Long, useOldServer: Boolean): Promise { val request = Request(verb = HTTP.Verb.GET, endpoint = "files/$file") - return send(request).map { json -> + return send(request, useOldServer).map { json -> val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed } diff --git a/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt b/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt index 48bcc89ba1..9aee38647e 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt @@ -41,11 +41,12 @@ object DownloadUtilities { @JvmStatic fun downloadFile(outputStream: OutputStream, url: String, maxSize: Int, listener: SignalServiceAttachment.ProgressListener?) { - if (url.contains(FileServerAPIV2.SERVER)) { + if (url.contains(FileServerAPIV2.SERVER) || url.contains(FileServerAPIV2.OLD_SERVER)) { val httpUrl = HttpUrl.parse(url)!! val fileId = httpUrl.pathSegments().last() + val useOldServer = url.contains(FileServerAPIV2.OLD_SERVER) try { - FileServerAPIV2.download(fileId.toLong()).get().let { + FileServerAPIV2.download(fileId.toLong(), useOldServer).get().let { outputStream.write(it) } } catch (e: Exception) {