1
0
Fork 0
forked from svrjs/svrjs

Cleaned up SVR.JS code

This commit is contained in:
Dorian Niemiec 2023-08-29 15:03:13 +02:00
parent 1ebf19d768
commit 71d1970571

48
svr.js
View file

@ -192,9 +192,9 @@ if (!singlethreaded) {
}); });
}; };
process.removeFakeIPC = function() { process.removeFakeIPC = function () {
// Close IPC server // Close IPC server
process.send = function() {}; process.send = function () {};
fakeIPCServer.close(); fakeIPCServer.close();
} }
} }
@ -228,7 +228,7 @@ if (!singlethreaded) {
} }
oldLog = console.log; oldLog = console.log;
console.log = function(a,b,c,d,e,f) { console.log = function (a,b,c,d,e,f) {
if(a == "ChildProcess.prototype.send() - Sorry! Not implemented yet") { if(a == "ChildProcess.prototype.send() - Sorry! Not implemented yet") {
throw new Error("NOT IMPLEMENTED"); throw new Error("NOT IMPLEMENTED");
} else { } else {
@ -755,7 +755,7 @@ function ipBlockList(rawBlockList) {
var ipParsedObject = (!isIPv6 ? ipv4ToInt : ipv6ToBlocks)(rawValue); var ipParsedObject = (!isIPv6 ? ipv4ToInt : ipv6ToBlocks)(rawValue);
var checkMethod = (!isIPv6 ? checkIfIPv4CIDRMatches : checkIfIPv6CIDRMatches); var checkMethod = (!isIPv6 ? checkIfIPv4CIDRMatches : checkIfIPv6CIDRMatches);
return instance.cidrs.some(function(iCidr) { return instance.cidrs.some(function (iCidr) {
return checkMethod(ipParsedObject, iCidr); return checkMethod(ipParsedObject, iCidr);
}); });
}; };
@ -875,14 +875,14 @@ if (host != "[offline]" || ifaceEx) {
} else { } else {
var callbackDone = false; var callbackDone = false;
var dnsTimeout = setTimeout(function() { var dnsTimeout = setTimeout(function () {
callbackDone = true; callbackDone = true;
ipRequestCompleted = true; ipRequestCompleted = true;
process.emit("ipRequestCompleted"); process.emit("ipRequestCompleted");
}, 3000); }, 3000);
try { try {
dns.reverse(pubip, function(err, hostnames) { dns.reverse(pubip, function (err, hostnames) {
if(callbackDone) return; if(callbackDone) return;
clearTimeout(dnsTimeout); clearTimeout(dnsTimeout);
if(!err && hostnames.length > 0) domain = hostnames[0]; if(!err && hostnames.length > 0) domain = hostnames[0];
@ -939,14 +939,14 @@ if (host != "[offline]" || ifaceEx) {
} else { } else {
var callbackDone = false; var callbackDone = false;
var dnsTimeout = setTimeout(function() { var dnsTimeout = setTimeout(function () {
callbackDone = true; callbackDone = true;
ipRequestCompleted = true; ipRequestCompleted = true;
process.emit("ipRequestCompleted"); process.emit("ipRequestCompleted");
}, 3000); }, 3000);
try { try {
dns.reverse(pubip, function(err, hostnames) { dns.reverse(pubip, function (err, hostnames) {
if(callbackDone) return; if(callbackDone) return;
clearTimeout(dnsTimeout); clearTimeout(dnsTimeout);
if(!err && hostnames.length > 0) domain = hostnames[0]; if(!err && hostnames.length > 0) domain = hostnames[0];
@ -2818,10 +2818,10 @@ if (!cluster.isPrimary) {
}); });
var modFunction = ffinals; var modFunction = ffinals;
proxyMods.reverse().forEach(function(proxyMod) { proxyMods.reverse().forEach(function (proxyMod) {
modFunction = proxyMod.proxyCallback(req, socket, head, configJSON, serverconsole, modFunction); modFunction = proxyMod.proxyCallback(req, socket, head, configJSON, serverconsole, modFunction);
}); });
modFunction(); modfunction ();
} }
function vres(req, socket, head, serverconsole) { function vres(req, socket, head, serverconsole) {
@ -3442,7 +3442,7 @@ if (!cluster.isPrimary) {
for (var i = mods.length - 1; i >= 0; i--) { for (var i = mods.length - 1; i >= 0; i--) {
modFunction = mods[i].callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, fd, modFunction, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData); modFunction = mods[i].callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, fd, modFunction, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData);
} }
modFunction(); modfunction ();
} }
var vresCalled = false; var vresCalled = false;
@ -4461,13 +4461,13 @@ if (!cluster.isPrimary) {
callServerError(500, undefined, new Error("SVR.JS doesn't support scrypt-hashed passwords on Node.JS versions without scrypt hash support.")); callServerError(500, undefined, new Error("SVR.JS doesn't support scrypt-hashed passwords on Node.JS versions without scrypt hash support."));
return; return;
} else { } else {
var cacheEntry = scryptCache.find(function(entry) { var cacheEntry = scryptCache.find(function (entry) {
return (entry.password == hashedPassword && entry.salt == list[_i].salt) return (entry.password == hashedPassword && entry.salt == list[_i].salt)
}); });
if(cacheEntry) { if(cacheEntry) {
cb(cacheEntry.hash); cb(cacheEntry.hash);
} else { } else {
crypto.scrypt(password, list[_i].salt, 64, function(err, derivedKey) { crypto.scrypt(password, list[_i].salt, 64, function (err, derivedKey) {
if(err) { if(err) {
callServerError(500, undefined, err); callServerError(500, undefined, err);
} else { } else {
@ -4483,13 +4483,13 @@ if (!cluster.isPrimary) {
callServerError(500, undefined, new Error("SVR.JS doesn't support PBKDF2-hashed passwords on Node.JS versions without crypto support.")); callServerError(500, undefined, new Error("SVR.JS doesn't support PBKDF2-hashed passwords on Node.JS versions without crypto support."));
return; return;
} else { } else {
var cacheEntry = pbkdf2Cache.find(function(entry) { var cacheEntry = pbkdf2Cache.find(function (entry) {
return (entry.password == hashedPassword && entry.salt == list[_i].salt) return (entry.password == hashedPassword && entry.salt == list[_i].salt)
}); });
if(cacheEntry) { if(cacheEntry) {
cb(cacheEntry.hash); cb(cacheEntry.hash);
} else { } else {
crypto.pbkdf2(password, list[_i].salt, 36250, 64, "sha512", function(err, derivedKey) { crypto.pbkdf2(password, list[_i].salt, 36250, 64, "sha512", function (err, derivedKey) {
if(err) { if(err) {
callServerError(500, undefined, err); callServerError(500, undefined, err);
} else { } else {
@ -4535,7 +4535,7 @@ if (!cluster.isPrimary) {
if(usernameMatch.length == 0) { if(usernameMatch.length == 0) {
usernameMatch.push({name: username, pass: "FAKEPASS", salt: "FAKESALT"}); //Fake credentials usernameMatch.push({name: username, pass: "FAKEPASS", salt: "FAKESALT"}); //Fake credentials
} }
checkIfPasswordMatches(usernameMatch, password, function(authorized) { checkIfPasswordMatches(usernameMatch, password, function (authorized) {
if (!authorized) { if (!authorized) {
if (bruteProtection) { if (bruteProtection) {
if (process.send) { if (process.send) {
@ -4853,8 +4853,8 @@ function start(init) {
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!");
if (process.isBun) { if (process.isBun) {
serverconsole.locwarnmessage("Bun support is experimental. Some features of SVR.JS, SVR.JS mods and SVR.JS server-side JavaScript may not work as expected."); serverconsole.locwarnmessage("Bun support is experimental. Some features of SVR.JS, SVR.JS mods and SVR.JS server-side JavaScript may not work as expected.");
if(users.some(function(entry) {return entry.pbkdf2;})) serverconsole.locwarnmessage("PBKDF2 password hashing function in Bun blocks the event loop, which may result in denial of service."); if(users.some(function (entry) {return entry.pbkdf2;})) serverconsole.locwarnmessage("PBKDF2 password hashing function in Bun blocks the event loop, which may result in denial of service.");
if(users.some(function(entry) {return entry.scrypt;})) serverconsole.locwarnmessage("scrypt password hashing function in Bun blocks the event loop, which may result in denial of service."); if(users.some(function (entry) {return entry.scrypt;})) serverconsole.locwarnmessage("scrypt password hashing function in Bun blocks the event loop, which may result in denial of service.");
} }
if (cluster.isPrimary === undefined) serverconsole.locwarnmessage("You're running SVR.JS on single thread. Reliability may suffer, as the server is stopped after crash."); if (cluster.isPrimary === undefined) serverconsole.locwarnmessage("You're running SVR.JS on single thread. Reliability may suffer, as the server is stopped after crash.");
if (crypto.__disabled__ !== undefined) serverconsole.locwarnmessage("Your Node.JS version doesn't have crypto support! The 'crypto' module is essential for providing cryptographic functionality in Node.JS. Without crypto support, certain security features may be unavailable, and some functionality may not work as expected. It's recommended to use a Node.JS version that includes crypto support to ensure the security and proper functioning of your server."); if (crypto.__disabled__ !== undefined) serverconsole.locwarnmessage("Your Node.JS version doesn't have crypto support! The 'crypto' module is essential for providing cryptographic functionality in Node.JS. Without crypto support, certain security features may be unavailable, and some functionality may not work as expected. It's recommended to use a Node.JS version that includes crypto support to ensure the security and proper functioning of your server.");
@ -4961,10 +4961,10 @@ function start(init) {
clearInterval(pbkdf2CacheIntervalId); clearInterval(pbkdf2CacheIntervalId);
if((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { if((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) {
try { try {
server.close(function() { server.close(function () {
if(server2.listening) { if(server2.listening) {
try { try {
server2.close(function() { server2.close(function () {
if(!process.removeFakeIPC) { if(!process.removeFakeIPC) {
if (typeof retcode == "number") { if (typeof retcode == "number") {
process.exit(retcode); process.exit(retcode);
@ -5082,10 +5082,10 @@ function start(init) {
} }
if (!cluster.isPrimary) { if (!cluster.isPrimary) {
pbkdf2CacheIntervalId = setInterval(function () { pbkdf2CacheIntervalId = setInterval(function () {
pbkdf2Cache = pbkdf2Cache.filter(function(entry) { pbkdf2Cache = pbkdf2Cache.filter(function (entry) {
return entry.addDate > (new Date() - 3600000); return entry.addDate > (new Date() - 3600000);
}); });
scryptCache = scryptCache.filter(function(entry) { scryptCache = scryptCache.filter(function (entry) {
return entry.addDate > (new Date() - 3600000); return entry.addDate > (new Date() - 3600000);
}); });
}, 1800000); }, 1800000);
@ -5414,7 +5414,7 @@ function start(init) {
isWorkerHungUpBuff = true; isWorkerHungUpBuff = true;
cluster.workers[allClusters[_id]].on("message", msgListener); cluster.workers[allClusters[_id]].on("message", msgListener);
cluster.workers[allClusters[_id]].send("\x14KILLPING"); cluster.workers[allClusters[_id]].send("\x14KILLPING");
setTimeout(function() { setTimeout(function () {
if (isWorkerHungUpBuff) { if (isWorkerHungUpBuff) {
checkWorker(callback, _id+1); checkWorker(callback, _id+1);
} else { } else {
@ -5434,7 +5434,7 @@ function start(init) {
checkWorker(callback, _id+1); checkWorker(callback, _id+1);
} }
} }
checkWorker(function() { checkWorker(function () {
if (goodWorkers.length > minClusters) { if (goodWorkers.length > minClusters) {
var wN = Math.floor(Math.random() * goodWorkers.length); var wN = Math.floor(Math.random() * goodWorkers.length);
if (wN == goodWorkers.length) return; if (wN == goodWorkers.length) return;