make sure token comms are done over fileProxy, other notes, logging adjustment

pull/774/head
Ryan Tharp 6 years ago
parent 5b565695a3
commit 3d70a6dc66

@ -1,4 +1,4 @@
/* global log, textsecure, libloki, Signal, Whisper, Headers, ConversationController, /* global log, textsecure, libloki, Signal, Whisper, ConversationController,
clearTimeout, MessageController, libsignal, StringView, window, _, clearTimeout, MessageController, libsignal, StringView, window, _,
dcodeIO, Buffer, lokiSnodeAPI, TextDecoder */ dcodeIO, Buffer, lokiSnodeAPI, TextDecoder */
const nodeFetch = require('node-fetch'); const nodeFetch = require('node-fetch');
@ -44,15 +44,24 @@ class LokiAppDotNetServerAPI {
if (!thisChannel) { if (!thisChannel) {
// make sure we're subscribed // make sure we're subscribed
// eventually we'll need to move to account registration/add server // eventually we'll need to move to account registration/add server
await this.serverRequest(`channels/${channelId}/subscribe`, { await this.serverRequest(
method: 'POST', `channels/${channelId}/subscribe`,
}); {
method: 'POST',
}
);
thisChannel = new LokiPublicChannelAPI( thisChannel = new LokiPublicChannelAPI(
chatAPI, chatAPI,
this, this,
channelId, channelId,
conversationId conversationId
); );
log.info(
'LokiPublicChannelAPI started for',
channelId,
'on',
this.baseServerUrl
);
this.channels.push(thisChannel); this.channels.push(thisChannel);
} }
return thisChannel; return thisChannel;
@ -220,9 +229,11 @@ class LokiAppDotNetServerAPI {
async refreshServerToken() { async refreshServerToken() {
// if currently not in progress // if currently not in progress
if (this.tokenPromise === null) { if (this.tokenPromise === null) {
// FIXME: add timeout
// a broken/stuck token endpoint can prevent you from removing channels
// set lock // set lock
this.tokenPromise = new Promise(async res => { this.tokenPromise = new Promise(async res => {
// request the oken // request the token
const token = await this.requestToken(); const token = await this.requestToken();
if (!token) { if (!token) {
res(null); res(null);
@ -255,11 +266,13 @@ class LokiAppDotNetServerAPI {
}; };
url.search = new URLSearchParams(params); url.search = new URLSearchParams(params);
res = await nodeFetch(url); res = await this.proxyFetch(url);
} catch (e) { } catch (e) {
log.error('requestToken request failed', e);
return null; return null;
} }
if (!res.ok) { if (!res.ok) {
log.error('requestToken request failed');
return null; return null;
} }
const body = await res.json(); const body = await res.json();
@ -281,7 +294,7 @@ class LokiAppDotNetServerAPI {
}; };
try { try {
const res = await nodeFetch( const res = await this.proxyFetch(
`${this.baseServerUrl}/loki/v1/submit_challenge`, `${this.baseServerUrl}/loki/v1/submit_challenge`,
options 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) { async _sendToProxy(fetchOptions, method, headers, endpoint) {
const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const randSnode = await lokiSnodeAPI.getRandomSnodeAddress();
const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`; const url = `https://${randSnode.ip}:${randSnode.port}/file_proxy`;
@ -419,7 +459,7 @@ class LokiAppDotNetServerAPI {
} else if (rawBody) { } else if (rawBody) {
fetchOptions.body = rawBody; fetchOptions.body = rawBody;
} }
fetchOptions.headers = new Headers(headers); fetchOptions.headers = headers;
} catch (e) { } catch (e) {
log.info('serverRequest set up error:', JSON.stringify(e)); log.info('serverRequest set up error:', JSON.stringify(e));
return { return {
@ -446,7 +486,7 @@ class LokiAppDotNetServerAPI {
endpoint endpoint
)); ));
} else { } else {
result = await nodeFetch(url, fetchOptions || undefined); result = await nodeFetch(url, fetchOptions);
txtResponse = await result.text(); txtResponse = await result.text();
response = JSON.parse(txtResponse); response = JSON.parse(txtResponse);
} }
@ -714,7 +754,7 @@ class LokiAppDotNetServerAPI {
options options
); );
if (statusCode !== 200) { if (statusCode !== 200) {
log.warn('Failed to upload data to fileserver'); log.warn('Failed to upload data to server', this.baseServerUrl);
return null; return null;
} }

Loading…
Cancel
Save