1
0
Fork 0
forked from svrjs/svrjs

Add option to rewrite "dirty" URLs

This commit is contained in:
Dorian Niemiec 2023-08-18 23:29:18 +02:00
parent bd475a2e8e
commit aedbd134f8
2 changed files with 30 additions and 4 deletions

View file

@ -3,7 +3,7 @@
"port": 80, "port": 80,
"pubport": 80, "pubport": 80,
"page404": "404.html", "page404": "404.html",
"timestamp": 1692388365140, "timestamp": 1692394031580,
"blacklist": [], "blacklist": [],
"nonStandardCodes": [], "nonStandardCodes": [],
"enableCompression": true, "enableCompression": true,
@ -88,5 +88,6 @@
"disableNonEncryptedServer": false, "disableNonEncryptedServer": false,
"disableToHTTPSRedirect": false, "disableToHTTPSRedirect": false,
"enableETag": true, "enableETag": true,
"disableUnusedWorkerTermination": false "disableUnusedWorkerTermination": false,
"rewriteDirtyURLs": true
} }

29
svr.js
View file

@ -993,6 +993,7 @@ var disableNonEncryptedServer = false;
var disableToHTTPSRedirect = false; var disableToHTTPSRedirect = false;
var nonStandardCodesRaw = []; var nonStandardCodesRaw = [];
var disableUnusedWorkerTermination = false; var disableUnusedWorkerTermination = false;
var rewriteDirtyURLs = false;
//Get properties from config.json //Get properties from config.json
if (configJSON.blacklist != undefined) rawBlackList = configJSON.blacklist; if (configJSON.blacklist != undefined) rawBlackList = configJSON.blacklist;
@ -1018,6 +1019,7 @@ if (configJSON.sni != undefined) sni = configJSON.sni;
if (configJSON.disableNonEncryptedServer != undefined) disableNonEncryptedServer = configJSON.disableNonEncryptedServer; if (configJSON.disableNonEncryptedServer != undefined) disableNonEncryptedServer = configJSON.disableNonEncryptedServer;
if (configJSON.disableToHTTPSRedirect != undefined) disableToHTTPSRedirect = configJSON.disableToHTTPSRedirect; if (configJSON.disableToHTTPSRedirect != undefined) disableToHTTPSRedirect = configJSON.disableToHTTPSRedirect;
if (configJSON.disableUnusedWorkerTermination != undefined) disableUnusedWorkerTermination = configJSON.disableUnusedWorkerTermination; if (configJSON.disableUnusedWorkerTermination != undefined) disableUnusedWorkerTermination = configJSON.disableUnusedWorkerTermination;
if (configJSON.rewriteDirtyURLs != undefined) rewriteDirtyURLs = configJSON.rewriteDirtyURLs;
if (configJSON.wwwroot != undefined) { if (configJSON.wwwroot != undefined) {
var wwwroot = configJSON.wwwroot; var wwwroot = configJSON.wwwroot;
if (cluster.isPrimary || cluster.isPrimary === undefined) process.chdir(wwwroot); if (cluster.isPrimary || cluster.isPrimary === undefined) process.chdir(wwwroot);
@ -4280,8 +4282,25 @@ if (!cluster.isPrimary) {
sanitizedURL.slashes = null; sanitizedURL.slashes = null;
sanitizedURL = url.format(sanitizedURL); sanitizedURL = url.format(sanitizedURL);
serverconsole.resmessage("URL sanitized: " + req.url + " => " + sanitizedURL); serverconsole.resmessage("URL sanitized: " + req.url + " => " + sanitizedURL);
redirect(sanitizedURL, false); if(rewriteDirtyURLs) {
return; req.url = sanitizedURL;
uobject = parseURL(req.url);
search = uobject.search;
href = uobject.pathname;
ext = path.extname(href).toLowerCase();
ext = ext.substr(1, ext.length);
try {
decodedHref = decodeURIComponent(href);
} catch (err) {
//Return 400 error
callServerError(400);
serverconsole.errmessage("Bad request!");
return;
}
} else {
redirect(sanitizedURL, false);
return;
}
} }
//URL REWRITING //URL REWRITING
@ -4329,6 +4348,11 @@ if (!cluster.isPrimary) {
rewrittenAgainURL.path = null; rewrittenAgainURL.path = null;
rewrittenAgainURL.href = null; rewrittenAgainURL.href = null;
rewrittenAgainURL.pathname = sHref; rewrittenAgainURL.pathname = sHref;
rewrittenAgainURL.hostname = null;
rewrittenAgainURL.host = null;
rewrittenAgainURL.port = null;
rewrittenAgainURL.protocol = null;
rewrittenAgainURL.slashes = null;
rewrittenAgainURL = url.format(rewrittenAgainURL); rewrittenAgainURL = url.format(rewrittenAgainURL);
serverconsole.resmessage("URL sanitized: " + req.url + " => " + rewrittenAgainURL); serverconsole.resmessage("URL sanitized: " + req.url + " => " + rewrittenAgainURL);
req.url = rewrittenAgainURL; req.url = rewrittenAgainURL;
@ -5510,6 +5534,7 @@ function saveConfig() {
if (configJSONobj.disableToHTTPSRedirect === undefined) configJSONobj.disableToHTTPSRedirect = false; if (configJSONobj.disableToHTTPSRedirect === undefined) configJSONobj.disableToHTTPSRedirect = false;
if (configJSONobj.enableETag === undefined) configJSONobj.enableETag = true; if (configJSONobj.enableETag === undefined) configJSONobj.enableETag = true;
if (configJSONobj.disableUnusedWorkerTermination === undefined) configJSONobj.disableUnusedWorkerTermination = false; if (configJSONobj.disableUnusedWorkerTermination === undefined) configJSONobj.disableUnusedWorkerTermination = false;
if (configJSONobj.rewriteDirtyURLs === undefined) configJSONobj.rewriteDirtyURLs = false;
var configString = JSON.stringify(configJSONobj, null, 2); var configString = JSON.stringify(configJSONobj, null, 2);
fs.writeFileSync(__dirname + "/config.json", configString); fs.writeFileSync(__dirname + "/config.json", configString);