Use the new logging API for the history keeper

pull/1/head
yflory 6 years ago
parent 338b40face
commit e0665cb190

@ -6,12 +6,12 @@ const nThen = require('nthen');
const Nacl = require('tweetnacl'); const Nacl = require('tweetnacl');
const Crypto = require('crypto'); const Crypto = require('crypto');
let Log;
const now = function () { return (new Date()).getTime(); }; const now = function () { return (new Date()).getTime(); };
const getHash = function (msg) { const getHash = function (msg) {
if (typeof(msg) !== 'string') { if (typeof(msg) !== 'string') {
console.log('getHash() called on', typeof(msg), msg); Log.warn('', 'getHash() called on ' + typeof(msg) + ': ' + msg);
return ''; return '';
} }
return msg.slice(0,64); return msg.slice(0,64);
@ -21,7 +21,7 @@ const tryParse = function (str) {
try { try {
return JSON.parse(str); return JSON.parse(str);
} catch (err) { } catch (err) {
console.error(err); Log.error('HK_PARSE_ERROR', err);
} }
}; };
@ -40,10 +40,15 @@ module.exports.create = function (cfg) {
const rpc = cfg.rpc; const rpc = cfg.rpc;
const tasks = cfg.tasks; const tasks = cfg.tasks;
const store = cfg.store; const store = cfg.store;
Log = cfg.log;
Log.silly('LOADING HISTORY_KEEPER MODULE');
const historyKeeperKeys = {}; const historyKeeperKeys = {};
const HISTORY_KEEPER_ID = Crypto.randomBytes(8).toString('hex'); const HISTORY_KEEPER_ID = Crypto.randomBytes(8).toString('hex');
Log.verbose('History keeper ID: ' + HISTORY_KEEPER_ID);
let sendMsg = function () {}; let sendMsg = function () {};
let STANDARD_CHANNEL_LENGTH, EPHEMERAL_CHANNEL_LENGTH; let STANDARD_CHANNEL_LENGTH, EPHEMERAL_CHANNEL_LENGTH;
const setConfig = function (config) { const setConfig = function (config) {
@ -133,14 +138,13 @@ module.exports.create = function (cfg) {
store.messageBin(channel.id, msgBin, waitFor(function (err) { store.messageBin(channel.id, msgBin, waitFor(function (err) {
if (err) { if (err) {
waitFor.abort(); waitFor.abort();
return void console.log("Error writing message: " + err.message); return void Log.error("HK_STORE_MESSAGE_ERROR", err.message);
} }
})); }));
}).nThen((waitFor) => { }).nThen((waitFor) => {
getIndex(ctx, channel.id, waitFor((err, index) => { getIndex(ctx, channel.id, waitFor((err, index) => {
if (err) { if (err) {
console.log("getIndex()"); Log.warn("HK_STORE_MESSAGE_INDEX", err.stack);
console.log(err.stack);
// non-critical, we'll be able to get the channel index later // non-critical, we'll be able to get the channel index later
return; return;
} }
@ -189,7 +193,7 @@ module.exports.create = function (cfg) {
const validateKey = Nacl.util.decodeBase64(historyKeeperKeys[channel.id].validateKey); const validateKey = Nacl.util.decodeBase64(historyKeeperKeys[channel.id].validateKey);
const validated = Nacl.sign.open(signedMsg, validateKey); const validated = Nacl.sign.open(signedMsg, validateKey);
if (!validated) { if (!validated) {
console.log("Signed message rejected"); // TODO logging Log.info("HK_SIGNED_MESSAGE_REJECTED", 'Channel '+channel.id);
return; return;
} }
} }
@ -309,7 +313,7 @@ module.exports.create = function (cfg) {
messageBuffer.push(parsed); messageBuffer.push(parsed);
}, function (err) { }, function (err) {
if (err) { if (err) {
console.error("getOlderHistory", err); Log.error("HK_GET_OLDER_HISTORY", err);
} }
cb(messageBuffer); cb(messageBuffer);
}); });
@ -371,10 +375,13 @@ module.exports.create = function (cfg) {
let parsed; let parsed;
let channelName; let channelName;
let obj = HISTORY_KEEPER_ID; let obj = HISTORY_KEEPER_ID;
Log.silly(json);
try { try {
parsed = JSON.parse(json[2]); parsed = JSON.parse(json[2]);
} catch (err) { } catch (err) {
console.error("handleMessage(JSON.parse)", err); // TODO logging Log.error("HK_PARSE_CLIENT_MESSAGE", json);
return; return;
} }
@ -413,8 +420,8 @@ module.exports.create = function (cfg) {
// if there is an error, we don't want to crash the whole server... // if there is an error, we don't want to crash the whole server...
// just log it, and if there's a problem you'll be able to fix it // just log it, and if there's a problem you'll be able to fix it
// at a later date with the provided information // at a later date with the provided information
console.error('Failed to write expiration to disk:', err); // TODO logging Log.error('HK_CREATE_EXPIRE_TASK', err);
console.error([expire, 'EXPIRE', channelName]); // TODO logging Log.info('HK_INVALID_EXPIRE_TASK', JSON.stringify([expire, 'EXPIRE', channelName]));
} }
})); }));
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
@ -465,7 +472,7 @@ module.exports.create = function (cfg) {
if (expired) { return; } if (expired) { return; }
if (err && err.code !== 'ENOENT') { if (err && err.code !== 'ENOENT') {
if (err.message !== 'EINVAL') { console.error("GET_HISTORY", err); } if (err.message !== 'EINVAL') { Log.error("HK_GET_HISTORY", err); }
const parsedMsg = {error:err.message, channel: channelName}; const parsedMsg = {error:err.message, channel: channelName};
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]); sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]);
return; return;
@ -550,7 +557,7 @@ module.exports.create = function (cfg) {
}, (err) => { }, (err) => {
let parsedMsg = ['FULL_HISTORY_END', parsed[1]]; let parsedMsg = ['FULL_HISTORY_END', parsed[1]];
if (err) { if (err) {
console.error(err.stack); Log.error('HK_GET_FULL_HISTORY', err.stack);
parsedMsg = ['ERROR', parsed[1], err.message]; parsedMsg = ['ERROR', parsed[1], err.message];
} }
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]); sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(parsedMsg)]);
@ -582,7 +589,6 @@ module.exports.create = function (cfg) {
} }
}; };
// TODO logging
var cciLock = false; var cciLock = false;
const checkChannelIntegrity = function (ctx) { const checkChannelIntegrity = function (ctx) {
if (process.env['CRYPTPAD_DEBUG'] && !cciLock) { if (process.env['CRYPTPAD_DEBUG'] && !cciLock) {
@ -593,10 +599,15 @@ module.exports.create = function (cfg) {
if (!chan.index) { return; } if (!chan.index) { return; }
nt = nt((waitFor) => { nt = nt((waitFor) => {
store.getChannelSize(channelName, waitFor((err, size) => { store.getChannelSize(channelName, waitFor((err, size) => {
if (err) { return void console.log("Couldn't get size of channel", channelName); } if (err) {
return void Log.debug("HK_CHECK_CHANNEL_INTEGRITY",
"Couldn't get size of channel " + channelName);
}
if (size !== chan.index.size) { if (size !== chan.index.size) {
console.log("channel size mismatch for", channelName, return void Log.debug("HK_CHECK_CHANNEL_SIZE",
"cached:", chan.index.size, "fileSize:", size); "channel size mismatch for " + channelName +
" --- cached: " + chan.index.size +
" --- fileSize: " + size);
} }
})); }));
}).nThen; }).nThen;

@ -283,7 +283,8 @@ var nt = nThen(function (w) {
var hkConfig = { var hkConfig = {
tasks: config.tasks, tasks: config.tasks,
rpc: rpc, rpc: rpc,
store: config.store store: config.store,
log: log
}; };
historyKeeper = HK.create(hkConfig); historyKeeper = HK.create(hkConfig);
}).nThen(function () { }).nThen(function () {

Loading…
Cancel
Save