forked from svrjs/svrjs
Add some code comments
This commit is contained in:
parent
d85dedea65
commit
9f15a08378
1 changed files with 36 additions and 19 deletions
55
svr.js
55
svr.js
|
@ -306,7 +306,7 @@ function generateETag(filePath, stat) {
|
||||||
return ETagDB[filePath + "-" + stat.size + "-" + stat.mtime];
|
return ETagDB[filePath + "-" + stat.size + "-" + stat.mtime];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Brute force-related
|
// Brute force protection-related
|
||||||
var bruteForceDb = {};
|
var bruteForceDb = {};
|
||||||
|
|
||||||
// PBKDF2/scrypt cache
|
// PBKDF2/scrypt cache
|
||||||
|
@ -321,6 +321,7 @@ var reallyExiting = false;
|
||||||
var crashed = false;
|
var crashed = false;
|
||||||
var threadLimitWarned = false;
|
var threadLimitWarned = false;
|
||||||
|
|
||||||
|
// SVR.JS worker forking function
|
||||||
function SVRJSFork() {
|
function SVRJSFork() {
|
||||||
// Log
|
// Log
|
||||||
if (SVRJSInitialized) serverconsole.locmessage("Starting next thread, because previous one hung up/crashed...");
|
if (SVRJSInitialized) serverconsole.locmessage("Starting next thread, because previous one hung up/crashed...");
|
||||||
|
@ -932,6 +933,7 @@ function calculateNetworkIPv4FromCidr(ipWithCidr) {
|
||||||
}).join(".");
|
}).join(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IP and network inteface-related
|
||||||
var ifaces = {};
|
var ifaces = {};
|
||||||
var ifaceEx = null;
|
var ifaceEx = null;
|
||||||
try {
|
try {
|
||||||
|
@ -942,11 +944,7 @@ try {
|
||||||
var ips = [];
|
var ips = [];
|
||||||
var brdIPs = ["255.255.255.255", "127.255.255.255", "0.255.255.255"];
|
var brdIPs = ["255.255.255.255", "127.255.255.255", "0.255.255.255"];
|
||||||
var netIPs = ["127.0.0.0"];
|
var netIPs = ["127.0.0.0"];
|
||||||
var attmts = 5;
|
|
||||||
var attmtsRedir = 5;
|
|
||||||
var errors = os.constants.errno;
|
|
||||||
var timestamp = new Date().getTime();
|
|
||||||
var wwwredirect = false;
|
|
||||||
Object.keys(ifaces).forEach(function (ifname) {
|
Object.keys(ifaces).forEach(function (ifname) {
|
||||||
var alias = 0;
|
var alias = 0;
|
||||||
ifaces[ifname].forEach(function (iface) {
|
ifaces[ifname].forEach(function (iface) {
|
||||||
|
@ -963,6 +961,7 @@ Object.keys(ifaces).forEach(function (ifname) {
|
||||||
alias++;
|
alias++;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ips.length == 0) {
|
if (ips.length == 0) {
|
||||||
Object.keys(ifaces).forEach(function (ifname) {
|
Object.keys(ifaces).forEach(function (ifname) {
|
||||||
var alias = 0;
|
var alias = 0;
|
||||||
|
@ -979,9 +978,20 @@ if (ips.length == 0) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Server startup attempt counter
|
||||||
|
var attmts = 5;
|
||||||
|
var attmtsRedir = 5;
|
||||||
|
|
||||||
|
// Some variables...
|
||||||
|
var errors = os.constants.errno;
|
||||||
|
var timestamp = new Date().getTime();
|
||||||
|
|
||||||
|
// Server IP address
|
||||||
var host = ips[(ips.length) - 1];
|
var host = ips[(ips.length) - 1];
|
||||||
if (!host) host = "[offline]";
|
if (!host) host = "[offline]";
|
||||||
|
|
||||||
|
// Public IP address-related
|
||||||
var ipRequestCompleted = false;
|
var ipRequestCompleted = false;
|
||||||
var ipRequestGotError = false;
|
var ipRequestGotError = false;
|
||||||
if (host != "[offline]" || ifaceEx) {
|
if (host != "[offline]" || ifaceEx) {
|
||||||
|
@ -1138,11 +1148,11 @@ if (fs.existsSync(__dirname + "/config.json")) {
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
configJSONRErr = err2;
|
configJSONRErr = err2;
|
||||||
// throw new Error("Cannot read JSON file.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default server configuration properties
|
// Default server configuration properties
|
||||||
|
var wwwredirect = false;
|
||||||
var rawBlackList = [];
|
var rawBlackList = [];
|
||||||
var users = [];
|
var users = [];
|
||||||
var page404 = "404.html";
|
var page404 = "404.html";
|
||||||
|
@ -1277,6 +1287,7 @@ try {
|
||||||
if (vnum === undefined) vnum = 0;
|
if (vnum === undefined) vnum = 0;
|
||||||
if (process.isBun) vnum = 64;
|
if (process.isBun) vnum = 64;
|
||||||
|
|
||||||
|
// SVR.JS path sanitizer function
|
||||||
function sanitizeURL(resource) {
|
function sanitizeURL(resource) {
|
||||||
if (resource == "*") return "*";
|
if (resource == "*") return "*";
|
||||||
if (resource == "") return "";
|
if (resource == "") return "";
|
||||||
|
@ -1309,6 +1320,7 @@ function sanitizeURL(resource) {
|
||||||
else return sanitizedResource;
|
else return sanitizedResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Node.JS mojibake URL fixing function
|
||||||
function fixNodeMojibakeURL(string) {
|
function fixNodeMojibakeURL(string) {
|
||||||
var encoded = "";
|
var encoded = "";
|
||||||
|
|
||||||
|
@ -1327,6 +1339,7 @@ function fixNodeMojibakeURL(string) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSL-related
|
||||||
var key = "";
|
var key = "";
|
||||||
var cert = "";
|
var cert = "";
|
||||||
|
|
||||||
|
@ -1481,7 +1494,7 @@ var serverconsole = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Wrap around process.exit, so log contents can flush.
|
// Wrap around process.exit, so that log contents can be flushed.
|
||||||
process.unsafeExit = process.exit;
|
process.unsafeExit = process.exit;
|
||||||
process.exit = function (code) {
|
process.exit = function (code) {
|
||||||
if (logFile && logFile.writable && !logFile.pending) {
|
if (logFile && logFile.writable && !logFile.pending) {
|
||||||
|
@ -1516,6 +1529,7 @@ process.exit = function (code) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// SVR.JS mod loader
|
||||||
var modLoadingErrors = [];
|
var modLoadingErrors = [];
|
||||||
var SSJSError = undefined;
|
var SSJSError = undefined;
|
||||||
|
|
||||||
|
@ -1820,6 +1834,7 @@ function sha256(s) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to get URL path for use in forbidden path adding.
|
||||||
function getInitializePath(to) {
|
function getInitializePath(to) {
|
||||||
var cwd = process.cwd();
|
var cwd = process.cwd();
|
||||||
if (os.platform() == "win32") {
|
if (os.platform() == "win32") {
|
||||||
|
@ -1836,6 +1851,7 @@ function getInitializePath(to) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to check if URL path name is a forbidden path.
|
||||||
function isForbiddenPath(decodedHref, match) {
|
function isForbiddenPath(decodedHref, match) {
|
||||||
var forbiddenPath = forbiddenPaths[match];
|
var forbiddenPath = forbiddenPaths[match];
|
||||||
if (!forbiddenPath) return false;
|
if (!forbiddenPath) return false;
|
||||||
|
@ -1850,6 +1866,7 @@ function isForbiddenPath(decodedHref, match) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to check if URL path name is index of one of defined forbidden paths.
|
||||||
function isIndexOfForbiddenPath(decodedHref, match) {
|
function isIndexOfForbiddenPath(decodedHref, match) {
|
||||||
var forbiddenPath = forbiddenPaths[match];
|
var forbiddenPath = forbiddenPaths[match];
|
||||||
if (!forbiddenPath) return false;
|
if (!forbiddenPath) return false;
|
||||||
|
@ -1864,7 +1881,6 @@ function isIndexOfForbiddenPath(decodedHref, match) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Set up forbidden paths
|
// Set up forbidden paths
|
||||||
var forbiddenPaths = {};
|
var forbiddenPaths = {};
|
||||||
|
|
||||||
|
@ -1942,7 +1958,7 @@ var serverErrorDescs = {
|
||||||
599: "The server couldn't connect in time while it was acting as a proxy."
|
599: "The server couldn't connect in time while it was acting as a proxy."
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create server
|
// Create server instances
|
||||||
if (!cluster.isPrimary) {
|
if (!cluster.isPrimary) {
|
||||||
var reqcounter = 0;
|
var reqcounter = 0;
|
||||||
var malformedcounter = 0;
|
var malformedcounter = 0;
|
||||||
|
@ -3364,7 +3380,7 @@ if (!cluster.isPrimary) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URL-related objects.
|
||||||
var uobject = parseURL(req.url);
|
var uobject = parseURL(req.url);
|
||||||
var search = uobject.search;
|
var search = uobject.search;
|
||||||
var href = uobject.pathname;
|
var href = uobject.pathname;
|
||||||
|
@ -3381,6 +3397,7 @@ if (!cluster.isPrimary) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
|
if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
|
||||||
|
// Expectations not met.
|
||||||
callServerError(417);
|
callServerError(417);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -3505,11 +3522,6 @@ if (!cluster.isPrimary) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
|
||||||
////THERE IS NO MORE "THE BOOK OF ZSOIE"!////
|
|
||||||
//// But it's in easteregg.tar.gz mod... ////
|
|
||||||
/////////////////////////////////////////////
|
|
||||||
|
|
||||||
var pth = decodeURIComponent(href).replace(/\/+/g, "/").substr(1);
|
var pth = decodeURIComponent(href).replace(/\/+/g, "/").substr(1);
|
||||||
var readFrom = "./" + pth;
|
var readFrom = "./" + pth;
|
||||||
fs.stat(readFrom, function (err, stats) {
|
fs.stat(readFrom, function (err, stats) {
|
||||||
|
@ -4263,7 +4275,7 @@ if (!cluster.isPrimary) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle redirects to addresses with www.
|
// Handle redirects to addresses with "www." prefix
|
||||||
if (wwwredirect) {
|
if (wwwredirect) {
|
||||||
var hostname = req.headers.host.split[":"];
|
var hostname = req.headers.host.split[":"];
|
||||||
var hostport = null;
|
var hostport = null;
|
||||||
|
@ -4872,12 +4884,14 @@ if (!cluster.isPrimary) {
|
||||||
|
|
||||||
var closedMaster = true;
|
var closedMaster = true;
|
||||||
|
|
||||||
|
// IPC listener for server listening signalization
|
||||||
function listenConnListener(msg) {
|
function listenConnListener(msg) {
|
||||||
if (msg == "\x12LISTEN") {
|
if (msg == "\x12LISTEN") {
|
||||||
listeningMessage();
|
listeningMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPC listener for brue force protection
|
||||||
function bruteForceListenerWrapper(worker) {
|
function bruteForceListenerWrapper(worker) {
|
||||||
return function bruteForceListener(message) {
|
return function bruteForceListener(message) {
|
||||||
var ip = "";
|
var ip = "";
|
||||||
|
@ -4912,6 +4926,7 @@ function bruteForceListenerWrapper(worker) {
|
||||||
var isWorkerHungUpBuff = true;
|
var isWorkerHungUpBuff = true;
|
||||||
var isWorkerHungUpBuff2 = true;
|
var isWorkerHungUpBuff2 = true;
|
||||||
|
|
||||||
|
// General IPC message listener
|
||||||
function msgListener(msg) {
|
function msgListener(msg) {
|
||||||
for (var i = 0; i < Object.keys(cluster.workers).length; i++) {
|
for (var i = 0; i < Object.keys(cluster.workers).length; i++) {
|
||||||
if (msg == "\x12END") {
|
if (msg == "\x12END") {
|
||||||
|
@ -5891,20 +5906,22 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
||||||
process.exit();
|
process.exit();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// Crash handler
|
||||||
process.on("uncaughtException", function (err) {
|
process.on("uncaughtException", function (err) {
|
||||||
// CRASH HANDLER
|
|
||||||
serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
|
serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
|
||||||
serverconsole.locerrmessage("Stack:");
|
serverconsole.locerrmessage("Stack:");
|
||||||
serverconsole.locerrmessage(generateErrorStack(err));
|
serverconsole.locerrmessage(generateErrorStack(err));
|
||||||
process.exit(err.errno);
|
process.exit(err.errno);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("unhandledRejection", function (err) {
|
process.on("unhandledRejection", function (err) {
|
||||||
// CRASH HANDLER
|
|
||||||
serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
|
serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
|
||||||
serverconsole.locerrmessage("Stack:");
|
serverconsole.locerrmessage("Stack:");
|
||||||
serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err));
|
serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err));
|
||||||
process.exit(err.errno);
|
process.exit(err.errno);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Warning handler
|
||||||
process.on("warning", function (warning) {
|
process.on("warning", function (warning) {
|
||||||
serverconsole.locwarnmessage(warning.message);
|
serverconsole.locwarnmessage(warning.message);
|
||||||
if (warning.stack) {
|
if (warning.stack) {
|
||||||
|
|
Reference in a new issue