|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|