replace ad-hoc response handler with Util.response
This commit is contained in:
@@ -9,39 +9,43 @@ const Nacl = require('tweetnacl/nacl-fast');
|
|||||||
console.log('New child process', process.pid);
|
console.log('New child process', process.pid);
|
||||||
|
|
||||||
process.on('message', function (data) {
|
process.on('message', function (data) {
|
||||||
console.log('In process', process.pid);
|
//console.log('In process', process.pid);
|
||||||
console.log(+new Date(), "Message received by subprocess");
|
//console.log(+new Date(), "Message received by subprocess");
|
||||||
if (!data || !data.key || !data.msg || !data.txid) {
|
if (!data || !data.key || !data.msg || !data.txid) {
|
||||||
process.send({
|
return void process.send({
|
||||||
error:'E_INVAL'
|
error:'E_INVAL'
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
const txid = data.txid;
|
const txid = data.txid;
|
||||||
|
|
||||||
const signedMsg = Nacl.util.decodeBase64(data.msg);
|
var signedMsg;
|
||||||
|
try {
|
||||||
|
signedMsg = Nacl.util.decodeBase64(data.msg);
|
||||||
|
} catch (e) {
|
||||||
|
return void process.send({
|
||||||
|
txid: txid,
|
||||||
|
error: 'E_BAD_MESSAGE',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var validateKey;
|
var validateKey;
|
||||||
try {
|
try {
|
||||||
validateKey = Nacl.util.decodeBase64(data.key);
|
validateKey = Nacl.util.decodeBase64(data.key);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
process.send({
|
return void process.send({
|
||||||
txid: txid,
|
txid: txid,
|
||||||
error:'E_BADKEY'
|
error:'E_BADKEY'
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// validate the message
|
// validate the message
|
||||||
const validated = Nacl.sign.open(signedMsg, validateKey);
|
const validated = Nacl.sign.open(signedMsg, validateKey);
|
||||||
if (!validated) {
|
if (!validated) {
|
||||||
process.send({
|
return void process.send({
|
||||||
txid: txid,
|
txid: txid,
|
||||||
error:'FAILED'
|
error:'FAILED'
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
console.log(+new Date(), "Verification done in the subprocess");
|
|
||||||
process.send({
|
process.send({
|
||||||
txid: txid,
|
txid: txid,
|
||||||
success: true
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -925,17 +925,20 @@ HK.onDirectMessage = function (Env, Server, seq, userId, json) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
const onChecked = Util.mkEvent();
|
|
||||||
|
|
||||||
// Create our workers
|
// Create our workers
|
||||||
const workers = [];
|
const workers = [];
|
||||||
for (let i = 0; i < numCPUs; i++) {
|
for (let i = 0; i < numCPUs; i++) {
|
||||||
workers.push(fork('lib/check-signature.js'));
|
workers.push(fork('lib/check-signature.js'));
|
||||||
}
|
}
|
||||||
var nextWorker = 0;
|
var nextWorker = 0;
|
||||||
|
|
||||||
|
const response = Util.response();
|
||||||
|
|
||||||
var initWorker = function (worker) {
|
var initWorker = function (worker) {
|
||||||
worker.on('message', function (res) {
|
worker.on('message', function (res) {
|
||||||
onChecked.fire(res);
|
if (!res || !res.txid) { return; }
|
||||||
|
//console.log(+new Date(), "Received verification response");
|
||||||
|
response.handle(res.txid, [res.error]);
|
||||||
});
|
});
|
||||||
// Spawn a new process in one ends
|
// Spawn a new process in one ends
|
||||||
worker.on('exit', function () {
|
worker.on('exit', function () {
|
||||||
@@ -952,14 +955,32 @@ var initWorker = function (worker) {
|
|||||||
};
|
};
|
||||||
workers.forEach(initWorker);
|
workers.forEach(initWorker);
|
||||||
|
|
||||||
const validateMessage = function (msg) {
|
|
||||||
|
const validateMessage = function (signedMsg, key, _cb) {
|
||||||
|
// let's be paranoid about asynchrony and only calling back once..
|
||||||
|
var cb = Util.once(Util.mkAsync(_cb));
|
||||||
|
|
||||||
|
var txid = Util.uid();
|
||||||
|
|
||||||
|
// expect a response within 15s
|
||||||
|
response.expect(txid, cb, 15000);
|
||||||
|
|
||||||
nextWorker = (nextWorker + 1) % workers.length;
|
nextWorker = (nextWorker + 1) % workers.length;
|
||||||
|
if (workers.length === 0 || typeof(workers[nextWorker].send) !== 'function') {
|
||||||
|
console.error(workers);
|
||||||
|
throw new Error("INVALID_WORKERS");
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
workers[nextWorker].send(msg);
|
workers[nextWorker].send({
|
||||||
|
txid: txid,
|
||||||
|
msg: signedMsg,
|
||||||
|
key: key,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
||||||
console.log(+new Date(), "onChannelMessage");
|
//console.log(+new Date(), "onChannelMessage");
|
||||||
const Log = Env.Log;
|
const Log = Env.Log;
|
||||||
|
|
||||||
// TODO our usage of 'channel' here looks prone to errors
|
// TODO our usage of 'channel' here looks prone to errors
|
||||||
@@ -1000,36 +1021,22 @@ HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
|||||||
let signedMsg = (isCp) ? msgStruct[4].replace(CHECKPOINT_PATTERN, '') : msgStruct[4];
|
let signedMsg = (isCp) ? msgStruct[4].replace(CHECKPOINT_PATTERN, '') : msgStruct[4];
|
||||||
// convert the message from a base64 string into a Uint8Array
|
// convert the message from a base64 string into a Uint8Array
|
||||||
|
|
||||||
const txid = Util.uid();
|
//const txid = Util.uid();
|
||||||
const next = w();
|
|
||||||
|
|
||||||
// Listen for messages
|
// Listen for messages
|
||||||
const onCheck = function (res) {
|
//console.log(+new Date(), "Send verification request");
|
||||||
if (!res) { return; }
|
validateMessage(signedMsg, metadata.validateKey, w(function (err) {
|
||||||
if (res.txid !== txid) { return; }
|
if (err) {
|
||||||
// Wev'e received an answer, remove this handler
|
// validation can fail in multiple ways
|
||||||
console.log(+new Date(), "Received verification response");
|
if (err === 'FAILED') {
|
||||||
onChecked.unreg(onCheck);
|
// we log this case, but not others for some reason
|
||||||
if (res.error) {
|
|
||||||
// don't go any further if the message fails validation
|
|
||||||
if (res.error === 'FAILED') {
|
|
||||||
// Signature doesn't match the public key
|
|
||||||
Log.info("HK_SIGNED_MESSAGE_REJECTED", 'Channel '+channel.id);
|
Log.info("HK_SIGNED_MESSAGE_REJECTED", 'Channel '+channel.id);
|
||||||
}
|
}
|
||||||
w.abort();
|
// always abort if there was an error...
|
||||||
return;
|
return void w.abort();
|
||||||
}
|
}
|
||||||
// Success, continue to next block
|
// otherwise it was successful!
|
||||||
next();
|
}));
|
||||||
};
|
|
||||||
onChecked.reg(onCheck);
|
|
||||||
|
|
||||||
console.log(+new Date(), "Send verification request");
|
|
||||||
validateMessage( {
|
|
||||||
txid: txid,
|
|
||||||
msg: signedMsg,
|
|
||||||
key: metadata.validateKey
|
|
||||||
});
|
|
||||||
}).nThen(function () {
|
}).nThen(function () {
|
||||||
// do checkpoint stuff...
|
// do checkpoint stuff...
|
||||||
|
|
||||||
@@ -1056,9 +1063,9 @@ HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
|||||||
msgStruct.push(now());
|
msgStruct.push(now());
|
||||||
|
|
||||||
// storeMessage
|
// storeMessage
|
||||||
console.log(+new Date(), "Storing message");
|
//console.log(+new Date(), "Storing message");
|
||||||
storeMessage(Env, channel, JSON.stringify(msgStruct), isCp, getHash(msgStruct[4], Log));
|
storeMessage(Env, channel, JSON.stringify(msgStruct), isCp, getHash(msgStruct[4], Log));
|
||||||
console.log(+new Date(), "Message stored");
|
//console.log(+new Date(), "Message stored");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user