handle non-base64 responses appropriately, include which server failed in logs

pull/868/head
Ryan Tharp 6 years ago
parent 4c12bb6c44
commit 774c468c39

@ -29,10 +29,10 @@ const decryptResponse = async (response, address) => {
return {}; return {};
}; };
// TODO: Don't allow arbitrary URLs, only snodes and loki servers
const sendToProxy = async (options = {}, targetNode) => { const sendToProxy = async (options = {}, targetNode) => {
const randSnode = await lokiSnodeAPI.getRandomSnodeAddress(); const randSnode = await lokiSnodeAPI.getRandomSnodeAddress();
// Don't allow arbitrary URLs, only snodes and loki servers
const url = `https://${randSnode.ip}:${randSnode.port}/proxy`; const url = `https://${randSnode.ip}:${randSnode.port}/proxy`;
const snPubkeyHex = StringView.hexToArrayBuffer(targetNode.pubkey_x25519); const snPubkeyHex = StringView.hexToArrayBuffer(targetNode.pubkey_x25519);
@ -67,20 +67,42 @@ const sendToProxy = async (options = {}, targetNode) => {
const response = await nodeFetch(url, firstHopOptions); const response = await nodeFetch(url, firstHopOptions);
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;
const ciphertext = await response.text(); // FIXME: handle nodeFetch errors/exceptions...
const ciphertextBuffer = dcodeIO.ByteBuffer.wrap(
ciphertext,
'base64'
).toArrayBuffer();
const plaintextBuffer = await window.libloki.crypto.DHDecrypt( const ciphertext = await response.text();
symmetricKey, let plaintext
ciphertextBuffer try {
); // removed the following part to match the check we have in loki_app_dot_net_api.js
// ; not done syncing;
if (ciphertext.match(/Service node is not ready: not in any swarm/)) {
log.error(`lokiRpc sendToProxy snode ${randSnode.ip}:${randSnode.port} error`, ciphertext);
// mark as bad
lokiSnodeAPI.markRandomNodeUnreachable(randSnode);
// retry
return sendToProxy(options, targetNode);
}
const ciphertextBuffer = dcodeIO.ByteBuffer.wrap(
ciphertext,
'base64'
).toArrayBuffer();
const plaintextBuffer = await window.libloki.crypto.DHDecrypt(
symmetricKey,
ciphertextBuffer
);
const textDecoder = new TextDecoder(); const textDecoder = new TextDecoder();
const plaintext = textDecoder.decode(plaintextBuffer); plaintext = textDecoder.decode(plaintextBuffer);
} catch(e) {
log.error(
'lokiRpc sendToProxy decode error',
e.code,
e.message,
`from ${randSnode.ip}:${randSnode.port} ciphertext:`,
ciphertext
);
return false;
}
try { try {
const jsonRes = JSON.parse(plaintext); const jsonRes = JSON.parse(plaintext);
@ -90,10 +112,10 @@ const sendToProxy = async (options = {}, targetNode) => {
return JSON.parse(jsonRes.body); return JSON.parse(jsonRes.body);
} catch (e) { } catch (e) {
log.error( log.error(
'lokiRpc sendToProxy error', 'lokiRpc sendToProxy parse error',
e.code, e.code,
e.message, e.message,
'json', `from ${randSnode.ip}:${randSnode.port} json:`,
jsonRes.body jsonRes.body
); );
} }
@ -102,10 +124,10 @@ const sendToProxy = async (options = {}, targetNode) => {
return jsonRes; return jsonRes;
} catch (e) { } catch (e) {
log.error( log.error(
'lokiRpc sendToProxy error', 'lokiRpc sendToProxy parse error',
e.code, e.code,
e.message, e.message,
'json', `from ${randSnode.ip}:${randSnode.port} json:`,
plaintext plaintext
); );
} }

Loading…
Cancel
Save