1
0
Fork 0
forked from svrjs/svrjs

Change the way of adding process message listeners, and add process message listener to server error handler.

This commit is contained in:
Dorian Niemiec 2024-08-25 17:57:23 +02:00
parent 45815957d5
commit 58cb60fb8f
3 changed files with 30 additions and 15 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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) => {