From 3d70a6dc662d1c3f6f94551620f681e6cd5b55f1 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 30 Jan 2020 18:05:19 -0800 Subject: [PATCH 1/2] make sure token comms are done over fileProxy, other notes, logging adjustment --- js/modules/loki_app_dot_net_api.js | 60 +++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 0831a4028..ba6ae7b1e 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -1,4 +1,4 @@ -/* global log, textsecure, libloki, Signal, Whisper, Headers, ConversationController, +/* global log, textsecure, libloki, Signal, Whisper, ConversationController, clearTimeout, MessageController, libsignal, StringView, window, _, dcodeIO, Buffer, lokiSnodeAPI, TextDecoder */ const nodeFetch = require('node-fetch'); @@ -44,15 +44,24 @@ class LokiAppDotNetServerAPI { if (!thisChannel) { // make sure we're subscribed // eventually we'll need to move to account registration/add server - await this.serverRequest(`channels/${channelId}/subscribe`, { - method: 'POST', - }); + await this.serverRequest( + `channels/${channelId}/subscribe`, + { + method: 'POST', + } + ); thisChannel = new LokiPublicChannelAPI( chatAPI, this, channelId, conversationId ); + log.info( + 'LokiPublicChannelAPI started for', + channelId, + 'on', + this.baseServerUrl + ); this.channels.push(thisChannel); } return thisChannel; @@ -220,9 +229,11 @@ class LokiAppDotNetServerAPI { async refreshServerToken() { // if currently not in progress if (this.tokenPromise === null) { + // FIXME: add timeout + // a broken/stuck token endpoint can prevent you from removing channels // set lock this.tokenPromise = new Promise(async res => { - // request the oken + // request the token const token = await this.requestToken(); if (!token) { res(null); @@ -255,11 +266,13 @@ class LokiAppDotNetServerAPI { }; url.search = new URLSearchParams(params); - res = await nodeFetch(url); + res = await this.proxyFetch(url); } catch (e) { + log.error('requestToken request failed', e); return null; } if (!res.ok) { + log.error('requestToken request failed'); return null; } const body = await res.json(); @@ -281,7 +294,7 @@ class LokiAppDotNetServerAPI { }; try { - const res = await nodeFetch( + const res = await this.proxyFetch( `${this.baseServerUrl}/loki/v1/submit_challenge`, options ); @@ -291,6 +304,33 @@ class LokiAppDotNetServerAPI { } } + async proxyFetch(urlObj, fetchOptions) { + if ( + window.lokiFeatureFlags.useSnodeProxy && + (this.baseServerUrl === 'https://file-dev.lokinet.org' || + this.baseServerUrl === 'https://file.lokinet.org') + ) { + const finalOptions = {...fetchOptions} + if (!fetchOptions.method) { + finalOptions.method = 'GET'; + } + const urlStr = urlObj.toString(); + const endpoint = urlStr.replace(`${this.baseServerUrl}/`, ''); + const { response, result } = await this._sendToProxy( + finalOptions, + finalOptions.method, + finalOptions.headers, + endpoint + ); + // emulate nodeFetch response... + return { + ok: result.status === 200, + json: () => response, + }; + } + return nodeFetch(urlObj, fetchOptions); + } + async _sendToProxy(fetchOptions, method, headers, endpoint) { const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`; @@ -419,7 +459,7 @@ class LokiAppDotNetServerAPI { } else if (rawBody) { fetchOptions.body = rawBody; } - fetchOptions.headers = new Headers(headers); + fetchOptions.headers = headers; } catch (e) { log.info('serverRequest set up error:', JSON.stringify(e)); return { @@ -446,7 +486,7 @@ class LokiAppDotNetServerAPI { endpoint )); } else { - result = await nodeFetch(url, fetchOptions || undefined); + result = await nodeFetch(url, fetchOptions); txtResponse = await result.text(); response = JSON.parse(txtResponse); } @@ -714,7 +754,7 @@ class LokiAppDotNetServerAPI { options ); if (statusCode !== 200) { - log.warn('Failed to upload data to fileserver'); + log.warn('Failed to upload data to server', this.baseServerUrl); return null; } From 18065c101ba8a0df73498509924a9b45f0b4957e Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 30 Jan 2020 18:26:43 -0800 Subject: [PATCH 2/2] minor refactor --- js/modules/loki_app_dot_net_api.js | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index e5f4207fb..eee538a1b 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -44,12 +44,9 @@ class LokiAppDotNetServerAPI { if (!thisChannel) { // make sure we're subscribed // eventually we'll need to move to account registration/add server - await this.serverRequest( - `channels/${channelId}/subscribe`, - { - method: 'POST', - } - ); + await this.serverRequest(`channels/${channelId}/subscribe`, { + method: 'POST', + }); thisChannel = new LokiPublicChannelAPI( chatAPI, this, @@ -310,17 +307,15 @@ class LokiAppDotNetServerAPI { (this.baseServerUrl === 'https://file-dev.lokinet.org' || this.baseServerUrl === 'https://file.lokinet.org') ) { - const finalOptions = {...fetchOptions} + const finalOptions = { ...fetchOptions }; if (!fetchOptions.method) { finalOptions.method = 'GET'; } const urlStr = urlObj.toString(); const endpoint = urlStr.replace(`${this.baseServerUrl}/`, ''); const { response, result } = await this._sendToProxy( - finalOptions, - finalOptions.method, - finalOptions.headers, - endpoint + endpoint, + finalOptions ); // emulate nodeFetch response... return { @@ -331,7 +326,7 @@ class LokiAppDotNetServerAPI { return nodeFetch(urlObj, fetchOptions); } - async _sendToProxy(fetchOptions, method, headers, endpoint) { + async _sendToProxy(endpoint, fetchOptions) { const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`; @@ -339,8 +334,8 @@ class LokiAppDotNetServerAPI { // I think this is a stream, we may need to collect it all? body: fetchOptions.body, // might need to b64 if binary... endpoint, - method, - headers, + method: fetchOptions.method, + headers: fetchOptions.headers, }; // from https://github.com/sindresorhus/is-stream/blob/master/index.js @@ -478,12 +473,9 @@ class LokiAppDotNetServerAPI { this.baseServerUrl === 'https://file.lokinet.org') ) { mode = '_sendToProxy'; - // have to send headers because fetchOptions.headers isn't readable ({ response, txtResponse, result } = await this._sendToProxy( - fetchOptions, - method, - headers, - endpoint + endpoint, + fetchOptions )); } else { result = await nodeFetch(url, fetchOptions);