1
0
Fork 0
forked from svrjs/svrjs

Add default handler checks, server status page, sizify() utility function, and move counters from core.js middleware to index.js.

This commit is contained in:
Dorian Niemiec 2024-08-25 09:44:53 +02:00
parent 8a54834276
commit 55d5efe54f
6 changed files with 142 additions and 5 deletions

View file

@ -19,6 +19,11 @@ process.filename = __filename;
//process.singleThreaded = false; //process.singleThreaded = false;
process.singleThreaded = true; process.singleThreaded = true;
process.err4xxcounter = 0;
process.err5xxcounter = 0;
process.reqcounter = 0;
process.malformedcounter = 0;
if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into process.versions if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into process.versions
let forceSecure = false; let forceSecure = false;
@ -202,9 +207,11 @@ let middleware = [
require("./middleware/responseHeaders.js"), require("./middleware/responseHeaders.js"),
require("./middleware/checkForbiddenPaths.js"), require("./middleware/checkForbiddenPaths.js"),
require("./middleware/nonStandardCodesAndHttpAuthentication.js"), require("./middleware/nonStandardCodesAndHttpAuthentication.js"),
require("./middleware/redirectTrailingSlashes.js") require("./middleware/redirectTrailingSlashes.js"),
// TODO: SVR.JS mods go here // TODO: SVR.JS mods go here
// TODO: default handler // TODO: default handler
require("./middleware/defaultHandlerChecks.js"),
require("./middleware/status.js")
]; ];
function addMiddleware(mw) { function addMiddleware(mw) {

View file

@ -8,10 +8,6 @@ const fixNodeMojibakeURL = require("../utils/urlMojibakeFixer.js");
const ipMatch = require("../utils/ipMatch.js"); const ipMatch = require("../utils/ipMatch.js");
const matchHostname = require("../utils/matchHostname.js"); const matchHostname = require("../utils/matchHostname.js");
if (!process.err4xxcounter) process.err4xxcounter = 0;
if (!process.err5xxcounter) process.err5xxcounter = 0;
if (!process.reqcounter) process.reqcounter = 0;
module.exports = (req, res, logFacilities, config, next) => { module.exports = (req, res, logFacilities, config, next) => {
config.generateServerString = () => { config.generateServerString = () => {
return generateServerString(config.exposeServerVersion); return generateServerString(config.exposeServerVersion);

View file

@ -0,0 +1,27 @@
const http = require("http");
module.exports = (req, res, logFacilities, config, next) => {
if (req.isProxy) {
let eheaders = config.getCustomHeaders();
eheaders["Content-Type"] = "text/html; charset=utf-8";
res.writeHead(501, http.STATUS_CODES[501], eheaders);
res.write("<!DOCTYPE html><html><head><title>Proxy not implemented</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:\"DejaVu Sans Mono\", \"Bitstream Vera Sans Mono\", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:' ';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body><h1>Proxy not implemented</h1><p>SVR.JS doesn't support proxy without proxy mod. If you're administator of this server, then install this mod in order to use SVR.JS as a proxy.</p><p><i>" + config.generateServerString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</i></p></body></html>");
res.end();
logFacilities.errmessage("SVR.JS doesn't support proxy without proxy mod.");
return;
}
if (req.method == "OPTIONS") {
let hdss = config.getCustomHeaders();
hdss["Allow"] = "GET, POST, HEAD, OPTIONS";
res.writeHead(204, http.STATUS_CODES[204], hdss);
res.end();
return;
} else if (req.method != "GET" && req.method != "POST" && req.method != "HEAD") {
res.error(405);
logFacilities.errmessage("Invaild method: " + req.method);
return;
}
next();
}

37
src/middleware/status.js Normal file
View file

@ -0,0 +1,37 @@
const http = require("http");
const os = require("os");
const sizify = require("../utils/sizify.js");
const svrjsInfo = require("../../svrjs.json");
const {name} = svrjsInfo;
module.exports = (req, res, logFacilities, config, next) => {
if (config.allowStatus && (req.parsedURL.pathname == "/svrjsstatus.svr" || (os.platform() == "win32" && req.parsedURL.pathname.toLowerCase() == "/svrjsstatus.svr"))) {
const formatRelativeTime = (relativeTime) => {
const days = Math.floor(relativeTime / 60 / (60 * 24));
const dateDiff = new Date(relativeTime * 1000);
return days + " days, " + dateDiff.getUTCHours() + " hours, " + dateDiff.getUTCMinutes() + " minutes, " + dateDiff.getUTCSeconds() + " seconds";
}
let statusBody = "";
statusBody += "Server version: " + config.generateServerString() + "<br/><hr/>";
//Those entries are just dates and numbers converted/formatted to strings, so no escaping is needed.
statusBody += "Current time: " + new Date().toString() + "<br/>Thread start time: " + new Date(new Date() - (process.uptime() * 1000)).toString() + "<br/>Thread uptime: " + formatRelativeTime(Math.floor(process.uptime())) + "<br/>";
statusBody += "OS uptime: " + formatRelativeTime(os.uptime()) + "<br/>";
statusBody += "Total request count: " + process.reqcounter + "<br/>";
statusBody += "Average request rate: " + (Math.round((process.reqcounter / process.uptime()) * 100) / 100) + " requests/s<br/>";
statusBody += "Client errors (4xx): " + process.err4xxcounter + "<br/>";
statusBody += "Server errors (5xx): " + process.err5xxcounter + "<br/>";
statusBody += "Average error rate: " + (Math.round(((process.err4xxcounter + process.err5xxcounter) / process.reqcounter) * 10000) / 100) + "%<br/>";
statusBody += "Malformed HTTP requests: " + process.malformedcounter;
if (process.memoryUsage) statusBody += "<br/>Memory usage of thread: " + sizify(process.memoryUsage().rss, true) + "B";
if (process.cpuUsage) statusBody += "<br/>Total CPU usage by thread: u" + (process.cpuUsage().user / 1000) + "ms s" + (process.cpuUsage().system / 1000) + "ms - " + (Math.round((((process.cpuUsage().user + process.cpuUsage().system) / 1000000) / process.uptime()) * 1000) / 1000) + "%";
statusBody += "<br/>Thread PID: " + process.pid + "<br/>";
res.writeHead(200, http.STATUS_CODES[200], {
"Content-Type": "text/html; charset=utf-8"
});
res.end((res.head == "" ? "<!DOCTYPE html><html><head><title>SVR.JS status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:\"DejaVu Sans Mono\", \"Bitstream Vera Sans Mono\", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:' ';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body>" : res.head.replace(/<head>/i, "<head><title>" + name.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + " status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title>")) + "<h1>" + name.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + " status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</h1>" + statusBody + (res.foot == "" ? "</body></html>" : res.foot));
return;
}
next();
}

13
src/utils/sizify.js Normal file
View file

@ -0,0 +1,13 @@
function sizify(bytes, addI) {
if (bytes == 0) return "0";
if (bytes < 0) bytes = -bytes;
const prefixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"];
let prefixIndex = Math.floor(Math.log2 ? Math.log2(bytes) / 10 : (Math.log(bytes) / (Math.log(2) * 10)));
if (prefixIndex >= prefixes.length - 1) prefixIndex = prefixes.length - 1;
let prefixIndexTranslated = Math.pow(2, 10 * prefixIndex);
let decimalPoints = 2 - Math.floor(Math.log10 ? Math.log10(bytes / prefixIndexTranslated) : (Math.log(bytes / prefixIndexTranslated) / Math.log(10)));
if (decimalPoints < 0) decimalPoints = 0;
return (Math.ceil((bytes / prefixIndexTranslated) * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints)) + prefixes[prefixIndex] + ((prefixIndex > 0 && addI) ? "i" : "");
}
module.exports = sizify;

View file

@ -0,0 +1,57 @@
const sizify = require('../../src/utils/sizify');
describe('"sizify" function', () => {
test('should return "0" for 0 bytes', () => {
expect(sizify(0)).toBe('0');
});
test('should handle negative bytes', () => {
expect(sizify(-1024)).toBe('1K');
});
test('should return correct size for small values', () => {
expect(sizify(1000)).toBe('1000');
expect(sizify(1024)).toBe('1K');
});
test('should return correct size for larger values', () => {
expect(sizify(1048576)).toBe('1M');
expect(sizify(1073741824)).toBe('1G');
expect(sizify(1099511627776)).toBe('1T');
expect(sizify(1125899906842624)).toBe('1P');
expect(sizify(1152921504606846976)).toBe('1E');
expect(sizify(1180591620717411303424)).toBe('1Z');
expect(sizify(1208925819614629174706176)).toBe('1Y');
expect(sizify(1237940039285380274899124224)).toBe('1R');
expect(sizify(1267650600228229401496703205376)).toBe('1Q');
});
test('should handle very large values', () => {
const largeValue = 2 ** 100; // A very large number
expect(sizify(largeValue)).toBe('1Q');
});
test('should add "i" suffix when addI is true', () => {
expect(sizify(1024, true)).toBe('1Ki');
expect(sizify(1048576, true)).toBe('1Mi');
expect(sizify(1073741824, true)).toBe('1Gi');
});
test('should not add "i" suffix when addI is false', () => {
expect(sizify(1024, false)).toBe('1K');
expect(sizify(1048576, false)).toBe('1M');
expect(sizify(1073741824, false)).toBe('1G');
});
test('should handle decimal points correctly', () => {
expect(sizify(1500)).toBe('1.47K');
expect(sizify(1500000)).toBe('1.44M');
expect(sizify(1500000000)).toBe('1.4G');
});
test('should handle edge cases', () => {
expect(sizify(1)).toBe('1');
expect(sizify(1023)).toBe('1023');
expect(sizify(1025)).toBe('1.01K');
});
});