1
0
Fork 0
forked from svrjs/svrjs

Reformatted the source code

This commit is contained in:
Dorian Niemiec 2024-01-13 08:25:38 +01:00
parent 1123f40961
commit 03556813ec

46
svr.js
View file

@ -1,15 +1,4 @@
///////////////////////////////////////// // SVR.JS - a web server running on Node.JS
// //
// S V R . J S //
// S O U R C E C O D E //
// //
/////////////////////////////////////////
/////////////////////////////////////////
///// SVR.JS /////
/////a web server running on Node.JS/////
/////////////////////////////////////////
/////////////////////////////////////////
/* /*
* MIT License * MIT License
@ -91,8 +80,8 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
console.log("SVR.JS usage:"); console.log("SVR.JS usage:");
console.log("node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [-v] [--version]"); console.log("node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [-v] [--version]");
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("--clean -- Cleans files, that SVR.JS created"); console.log("--clean -- Cleans up files created by SVR.JS");
console.log("--reset -- Resets SVR.JS to factory settings (WARNING: DANGEROUS)"); console.log("--reset -- Resets SVR.JS to default settings (WARNING: DANGEROUS)");
console.log("--secure -- Runs HTTPS server"); console.log("--secure -- Runs HTTPS server");
console.log("--disable-mods -- Disables mods (safe mode)"); console.log("--disable-mods -- Disables mods (safe mode)");
console.log("--single-threaded -- Run single-threaded"); console.log("--single-threaded -- Run single-threaded");
@ -123,8 +112,8 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
console.log("SVR.JS usage:"); console.log("SVR.JS usage:");
console.log("node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [-v] [--version]"); console.log("node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [-v] [--version]");
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("--clean -- Cleans files, that SVR.JS created"); console.log("--clean -- Cleans up files created by SVR.JS");
console.log("--reset -- Resets SVR.JS to factory settings (WARNING: DANGEROUS)"); console.log("--reset -- Resets SVR.JS to default settings (WARNING: DANGEROUS)");
console.log("--secure -- Runs HTTPS server"); console.log("--secure -- Runs HTTPS server");
console.log("--disable-mods -- Disables mods (safe mode)"); console.log("--disable-mods -- Disables mods (safe mode)");
console.log("--single-threaded -- Run single-threaded"); console.log("--single-threaded -- Run single-threaded");
@ -320,10 +309,10 @@ function generateETag(filePath, stat) {
// Brute force-related // Brute force-related
var bruteForceDb = {}; var bruteForceDb = {};
// PBKDF2 cache // PBKDF2/scrypt cache
var pbkdf2Cache = []; var pbkdf2Cache = [];
var scryptCache = []; var scryptCache = [];
var pbkdf2CacheIntervalId = -1; var passwordHashCacheIntervalId = -1;
// SVR.JS worker spawn-related // SVR.JS worker spawn-related
var SVRJSInitialized = false; var SVRJSInitialized = false;
@ -3267,7 +3256,7 @@ if (!cluster.isPrimary) {
try { try {
decodedHref = decodeURIComponent(href); decodedHref = decodeURIComponent(href);
} catch (err) { } catch (err) {
// Return 400 error // Return an 400 error
callServerError(400); callServerError(400);
serverconsole.errmessage("Bad request!"); serverconsole.errmessage("Bad request!");
return; return;
@ -3278,7 +3267,7 @@ if (!cluster.isPrimary) {
return; return;
} }
// MOD EXCECUTION FUNCTION // Mod execution function
function modExecute(mods, ffinals) { function modExecute(mods, ffinals) {
// Prepare modFunction // Prepare modFunction
var modFunction = ffinals; var modFunction = ffinals;
@ -3296,7 +3285,7 @@ if (!cluster.isPrimary) {
modFunction = modO.callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", modFunction, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData); modFunction = modO.callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", modFunction, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData);
}); });
// Execute modfunction // Execute modFunction
modFunction(); modFunction();
} }
@ -4857,6 +4846,7 @@ function start(init) {
for (i = 0; i < logo.length; i++) console.log(logo[i]); // Print logo for (i = 0; i < logo.length; i++) console.log(logo[i]); // Print logo
console.log(); console.log();
console.log("Welcome to SVR.JS - a web server running on Node.JS"); console.log("Welcome to SVR.JS - a web server running on Node.JS");
// Print warnings // Print warnings
if (version.indexOf("Nightly-") === 0) serverconsole.locwarnmessage("This version is only for test purposes and may be unstable."); if (version.indexOf("Nightly-") === 0) serverconsole.locwarnmessage("This version is only for test purposes and may be unstable.");
if (configJSON.enableHTTP2 && !secure) serverconsole.locwarnmessage("HTTP/2 without HTTPS may not work in web browsers. Web browsers only support HTTP/2 with HTTPS!"); if (configJSON.enableHTTP2 && !secure) serverconsole.locwarnmessage("HTTP/2 without HTTPS may not work in web browsers. Web browsers only support HTTP/2 with HTTPS!");
@ -4899,7 +4889,7 @@ function start(init) {
if (SSJSError || modLoadingErrors.length > 0) console.log(); if (SSJSError || modLoadingErrors.length > 0) console.log();
} }
// Print info // Print server information
serverconsole.locmessage("Server version: " + version); serverconsole.locmessage("Server version: " + version);
if (process.isBun) serverconsole.locmessage("Bun version: v" + process.versions.bun); if (process.isBun) serverconsole.locmessage("Bun version: v" + process.versions.bun);
else serverconsole.locmessage("Node.JS version: " + process.version); else serverconsole.locmessage("Node.JS version: " + process.version);
@ -4923,7 +4913,7 @@ function start(init) {
if (sniReDos) throw new Error("Refusing to start, because the current SNI configuration would make the server vulnerable to ReDoS."); if (sniReDos) throw new Error("Refusing to start, because the current SNI configuration would make the server vulnerable to ReDoS.");
} }
// Information about starting the server // Print server startup information
if (!(secure && disableNonEncryptedServer)) serverconsole.locmessage("Starting HTTP server at " + (typeof port == "number" ? (listenAddress ? ((listenAddress.indexOf(":") > -1 ? "[" + listenAddress + "]" : listenAddress)) + ":" : "port ") : "") + port.toString() + "..."); if (!(secure && disableNonEncryptedServer)) serverconsole.locmessage("Starting HTTP server at " + (typeof port == "number" ? (listenAddress ? ((listenAddress.indexOf(":") > -1 ? "[" + listenAddress + "]" : listenAddress)) + ":" : "port ") : "") + port.toString() + "...");
if (secure) serverconsole.locmessage("Starting HTTPS server at " + (typeof sport == "number" ? (sListenAddress ? ((sListenAddress.indexOf(":") > -1 ? "[" + sListenAddress + "]" : sListenAddress)) + ":" : "port ") : "") + sport.toString() + "..."); if (secure) serverconsole.locmessage("Starting HTTPS server at " + (typeof sport == "number" ? (sListenAddress ? ((sListenAddress.indexOf(":") > -1 ? "[" + sListenAddress + "]" : sListenAddress)) + ":" : "port ") : "") + sport.toString() + "...");
} }
@ -5011,7 +5001,7 @@ function start(init) {
}, },
stop: function (retcode) { stop: function (retcode) {
reallyExiting = true; reallyExiting = true;
clearInterval(pbkdf2CacheIntervalId); clearInterval(passwordHashCacheIntervalId);
if ((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { if ((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) {
try { try {
server.close(function () { server.close(function () {
@ -5169,7 +5159,7 @@ function start(init) {
}, 300000); }, 300000);
} }
if (!cluster.isPrimary) { if (!cluster.isPrimary) {
pbkdf2CacheIntervalId = setInterval(function () { passwordHashCacheIntervalId = setInterval(function () {
pbkdf2Cache = pbkdf2Cache.filter(function (entry) { pbkdf2Cache = pbkdf2Cache.filter(function (entry) {
return entry.addDate > (new Date() - 3600000); return entry.addDate > (new Date() - 3600000);
}); });
@ -5732,9 +5722,3 @@ try {
process.exit(err.errno ? err.errno : 1); process.exit(err.errno ? err.errno : 1);
}, 10); }, 10);
} }
//////////////////////////////////
//// THE END! ////
//// WARNING: THE CODE HAS ////
//// 5000+ LINES! ////
//////////////////////////////////