feat: add support for multiple FastCGI servers

This commit is contained in:
Dorian Niemiec 2024-11-29 16:07:58 +01:00
parent d3baa550b9
commit bd40d6f822

View file

@ -21,9 +21,9 @@ try {
//GreenRhombus will not care about configJSONS in SVR.JS 3.x and newer //GreenRhombus will not care about configJSONS in SVR.JS 3.x and newer
//SVR.JS 2.x and older will fail to start with broken configuration file anyway... //SVR.JS 2.x and older will fail to start with broken configuration file anyway...
} }
var fastcgiConf = {}; var fastcgiConfO = {};
try { try {
fastcgiConf = JSON.parse(fs.readFileSync(__dirname + "/../../../greenrhombus-config.json")); fastcgiConfO = JSON.parse(fs.readFileSync(__dirname + "/../../../greenrhombus-config.json"));
} catch (ex) { } catch (ex) {
// Use defaults // Use defaults
} }
@ -338,10 +338,20 @@ function createFastCGIHandler(options) {
} }
// Load default configuration // Load default configuration
if (fastcgiConf.path !== undefined) fastcgiConf.path = fastcgiConf.path.replace(/([^\/])\/+$/, "$1"); if (fastcgiConfO.path !== undefined) fastcgiConfO.path = fastcgiConfO.path.replace(/([^\/])\/+$/, "$1");
if (fastcgiConf.socketPath === undefined) { if (fastcgiConfO.socketPath === undefined) {
if (fastcgiConf.host === undefined) fastcgiConf.host = "localhost"; if (fastcgiConfO.host === undefined) fastcgiConfO.host = "localhost";
if (fastcgiConf.port === undefined) fastcgiConf.port = 4000; if (fastcgiConfO.port === undefined) fastcgiConfO.port = 4000;
}
if (typeof fastcgiConfO.multiConfig == "object" && fastcgiConfO.multiConfig !== null) {
var fastcgiConfOMCK = Object.keys(fastcgiConfO.multiConfig);
for (var i = 0; i < fastcgiConfOMCK.length; i++) {
if (fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].path !== undefined) fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].path = fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].path.replace(/([^\/])\/+$/, "$1");
if (fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].socketPath === undefined) {
if (fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].host === undefined) fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].host = "localhost";
if (fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].port === undefined) fastcgiConfO.multiConfig[fastcgiConfOMCK[i]].port = 4000;
}
}
} }
var disableModExposeSupported = process.versions.svrjs && process.versions.svrjs.match(/^(?:Nightly-|(?:[4-9]|[123][0-9])[0-9]*\.|3\.(?:[1-9][0-9]+\.|9\.(?:[1-9])|4\.(?:(?:[3-9]|[12][0-9])[0-9]+|29)))/i); var disableModExposeSupported = process.versions.svrjs && process.versions.svrjs.match(/^(?:Nightly-|(?:[4-9]|[123][0-9])[0-9]*\.|3\.(?:[1-9][0-9]+\.|9\.(?:[1-9])|4\.(?:(?:[3-9]|[12][0-9])[0-9]+|29)))/i);
@ -404,7 +414,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
var abheaders = JSON.parse(JSON.stringify(bheaders)); var abheaders = JSON.parse(JSON.stringify(bheaders));
var socket = {}; var socket = {};
function executeFastCGI(req, res, dh, nEnv) { function executeFastCGI(req, res, dh, nEnv, fastcgiConf) {
// Function to execute FastCGI scripts // Function to execute FastCGI scripts
var env = JSON.parse(JSON.stringify(process.env)); var env = JSON.parse(JSON.stringify(process.env));
var nEnvKeys = Object.keys(nEnv); var nEnvKeys = Object.keys(nEnv);
@ -555,7 +565,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
else if(!handler.socket.destroyed) handlerConnection(); else if(!handler.socket.destroyed) handlerConnection();
} }
function executeFastCGIWithEnv(a, b, req, res, pubip, port, software, dh, user, cPath) { function executeFastCGIWithEnv(a, b, req, res, pubip, port, software, dh, user, cPath, fastcgiConf) {
// Function to set up environment variables and execute sCGI scripts // Function to set up environment variables and execute sCGI scripts
var nEnv = {}; var nEnv = {};
if (typeof user != "undefined") { if (typeof user != "undefined") {
@ -614,11 +624,30 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
for (var i = 0; i < nhKeys.length; i++) { for (var i = 0; i < nhKeys.length; i++) {
nEnv["HTTP_" + nhKeys[i].replace(/[^0-9A-Za-z]+/g, "_").toUpperCase()] = req.headers[nhKeys[i]]; nEnv["HTTP_" + nhKeys[i].replace(/[^0-9A-Za-z]+/g, "_").toUpperCase()] = req.headers[nhKeys[i]];
} }
executeFastCGI(req, res, dh, nEnv); executeFastCGI(req, res, dh, nEnv, fastcgiConf);
} }
var isScriptExt = scriptExts.indexOf("." + ext) != -1; var isScriptExt = scriptExts.indexOf("." + ext) != -1;
var fastcgiConf = fastcgiConfO;
if (fastcgiConfO.multiConfig) {
var hostnames = Object.keys(fastcgiConfO.multiConfig);
for (var i = 0; i < hostnames.length; i++) {
if (hostnames[i] == "*") {
fastcgiConf = fastcgiConfO.multiConfig["*"];
break;
} else if (req.headers.host && hostnames[i].indexOf("*.") == 0 && hostnames[i] != "*.") {
var hostnamesRoot = hostnames[i].substr(2);
if (req.headers.host == hostnamesRoot || (req.headers.host.length > hostnamesRoot.length && req.headers.host.indexOf("." + hostnamesRoot) == req.headers.host.length - hostnamesRoot.length - 1 )) {
fastcgiConf = fastcgiConfO.multiConfig[hostnames[i]];
break;
}
} else if (req.headers.host && req.headers.host == hostnames[i]) {
fastcgiConf = fastcgiConfO.multiConfig[hostnames[i]];
break;
}
}
}
if (fastcgiConf.path !== undefined && (href == fastcgiConf.path || href.indexOf(fastcgiConf.path + "/") == 0)) { if (fastcgiConf.path !== undefined && (href == fastcgiConf.path || href.indexOf(fastcgiConf.path + "/") == 0)) {
try { try {
@ -645,7 +674,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
version, version,
bheaders, bheaders,
authUser, authUser,
fastcgiConf.path fastcgiConf.path,
fastcgiConf
); );
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
@ -712,7 +742,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
version, version,
bheaders, bheaders,
authUser, authUser,
(decodeURIComponent(href) + "/index.php").replace(/\/+/g, "/") (decodeURIComponent(href) + "/index.php").replace(/\/+/g, "/"),
fastcgiConf
); );
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
@ -769,7 +800,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
version, version,
bheaders, bheaders,
authUser, authUser,
(decodeURIComponent(href) + "/index.cgi").replace(/\/+/g, "/") (decodeURIComponent(href) + "/index.cgi").replace(/\/+/g, "/"),
fastcgiConf
); );
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
@ -828,7 +860,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
version, version,
bheaders, bheaders,
authUser, authUser,
decodeURIComponent(href) decodeURIComponent(href),
fastcgiConf
); );
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
@ -911,7 +944,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
version, version,
bheaders, bheaders,
authUser, authUser,
pathp.fpth.substr(1) pathp.fpth.substr(1),
fastcgiConf
); );
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {