fix: add option to remove custom headers

This commit is contained in:
Dorian Niemiec 2024-12-11 14:55:43 +01:00
parent 951a3ecffc
commit 128d9f8648

View file

@ -1,4 +1,4 @@
var fs = require("fs"); svar fs = require("fs");
var os = require("os"); var os = require("os");
var http = require("http"); var http = require("http");
var https = require("https"); var https = require("https");
@ -39,18 +39,21 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
var port = proxyConfig[matchingHostname].port; var port = proxyConfig[matchingHostname].port;
var secureHostname = proxyConfig[matchingHostname].secureHostname; var secureHostname = proxyConfig[matchingHostname].secureHostname;
var securePort = proxyConfig[matchingHostname].securePort; var securePort = proxyConfig[matchingHostname].securePort;
var removeCustomHeaders = proxyConfig[matchingHostname].removeCustomHeaders;
if (!port) port = 80; if (!port) port = 80;
if (!securePort && secureHostname) securePort = 443; if (!securePort && secureHostname) securePort = 443;
if (!hostname) { if (!hostname) {
callServerError(500, "reverse-proxy-mod/1.1.4", new Error("Proxy server is misconfigured. Hostname property is missing.")); callServerError(500, "reverse-proxy-mod/1.1.5", new Error("Proxy server is misconfigured. Hostname property is missing."));
return; return;
} }
if (removeCustomHeaders === undefined || removeCustomHeaders) {
try { try {
var x = res.getHeaderNames(); var x = res.getHeaderNames();
for (var i = 0; i < x.length; i++) { for (var i = 0; i < x.length; i++) {
res.removeHeader(x[i]); res.removeHeader(x[i]);
} }
} catch (ex) {} } catch (ex) {}
}
var preparedPath = req.url; var preparedPath = req.url;
if(isPath) { if(isPath) {
if(preparedPath == matchingHostname) { if(preparedPath == matchingHostname) {
@ -94,11 +97,11 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
}).on("error", (ex) => { }).on("error", (ex) => {
try { try {
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") { if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
callServerError(503, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(503, "reverse-proxy-mod/1.1.5", ex); //Server error
} else if (ex.code == "ETIMEDOUT") { } else if (ex.code == "ETIMEDOUT") {
callServerError(504, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(504, "reverse-proxy-mod/1.1.5", ex); //Server error
} else { } else {
callServerError(502, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(502, "reverse-proxy-mod/1.1.5", ex); //Server error
} }
} catch (ex) {} } catch (ex) {}
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
@ -133,17 +136,17 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
} catch (ex) {} } catch (ex) {}
}); });
} catch (ex) { } catch (ex) {
callServerError(502, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(502, "reverse-proxy-mod/1.1.5", ex); //Server error
} }
}); });
proxy.on("error", function (ex) { proxy.on("error", function (ex) {
try { try {
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") { if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
callServerError(503, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(503, "reverse-proxy-mod/1.1.5", ex); //Server error
} else if (ex.code == "ETIMEDOUT") { } else if (ex.code == "ETIMEDOUT") {
callServerError(504, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(504, "reverse-proxy-mod/1.1.5", ex); //Server error
} else { } else {
callServerError(502, "reverse-proxy-mod/1.1.4", ex); //Server error callServerError(502, "reverse-proxy-mod/1.1.5", ex); //Server error
} }
} catch (ex) {} } catch (ex) {}
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
@ -156,7 +159,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
}); });
} }
} else if ((href == "/reverse-proxy-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/reverse-proxy-config.json")) && path.normalize(__dirname + "/../../..") == process.cwd()) { } else if ((href == "/reverse-proxy-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/reverse-proxy-config.json")) && path.normalize(__dirname + "/../../..") == process.cwd()) {
callServerError(403, "reverse-proxy-mod/1.1.4"); callServerError(403, "reverse-proxy-mod/1.1.5");
} else { } else {
elseCallback(); elseCallback();
} }