From 45815957d5c0a3f458b491eab55cfa6753a12e78 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sun, 25 Aug 2024 17:51:25 +0200 Subject: [PATCH] Create the server error handler and temporary startup script. --- src/handlers/serverErrorHandler.js | 53 ++++++++++++++++++++++++++++++ src/index.js | 43 ++++++++++++++++++------ src/res/httpErrorDescriptions.js | 2 +- src/res/serverErrorDescriptions.js | 36 ++++++++++++++++++++ 4 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/handlers/serverErrorHandler.js create mode 100644 src/res/serverErrorDescriptions.js diff --git a/src/handlers/serverErrorHandler.js b/src/handlers/serverErrorHandler.js new file mode 100644 index 0000000..7079102 --- /dev/null +++ b/src/handlers/serverErrorHandler.js @@ -0,0 +1,53 @@ +const os = require("os"); +const cluster = require("../utils/clusterBunShim.js"); +const serverErrorDescs = require("../res/serverErrorDescriptions.js"); +let serverconsole = {}; +let attmts = 5; +let attmtsRedir = 5; + +function serverErrorHandler(err, isRedirect, server, start) { + if (isRedirect) attmtsRedir--; + else attmts--; + if (cluster.isPrimary === undefined && (isRedirect ? attmtsRedir : attmts)) { + serverconsole.locerrmessage( + serverErrorDescs[err.code] + ? serverErrorDescs[err.code] + : serverErrorDescs["UNKNOWN"], + ); + serverconsole.locmessage( + (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 + }*/ + } + 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 + }*/ + setTimeout(function () { + var errno = os.constants.errno[err.code]; + process.exit(errno !== undefined ? errno : 1); + }, 50); + } +} + +serverErrorHandler.resetAttempts = (isRedirect) => { + if (isRedirect) attmtsRedir = 5; + else attmts = 5; +}; + +module.exports = (serverconsoleO) => { + serverconsole = serverconsoleO; + return serverErrorHandler; +}; diff --git a/src/index.js b/src/index.js index fa5f751..874566f 100644 --- a/src/index.js +++ b/src/index.js @@ -623,6 +623,10 @@ const clientErrorHandler = require("./handlers/clientErrorHandler.js")( serverconsole, ); +const serverErrorHandler = require("./handlers/serverErrorHandler.js")( + serverconsole, +); + let server = {}; let server2 = {}; @@ -643,6 +647,13 @@ server2.on( "connect", process.serverConfig.disableToHTTPSRedirect ? proxyHandler : noproxyHandler, ); +server2.on("error", (err) => { + serverErrorHandler(err, true, server2, start); +}); +server2.on("listening", () => { + serverErrorHandler.resetAttempts(true); + // TODO: listeningMessage(); +}); // Create HTTP server if (process.serverConfig.enableHTTP2 == true) { @@ -740,6 +751,13 @@ server.on("request", requestHandler); server.on("checkExpectation", requestHandler); server.on("connect", proxyHandler); server.on("clientError", clientErrorHandler); +server.on("error", function (err) { + serverErrorHandler(err, false, server, start); +}); +server.on("listening", () => { + serverErrorHandler.resetAttempts(false); + // TODO: listeningMessage(); +}); if (process.serverConfig.secure) { server.prependListener("connection", function (sock) { @@ -856,16 +874,19 @@ middleware.forEach((middlewareO) => { } }); -// TODO: HTTP ports -// Listen HTTP server to port 3000 -server.listen(3000); +// TODO: HTTP ports and start script +function start() { + // Listen HTTP server to port 3000 + server.listen(3000); + + // TODO: error logging + if (wwwrootError) throw wwwrootError; + if (configJSONRErr) throw configJSONRErr; + if (configJSONPErr) throw configJSONPErr; + if (certificateError) throw certificateError; + if (sniReDos) throw new Error("SNI REDOS!!!"); +} -// TODO: error logging -if (wwwrootError) throw wwwrootError; -if (configJSONRErr) throw configJSONRErr; -if (configJSONPErr) throw configJSONPErr; -if (certificateError) throw certificateError; -if (sniReDos) throw new Error("SNI REDOS!!!"); modLoadingErrors.forEach((modLoadingError) => { console.log('Error while loading "' + modLoadingError.modName + '" mod:'); console.log(modLoadingError.error); @@ -879,7 +900,7 @@ if (SSJSError) { if (cluster.isPrimary || cluster.isPrimary === undefined) { // Crash handler function crashHandlerMaster(err) { - serverconsole.locerrmessage("SVR.JS worker just crashed!!!"); + serverconsole.locerrmessage("SVR.JS main process just crashed!!!"); serverconsole.locerrmessage("Stack:"); serverconsole.locerrmessage( err.stack ? generateErrorStack(err) : String(err), @@ -976,3 +997,5 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) { } }); } + +start(); diff --git a/src/res/httpErrorDescriptions.js b/src/res/httpErrorDescriptions.js index 7625909..f8aa70e 100644 --- a/src/res/httpErrorDescriptions.js +++ b/src/res/httpErrorDescriptions.js @@ -1,5 +1,5 @@ // HTTP error descriptions -var serverHTTPErrorDescs = { +const serverHTTPErrorDescs = { 200: "The request succeeded! :)", 201: "A new resource has been created.", 202: "The request has been accepted for processing, but the processing has not been completed.", diff --git a/src/res/serverErrorDescriptions.js b/src/res/serverErrorDescriptions.js new file mode 100644 index 0000000..18af9d2 --- /dev/null +++ b/src/res/serverErrorDescriptions.js @@ -0,0 +1,36 @@ +// Server error descriptions +const serverErrorDescs = { + EADDRINUSE: "Address is already in use by another process.", + EADDRNOTAVAIL: "Address is not available on this machine.", + EACCES: + "Permission denied. You may not have sufficient privileges to access the requested address.", + EAFNOSUPPORT: + "Address family not supported. The address family (IPv4 or IPv6) of the requested address is not supported.", + EALREADY: + "Operation already in progress. The server is already in the process of establishing a connection on the requested address.", + ECONNABORTED: + "Connection aborted. The connection to the server was terminated abruptly.", + ECONNREFUSED: + "Connection refused. The server refused the connection attempt.", + ECONNRESET: + "Connection reset by peer. The connection to the server was reset by the remote host.", + EDESTADDRREQ: + "Destination address required. The destination address must be specified.", + EINVAL: "Invalid argument (invalid IP address?).", + ENETDOWN: + "Network is down. The network interface used for the connection is not available.", + ENETUNREACH: + "Network is unreachable. The network destination is not reachable from this host.", + ENOBUFS: + "No buffer space available. Insufficient buffer space is available for the server to process the request.", + ENOTFOUND: "Domain name doesn't exist (invalid IP address?).", + ENOTSOCK: "Not a socket. The file descriptor provided is not a valid socket.", + EPROTO: "Protocol error. An unspecified protocol error occurred.", + EPROTONOSUPPORT: + "Protocol not supported. The requested network protocol is not supported.", + ETIMEDOUT: + "Connection timed out. The server did not respond within the specified timeout period.", + UNKNOWN: "There was an unknown error with the server.", +}; + +module.exports = serverErrorDescs;