From 58cb60fb8f69eecafe6dbc600dafd6ac32d01245 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sun, 25 Aug 2024 17:57:23 +0200 Subject: [PATCH] Change the way of adding process message listeners, and add process message listener to server error handler. --- src/handlers/serverErrorHandler.js | 37 +++++++++++++------ src/index.js | 2 + .../nonStandardCodesAndHttpAuthentication.js | 6 +-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/handlers/serverErrorHandler.js b/src/handlers/serverErrorHandler.js index 7079102..0dfe5ed 100644 --- a/src/handlers/serverErrorHandler.js +++ b/src/handlers/serverErrorHandler.js @@ -18,23 +18,21 @@ function serverErrorHandler(err, isRedirect, server, start) { (isRedirect ? attmtsRedir : attmts) + " attempts left.", ); } else { - // TODO: worker message listener - /*try { - process.send("\x12ERRLIST" + (isRedirect ? attmtsRedir : attmts) + err.code); - } catch (err) { - // Probably main process exited - }*/ + try { + process.send("\x12ERRLIST" + (isRedirect ? attmtsRedir : attmts) + err.code); + } catch (err) { + // Probably main process exited + } } if ((isRedirect ? attmtsRedir : attmts) > 0) { server.close(); setTimeout(start, 900); } else { - // TODO: worker message listener - /*try { - if (cluster.isPrimary !== undefined) process.send("\x12ERRCRASH" + err.code); - } catch (err) { - // Probably main process exited - }*/ + try { + if (cluster.isPrimary !== undefined) process.send("\x12ERRCRASH" + err.code); + } catch (err) { + // Probably main process exited + } setTimeout(function () { var errno = os.constants.errno[err.code]; process.exit(errno !== undefined ? errno : 1); @@ -47,6 +45,21 @@ serverErrorHandler.resetAttempts = (isRedirect) => { else attmts = 5; }; +process.messageEventListeners.push((worker, serverconsole) => { + return (message) => { + if (message.indexOf("\x12ERRLIST") == 0) { + const tries = parseInt(msg.substring(8, 9)); + const errCode = msg.substring(9); + serverconsole.locerrmessage(serverErrorDescs[errCode] ? serverErrorDescs[errCode] : serverErrorDescs["UNKNOWN"]); + serverconsole.locmessage(tries + " attempts left."); + } + if (message.length >= 9 && msg.indexOf("\x12ERRCRASH") == 0) { + const errno = errors[msg.substring(9)]; + process.exit(errno !== undefined ? errno : 1); + } + } +}); + module.exports = (serverconsoleO) => { serverconsole = serverconsoleO; return serverErrorHandler; diff --git a/src/index.js b/src/index.js index 874566f..0e58dd9 100644 --- a/src/index.js +++ b/src/index.js @@ -92,6 +92,8 @@ process.err5xxcounter = 0; process.reqcounter = 0; process.malformedcounter = 0; +process.messageEventListeners = []; + if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into process.versions let exiting = false; diff --git a/src/middleware/nonStandardCodesAndHttpAuthentication.js b/src/middleware/nonStandardCodesAndHttpAuthentication.js index 1fd777d..280fb6c 100644 --- a/src/middleware/nonStandardCodesAndHttpAuthentication.js +++ b/src/middleware/nonStandardCodesAndHttpAuthentication.js @@ -422,8 +422,8 @@ module.exports = (req, res, logFacilities, config, next) => { }; // IPC listener for brute force protection -module.exports.mainMessageListenerWrapper = (worker) => { - return function bruteForceListener(message) { +process.messageEventListeners.push((worker, serverconsole) => { + return (message) => { let ip = ""; if (message.substring(0, 6) == "\x12AUTHQ") { ip = message.substring(6); @@ -458,7 +458,7 @@ module.exports.mainMessageListenerWrapper = (worker) => { } } }; -}; +}); module.exports.commands = { stop: (args, log, passCommand) => {