1
0
Fork 0
forked from svrjs/svrjs

Improved extension checking function and corrected 503 error description

This commit is contained in:
Dorian Niemiec 2023-08-12 12:35:46 +02:00
parent f37a565ca1
commit 61dead9b4a
2 changed files with 29 additions and 30 deletions

View file

@ -3,7 +3,7 @@
"port": 80, "port": 80,
"pubport": 80, "pubport": 80,
"page404": "404.html", "page404": "404.html",
"timestamp": 1691626302396, "timestamp": 1691836531788,
"blacklist": [], "blacklist": [],
"nonStandardCodes": [], "nonStandardCodes": [],
"enableCompression": true, "enableCompression": true,

57
svr.js
View file

@ -146,7 +146,7 @@ if (!singlethreaded) {
} }
// Cluster & IPC shim for Bun // Cluster & IPC shim for Bun
cluster.bunShim = function () { cluster.bunShim = function () {
cluster.isMaster = !process.env.NODE_UNIQUE_ID; cluster.isMaster = !process.env.NODE_UNIQUE_ID;
cluster.isPrimary = cluster.isMaster; cluster.isPrimary = cluster.isMaster;
@ -191,7 +191,7 @@ if (!singlethreaded) {
fakeIPCConnection.end(message); fakeIPCConnection.end(message);
}); });
}; };
process.removeFakeIPC = function() { process.removeFakeIPC = function() {
// Close IPC server // Close IPC server
process.send = function() {}; process.send = function() {};
@ -235,7 +235,7 @@ if (!singlethreaded) {
oldLog(a,b,c,d,e,f); oldLog(a,b,c,d,e,f);
} }
} }
try { try {
worker.send(undefined); worker.send(undefined);
} catch (err) { } catch (err) {
@ -243,7 +243,7 @@ if (!singlethreaded) {
sendImplemented = false; sendImplemented = false;
} }
} }
console.log = oldLog; console.log = oldLog;
return sendImplemented; return sendImplemented;
@ -252,7 +252,7 @@ if (!singlethreaded) {
if (!checkSendImplementation(newWorker)) { if (!checkSendImplementation(newWorker)) {
var net = require("net"); var net = require("net");
var os = require("os"); var os = require("os");
// Create a fake IPC server for worker process to receive messages // Create a fake IPC server for worker process to receive messages
var fakeWorkerIPCServer = net.createServer(function (socket) { var fakeWorkerIPCServer = net.createServer(function (socket) {
var receivedData = ""; var receivedData = "";
@ -297,7 +297,7 @@ if (!singlethreaded) {
if (process.isBun && (cluster.isMaster === undefined || (cluster.isMaster && process.env.NODE_UNIQUE_ID))) { if (process.isBun && (cluster.isMaster === undefined || (cluster.isMaster && process.env.NODE_UNIQUE_ID))) {
cluster.bunShim(); cluster.bunShim();
} }
// Shim cluster.isPrimary field // Shim cluster.isPrimary field
if (cluster.isPrimary === undefined && cluster.isMaster !== undefined) cluster.isPrimary = cluster.isMaster; if (cluster.isPrimary === undefined && cluster.isMaster !== undefined) cluster.isPrimary = cluster.isMaster;
} }
@ -331,7 +331,7 @@ function SVRJSFork() {
cluster.bunShim(); cluster.bunShim();
newWorker = cluster.fork(); newWorker = cluster.fork();
} else { } else {
throw err; throw err;
} }
} }
newWorker.on("error", function (err) { newWorker.on("error", function (err) {
@ -538,7 +538,7 @@ function createRegex(regex, isPath) {
//IP Block list object //IP Block list object
function ipBlockList(rawBlockList) { function ipBlockList(rawBlockList) {
// Initialize the instance with empty arrays // Initialize the instance with empty arrays
if (rawBlockList === undefined) rawBlockList = []; if (rawBlockList === undefined) rawBlockList = [];
var instance = { var instance = {
@ -756,7 +756,7 @@ function ipBlockList(rawBlockList) {
for (var i = 0; i < rawBlockList.length; i++) { for (var i = 0; i < rawBlockList.length; i++) {
instance.add(rawBlockList[i]); instance.add(rawBlockList[i]);
} }
return instance; return instance;
} }
@ -1277,7 +1277,7 @@ if (!disableMods) {
// If not the master process, create a unique modloader folder name for each worker // If not the master process, create a unique modloader folder name for each worker
modloaderFolderName = ".modloader_w" + Math.floor(Math.random() * 65536); modloaderFolderName = ".modloader_w" + Math.floor(Math.random() * 65536);
} }
// Define the temporary server-side JavaScript file name // Define the temporary server-side JavaScript file name
var tempServerSideScriptName = "serverSideScript.js"; var tempServerSideScriptName = "serverSideScript.js";
if (!process.isBun && cluster.isPrimary === false) { if (!process.isBun && cluster.isPrimary === false) {
@ -2602,7 +2602,7 @@ if (!cluster.isPrimary) {
return; return;
} }
} }
if (err.code && err.code.indexOf("ERR_HTTP2_") == 0) { if (err.code && err.code.indexOf("ERR_HTTP2_") == 0) {
serverconsole.errmessage("An HTTP/2 error occured: " + err.code); serverconsole.errmessage("An HTTP/2 error occured: " + err.code);
callServerError(400); callServerError(400);
@ -3085,7 +3085,7 @@ if (!cluster.isPrimary) {
} }
reqcounter++; reqcounter++;
if (!isProxy) serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + (request.headers.host == undefined ? "" : request.headers.host) + request.url); if (!isProxy) serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + (request.headers.host == undefined ? "" : request.headers.host) + request.url);
else serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + request.url); else serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + request.url);
if (request.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + request.headers["user-agent"]); if (request.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + request.headers["user-agent"]);
@ -3168,7 +3168,7 @@ if (!cluster.isPrimary) {
500: "The server had an unexpected error. Below, the error stack is shown: </p><code>{stack}</code><p>Please contact with developer/administrator at <i>{contact}</i>.", 500: "The server had an unexpected error. Below, the error stack is shown: </p><code>{stack}</code><p>Please contact with developer/administrator at <i>{contact}</i>.",
501: "The request requires use of a function, which isn't currently implemented by the server.", 501: "The request requires use of a function, which isn't currently implemented by the server.",
502: "The server had an error, while it was acting as a gateway.</p><p>Please contact with developer/administrator at <i>{contact}</i>.", 502: "The server had an error, while it was acting as a gateway.</p><p>Please contact with developer/administrator at <i>{contact}</i>.",
503: "The service provided by the server is currently unavailable, possibly due of maintenance downtime or capacity problems. Please try again later.</p><p>Please contact with developer/administrator at <i>{contact}</i>.", 503: "The service provided by the server is currently unavailable, possibly due to maintenance downtime or capacity problems. Please try again later.</p><p>Please contact with developer/administrator at <i>{contact}</i>.",
504: "The server couldn't get response in time, while it was acting as a gateway.</p><p>Please contact with developer/administrator at <i>{contact}</i>.", 504: "The server couldn't get response in time, while it was acting as a gateway.</p><p>Please contact with developer/administrator at <i>{contact}</i>.",
505: "The server doesn't support HTTP version used in the request.", 505: "The server doesn't support HTTP version used in the request.",
506: "Variant header is configured to be engaged in content negotiation.</p><p>Please contact with developer/administrator at <i>{contact}</i>.", 506: "Variant header is configured to be engaged in content negotiation.</p><p>Please contact with developer/administrator at <i>{contact}</i>.",
@ -3732,10 +3732,10 @@ if (!cluster.isPrimary) {
: (fs.existsSync("." + decodeURIComponent(href) + "/FOOT.html".replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) : (fs.existsSync("." + decodeURIComponent(href) + "/FOOT.html".replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/"))
? fs.readFileSync("." + decodeURIComponent(href) + "/FOOT.html".replace(/\/+/g, "/")).toString() ? fs.readFileSync("." + decodeURIComponent(href) + "/FOOT.html".replace(/\/+/g, "/")).toString()
: ""; : "";
// Check if custom header has HTML tag // Check if custom header has HTML tag
var headerHasHTMLTag = customDirListingHeader.replace(/<!--(?:(?:(?!--\>).)*|)(?:-->|$)/gs, "").match(/<html(?![a-zA-Z0-9])(?:"(?:\\(?:.|$)|[^\\"])*(?:"|$)|'(?:\\(?:.|$)|[^\\'])*(?:'|$)|[^'">])*(?:>|$)/si); var headerHasHTMLTag = customDirListingHeader.replace(/<!--(?:(?:(?!--\>).)*|)(?:-->|$)/gs, "").match(/<html(?![a-zA-Z0-9])(?:"(?:\\(?:.|$)|[^\\"])*(?:"|$)|'(?:\\(?:.|$)|[^\\'])*(?:'|$)|[^'">])*(?:>|$)/si);
// Generate HTML head and footer based on configuration and custom content // Generate HTML head and footer based on configuration and custom content
var htmlHead = (!configJSON.enableDirectoryListingWithDefaultHead || head == "" var htmlHead = (!configJSON.enableDirectoryListingWithDefaultHead || head == ""
? (!headerHasHTMLTag ? (!headerHasHTMLTag
@ -3815,8 +3815,7 @@ if (!cluster.isPrimary) {
getStatsForAllFiles(list, "." + decodeURIComponent(href), function (filelist) { getStatsForAllFiles(list, "." + decodeURIComponent(href), function (filelist) {
// Function to check file extension // Function to check file extension
function checkEXT(filename, ext) { function checkEXT(filename, ext) {
if (filename.length < ext.length) return false; return filename.match(new RegExp("\\." + ext.replace(/([.+*?^$()\[\]{}|\\])/,"\\$1") + "$","i"));
return filename.toLowerCase().indexOf(ext) === filename.length - ext.length;
} }
var directoryListingRows = []; var directoryListingRows = [];
@ -3947,12 +3946,12 @@ if (!cluster.isPrimary) {
callServerError(403); callServerError(403);
serverconsole.errmessage("Directory listing is disabled."); serverconsole.errmessage("Directory listing is disabled.");
} }
} else { } else {
var acceptEncoding = req.headers["accept-encoding"]; var acceptEncoding = req.headers["accept-encoding"];
if (!acceptEncoding) acceptEncoding = ""; if (!acceptEncoding) acceptEncoding = "";
// Check if the requested file exists and handle errors // Check if the requested file exists and handle errors
fs.stat(readFrom, function (err, stats) { fs.stat(readFrom, function (err, stats) {
if (err) { if (err) {
@ -3988,7 +3987,7 @@ if (!cluster.isPrimary) {
} }
var filelen = stats.size; var filelen = stats.size;
//ETag code //ETag code
var fileETag = undefined; var fileETag = undefined;
if (configJSON.enableETag == undefined || configJSON.enableETag) { if (configJSON.enableETag == undefined || configJSON.enableETag) {
@ -4012,7 +4011,7 @@ if (!cluster.isPrimary) {
return; return;
} }
} }
// Helper function to check if compression is allowed for the file // Helper function to check if compression is allowed for the file
function canCompress(path, list) { function canCompress(path, list) {
var canCompress = true; var canCompress = true;
@ -4070,7 +4069,7 @@ if (!cluster.isPrimary) {
rhd["Content-Length"] = end - begin + 1; rhd["Content-Length"] = end - begin + 1;
if (!(mime.contentType(ext) == false) && ext != "") rhd["Content-Type"] = mime.contentType(ext); if (!(mime.contentType(ext) == false) && ext != "") rhd["Content-Type"] = mime.contentType(ext);
if(fileETag) rhd["ETag"] = fileETag; if(fileETag) rhd["ETag"] = fileETag;
if (req.method != "HEAD") { if (req.method != "HEAD") {
var readStream = fs.createReadStream(readFrom, { var readStream = fs.createReadStream(readFrom, {
start: begin, start: begin,
@ -4256,7 +4255,7 @@ if (!cluster.isPrimary) {
redirect(sanitizedURL, false); redirect(sanitizedURL, false);
return; return;
} }
//URL REWRITING //URL REWRITING
function rewriteURL(address, map) { function rewriteURL(address, map) {
var rewrittenAddress = address; var rewrittenAddress = address;
@ -4321,7 +4320,7 @@ if (!cluster.isPrimary) {
} }
} }
} }
//Set response headers //Set response headers
if (!isProxy) { if (!isProxy) {
var hkh = getCustomHeaders(); var hkh = getCustomHeaders();
@ -4334,7 +4333,7 @@ if (!cluster.isPrimary) {
} }
} }
} }
//Check if path is forbidden //Check if path is forbidden
if ((isForbiddenPath(decodedHref, "config") || isForbiddenPath(decodedHref, "certificates")) && !isProxy) { if ((isForbiddenPath(decodedHref, "config") || isForbiddenPath(decodedHref, "certificates")) && !isProxy) {
callServerError(403); callServerError(403);
@ -4421,7 +4420,7 @@ if (!cluster.isPrimary) {
return; return;
} }
} }
// Handle HTTP authentication // Handle HTTP authentication
if (authIndex > -1) { if (authIndex > -1) {
var authcode = nonStandardCodes[authIndex]; var authcode = nonStandardCodes[authIndex];
@ -5118,7 +5117,7 @@ function start(init) {
rla.prompt(); rla.prompt();
}); });
} }
if (cluster.isPrimary || cluster.isPrimary === undefined) { if (cluster.isPrimary || cluster.isPrimary === undefined) {
//Cluster forking code //Cluster forking code
if (cluster.isPrimary !== undefined && init) { if (cluster.isPrimary !== undefined && init) {
@ -5187,7 +5186,7 @@ function start(init) {
if (msg == "\x12CRASH") process.exit(1); if (msg == "\x12CRASH") process.exit(1);
if (msg == "\x12EADDRINUSE" || msg == "\x12EADDRNOTAVAIL" || msg == "\x12EACCES") process.exit(errors[msg.substr(1)]); if (msg == "\x12EADDRINUSE" || msg == "\x12EADDRNOTAVAIL" || msg == "\x12EACCES") process.exit(errors[msg.substr(1)]);
}); });
// Hangup check and restart // Hangup check and restart
setInterval(function () { setInterval(function () {
if (!closedMaster && !exiting) { if (!closedMaster && !exiting) {
@ -5272,7 +5271,7 @@ function start(init) {
} }
} }
}, 4550); }, 4550);
// Termination of unused good workers // Termination of unused good workers
if(cluster.isPrimary !== undefined) { if(cluster.isPrimary !== undefined) {
setTimeout(function () { setTimeout(function () {