From 47e87a4720879862e60374ef7aca78b7f210b70a Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 6 Feb 2020 16:05:11 -0800 Subject: [PATCH 1/5] add note --- js/background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/background.js b/js/background.js index 85f040d99..7ef8f8208 100644 --- a/js/background.js +++ b/js/background.js @@ -1059,6 +1059,7 @@ window.setMediaPermissions(!mediaPermissions); }; + // attempts a connection to an open group server window.attemptConnection = async (serverURL, channelId) => { let rawserverURL = serverURL .replace(/^https?:\/\//i, '') From df1d032d967c62b30f6625dffd72f5d1d3f56632 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 6 Feb 2020 16:06:36 -0800 Subject: [PATCH 2/5] headers protection, textResponse support, improve logging --- js/modules/loki_app_dot_net_api.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 38b229c73..97ada5cba 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -367,12 +367,17 @@ class LokiAppDotNetServerAPI { return nodeFetch(urlObj, fetchOptions); } - async _sendToProxy(endpoint, fetchOptions) { + async _sendToProxy(endpoint, pFetchOptions) { const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`; + const fetchOptions = pFetchOptions; // make lint happy + // safety issue with file server, just safer to have this + if (fetchOptions.headers === undefined) { + fetchOptions.headers = {}; + } + const payloadObj = { - // I think this is a stream, we may need to collect it all? body: fetchOptions.body, // might need to b64 if binary... endpoint, method: fetchOptions.method, @@ -446,7 +451,7 @@ class LokiAppDotNetServerAPI { const txtResponse = await result.text(); if (txtResponse === 'Service node is not ready: not in any swarm; \n') { // mark snode bad - log.warn('Marking random snode bad', randSnode); + log.warn(`Marking random snode bad, internet address ${randSnode.ip}:${randSnode.port}`); lokiSnodeAPI.markRandomNodeUnreachable(randSnode); // retry (hopefully with new snode) // FIXME: max number of retries... @@ -563,7 +568,8 @@ class LokiAppDotNetServerAPI { // always make sure this check is enabled process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; txtResponse = await result.text(); - response = JSON.parse(txtResponse); + // hrm cloudflare timeouts (504s) will be html... + response = options.textResponse ? txtResponse : JSON.parse(txtResponse); } } catch (e) { if (txtResponse) { @@ -574,7 +580,7 @@ class LokiAppDotNetServerAPI { `json: ${txtResponse}` ); } else { - log.info(`serverRequest ${mode} error`, e.code, e.message); + log.info(`serverRequest ${mode} error`, e.code, e.message, 'atttempting connection to', url); } return { err: e, From 4d027909c6652be4cd1c5fad2daeac491fbf3cef Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 6 Feb 2020 16:26:30 -0800 Subject: [PATCH 3/5] more text response fixes, better snode not ready detection --- js/modules/loki_app_dot_net_api.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 97ada5cba..a44a945d9 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -317,7 +317,7 @@ class LokiAppDotNetServerAPI { // activate token async submitToken(token) { - const options = { + const fetchOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -331,7 +331,8 @@ class LokiAppDotNetServerAPI { try { const res = await this.proxyFetch( `${this.baseServerUrl}/loki/v1/submit_challenge`, - options + fetchOptions, + { textResponse: true } ); return res.ok; } catch (e) { @@ -340,7 +341,7 @@ class LokiAppDotNetServerAPI { } } - async proxyFetch(urlObj, fetchOptions = { method: 'GET' }) { + async proxyFetch(urlObj, fetchOptions = { method: 'GET' }, options = {}) { if ( window.lokiFeatureFlags.useSnodeProxy && (this.baseServerUrl === 'https://file-dev.lokinet.org' || @@ -364,10 +365,10 @@ class LokiAppDotNetServerAPI { json: () => response, }; } - return nodeFetch(urlObj, fetchOptions); + return nodeFetch(urlObj, fetchOptions, options); } - async _sendToProxy(endpoint, pFetchOptions) { + async _sendToProxy(endpoint, pFetchOptions, options = {}) { const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`; @@ -449,7 +450,7 @@ class LokiAppDotNetServerAPI { const result = await nodeFetch(url, firstHopOptions); const txtResponse = await result.text(); - if (txtResponse === 'Service node is not ready: not in any swarm; \n') { + if (txtResponse.match(/^Service node is not ready: not in any swarm/i)) { // mark snode bad log.warn(`Marking random snode bad, internet address ${randSnode.ip}:${randSnode.port}`); lokiSnodeAPI.markRandomNodeUnreachable(randSnode); @@ -476,12 +477,12 @@ class LokiAppDotNetServerAPI { ivAndCiphertextResponse ); const textDecoder = new TextDecoder(); - const json = textDecoder.decode(decrypted); + const respStr = textDecoder.decode(decrypted); // replace response try { - response = JSON.parse(json); + response = options.textResponse ? respStr : JSON.parse(respStr); } catch (e) { - log.warn(`_sendToProxy Could not parse inner JSON [${json}]`); + log.warn(`_sendToProxy Could not parse inner JSON [${respStr}]`); } } else { log.warn( @@ -557,7 +558,8 @@ class LokiAppDotNetServerAPI { .replace(`${this.baseServerUrl}/`, ''); ({ response, txtResponse, result } = await this._sendToProxy( endpointWithQS, - fetchOptions + fetchOptions, + options )); } else { // disable check for .loki From b9ab28e2fc396c06533fa5dcd2d9628f9733cefd Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 6 Feb 2020 16:26:50 -0800 Subject: [PATCH 4/5] inform that we expect a non-json repsonse --- js/modules/loki_rss_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/modules/loki_rss_api.js b/js/modules/loki_rss_api.js index 566a2afc1..a7612441b 100644 --- a/js/modules/loki_rss_api.js +++ b/js/modules/loki_rss_api.js @@ -64,7 +64,7 @@ class LokiRssAPI extends EventEmitter { return; } const result = await window.lokiFileServerAPI._server.serverRequest( - map[this.feedUrl] + map[this.feedUrl], { textResponse: true } ); if (!result) { log.error('LokiRssAPI empty rss proxy response'); From 1179105a75b312ab2a2a6fc48f1083ff49eb0468 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 6 Feb 2020 16:28:56 -0800 Subject: [PATCH 5/5] allow TLDs longer than 5 characters, actually close on sucess --- ts/components/session/LeftPaneChannelSection.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ts/components/session/LeftPaneChannelSection.tsx b/ts/components/session/LeftPaneChannelSection.tsx index c5131f448..e47d8c215 100644 --- a/ts/components/session/LeftPaneChannelSection.tsx +++ b/ts/components/session/LeftPaneChannelSection.tsx @@ -364,7 +364,8 @@ export class LeftPaneChannelSection extends React.Component { return false; } - const regexURL = /(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?/; + // longest TLD is now (20/02/06) 24 characters per https://jasontucker.blog/8945/what-is-the-longest-tld-you-can-get-for-a-domain-name + const regexURL = /(http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,24}(:[0-9]{1,5})?(\/.*)?/; if (groupUrl.length <= 0) { window.pushToast({ @@ -387,7 +388,7 @@ export class LeftPaneChannelSection extends React.Component { } joinChannelStateManager(this, groupUrl, () => { - this.handleToggleOverlay(SessionGroupType.Open); + this.handleToggleOverlay(undefined); }); return true;