From 34b95402c2f895e1ed1caa1b9ddbabeebcfd4907 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Fri, 29 Nov 2024 16:08:37 +0100 Subject: [PATCH] feat: add support for multiple SCGI servers --- index.js | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 0a24893..3a1d8b5 100644 --- a/index.js +++ b/index.js @@ -18,18 +18,27 @@ try { //OrangeCircle 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... } -var scgiConf = {}; +var scgiConfO = {}; try { - scgiConf = JSON.parse(fs.readFileSync(__dirname + "/../../../orangecircle-config.json")); + scgiConfO = JSON.parse(fs.readFileSync(__dirname + "/../../../orangecircle-config.json")); } catch (ex) { // Use defaults } // Load default configuration -if (scgiConf.path === undefined) scgiConf.path = "/scgi"; -scgiConf.path = scgiConf.path.replace(/([^\/])\/+$/, "$1"); -if (scgiConf.host === undefined) scgiConf.host = "localhost"; -if (scgiConf.port === undefined) scgiConf.port = 4000; +if (scgiConfO.path === undefined) scgiConfO.path = "/scgi"; +scgiConfO.path = scgiConfO.path.replace(/([^\/])\/+$/, "$1"); +if (scgiConfO.host === undefined) scgiConfO.host = "localhost"; +if (scgiConfO.port === undefined) scgiConfO.port = 4000; +if (typeof scgiConfO.multiConfig == "object" && scgiConfO.multiConfig !== null) { + var scgiConfOMCK = Object.keys(scgiConfO.multiConfig); + for (var i = 0; i < scgiConfOMCK.length; i++) { + if (scgiConfO.multiConfig[scgiConfOMCK[i]].path === undefined) scgiConfO.multiConfig[scgiConfOMCK[i]].path = "/scgi"; + scgiConfO.multiConfig[scgiConfOMCK[i]].path = scgiConfO.multiConfig[scgiConfOMCK[i]].path.replace(/([^\/])\/+$/, "$1"); + if (scgiConfO.multiConfig[scgiConfOMCK[i]].host === undefined) scgiConfO.multiConfig[scgiConfOMCK[i]].host = "localhost"; + if (scgiConfO.multiConfig[scgiConfOMCK[i]].port === undefined) scgiConfO.multiConfig[scgiConfOMCK[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); @@ -91,7 +100,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e var abheaders = JSON.parse(JSON.stringify(bheaders)); var socket = {}; - function executeSCGI(req, res, dh, nEnv) { + function executeSCGI(req, res, dh, nEnv, scgiConf) { // Function to execute SCGI scripts var env = JSON.parse(JSON.stringify(process.env)); var nEnvKeys = Object.keys(nEnv); @@ -209,7 +218,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e } - function executeSCGIWithEnv(a, req, res, pubip, port, software, dh, user) { + function executeSCGIWithEnv(a, req, res, pubip, port, software, dh, user, scgiConf) { // Function to set up environment variables and execute sCGI scripts var b = href.replace(scgiConf.path,"") var nEnv = {}; @@ -269,7 +278,27 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e for (var i = 0; i < nhKeys.length; i++) { nEnv["HTTP_" + nhKeys[i].replace(/[^0-9A-Za-z]+/g, "_").toUpperCase()] = req.headers[nhKeys[i]]; } - executeSCGI(req, res, dh, nEnv); + executeSCGI(req, res, dh, nEnv, scgiConf); + } + + var scgiConf = scgiConfO; + if (scgiConfO.multiConfig) { + var hostnames = Object.keys(scgiConfO.multiConfig); + for (var i = 0; i < hostnames.length; i++) { + if (hostnames[i] == "*") { + scgiConf = scgiConfO.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 )) { + scgiConf = scgiConfO.multiConfig[hostnames[i]]; + break; + } + } else if (req.headers.host && req.headers.host == hostnames[i]) { + scgiConf = scgiConfO.multiConfig[hostnames[i]]; + break; + } + } } if (href == scgiConf.path || href.indexOf(scgiConf.path + "/") == 0) { @@ -295,7 +324,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e ") OrangeCircle/" + version, bheaders, - authUser + authUser, + scgiConf ); } catch (ex) { if (!callServerError) {