Updated messenger to work with slightly different storage server API and changed swarm requests to go through storage server
parent
9a52319da3
commit
531ee92dcb
@ -0,0 +1,93 @@
|
|||||||
|
/* global log, libloki, textsecure */
|
||||||
|
|
||||||
|
const nodeFetch = require('node-fetch');
|
||||||
|
const { parse } = require('url');
|
||||||
|
|
||||||
|
const LOKI_EPHEMKEY_HEADER = 'X-Loki-EphemKey';
|
||||||
|
const endpointBase = '/v1/storage_rpc';
|
||||||
|
|
||||||
|
// A small wrapper around node-fetch which deserializes response
|
||||||
|
const fetch = async (url, options = {}) => {
|
||||||
|
const timeout = options.timeout || 10000;
|
||||||
|
const method = options.method || 'GET';
|
||||||
|
|
||||||
|
const address = parse(url).hostname;
|
||||||
|
const doEncryptChannel = address.endsWith('.snode');
|
||||||
|
if (doEncryptChannel) {
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
options.body = await libloki.crypto.snodeCipher.encrypt(
|
||||||
|
address,
|
||||||
|
options.body
|
||||||
|
);
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
options.headers = {
|
||||||
|
...options.headers,
|
||||||
|
'Content-Type': 'text/plain',
|
||||||
|
[LOKI_EPHEMKEY_HEADER]: libloki.crypto.snodeCipher.getChannelPublicKeyHex(),
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
log.warn(`Could not encrypt channel for ${address}: `, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await nodeFetch(url, {
|
||||||
|
...options,
|
||||||
|
timeout,
|
||||||
|
method,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new textsecure.HTTPError(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result;
|
||||||
|
if (response.headers.get('Content-Type') === 'application/json') {
|
||||||
|
result = await response.json();
|
||||||
|
} else if (options.responseType === 'arraybuffer') {
|
||||||
|
result = await response.buffer();
|
||||||
|
} else {
|
||||||
|
result = await response.text();
|
||||||
|
if (doEncryptChannel) {
|
||||||
|
try {
|
||||||
|
result = await libloki.crypto.snodeCipher.decrypt(address, result);
|
||||||
|
} catch (e) {
|
||||||
|
log.warn(`Could not decrypt response from ${address}`, e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
result = result === '' ? {} : JSON.parse(result);
|
||||||
|
} catch (e) {
|
||||||
|
log.warn(`Could not parse string to json ${result}`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Wrapper for a JSON RPC request
|
||||||
|
const rpc = (address, port, method, params, options = {}) => {
|
||||||
|
const headers = options.headers || {};
|
||||||
|
const url = `${address}${port}${endpointBase}`;
|
||||||
|
const body = {
|
||||||
|
method,
|
||||||
|
params,
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchOptions = {
|
||||||
|
method: 'POST',
|
||||||
|
...options,
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(url, fetchOptions);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
rpc,
|
||||||
|
};
|
Loading…
Reference in New Issue