From c7e54c4257fc88c7c806200aee9673c7137aa76e Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 8 Oct 2020 15:36:57 +1100 Subject: [PATCH] make attachment download use onion routing --- js/modules/loki_app_dot_net_api.js | 8 ++++++++ js/modules/loki_file_server_api.d.ts | 1 + js/modules/loki_file_server_api.js | 5 +++++ libtextsecure/sendmessage.js | 2 +- preload.js | 2 +- ts/receiver/attachments.ts | 9 ++------- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index ca78976a1..923d57daf 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -1240,6 +1240,14 @@ class LokiAppDotNetServerAPI { }); return this.uploadAvatar(formData); } + + async downloadAttachment(url) { + const endpoint = new URL(url).pathname; + + return this.serverRequest(`loki/v1${endpoint}`, { + method: 'GET', + }); + } } // functions to a specific ADN channel on an ADN server diff --git a/js/modules/loki_file_server_api.d.ts b/js/modules/loki_file_server_api.d.ts index f90fec127..9b92f3576 100644 --- a/js/modules/loki_file_server_api.d.ts +++ b/js/modules/loki_file_server_api.d.ts @@ -13,4 +13,5 @@ interface DeviceMappingAnnotation { interface LokiFileServerInstance { getUserDeviceMapping(pubKey: string): Promise; clearOurDeviceMappingAnnotations(): Promise; + downloadAttachment(url: string): Promise; } diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js index 9287875c9..65d281434 100644 --- a/js/modules/loki_file_server_api.js +++ b/js/modules/loki_file_server_api.js @@ -198,6 +198,11 @@ class LokiFileServerInstance { result.slaveMap = newSlavePrimaryMap; return result; } + + // for files + async downloadAttachment(url) { + return this._server.downloadAttachment(url); + } } // extends LokiFileServerInstance with functions we'd only perform on our own home server diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 0364b5b17..6e1e38975 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -185,8 +185,8 @@ Message.prototype = { }; function MessageSender() { + // Currently only used for getProxiedSize() and makeProxiedRequest(), which are only used for fetching previews this.server = WebAPI.connect(); - this.pendingMessages = {}; } MessageSender.prototype = { diff --git a/preload.js b/preload.js index 86eca4ccc..3a8645236 100644 --- a/preload.js +++ b/preload.js @@ -47,8 +47,8 @@ window.getCommitHash = () => config.commitHash; window.getNodeVersion = () => config.node_version; window.getHostName = () => config.hostname; window.getServerTrustRoot = () => config.serverTrustRoot; -window.isBehindProxy = () => Boolean(config.proxyUrl); window.JobQueue = JobQueue; +window.isBehindProxy = () => Boolean(config.proxyUrl); window.getStoragePubKey = key => window.isDev() ? key.substring(0, key.length - 2) : key; window.getDefaultFileServer = () => config.defaultFileServer; diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index bf71c795a..c326f5227 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -3,16 +3,11 @@ import _ from 'lodash'; import * as Data from '../../js/modules/data'; -// TODO: Might convert it to a class later -let webAPI: any; - export async function downloadAttachment(attachment: any) { - if (!webAPI) { - webAPI = window.WebAPI.connect(); - } + const res = await window.lokiFileServerAPI.downloadAttachment(attachment.url); // The attachment id is actually just the absolute url of the attachment - let data = await webAPI.getAttachment(attachment.url); + let data = new Uint8Array(res.response.data).buffer; if (!attachment.isRaw) { const { key, digest, size } = attachment;