From ebe310eca69d1ea799e290789061024b70a8e57d Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sun, 3 Dec 2023 13:58:35 +0100 Subject: [PATCH] Clean up SVR.JS code --- svr.js | 542 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 283 insertions(+), 259 deletions(-) diff --git a/svr.js b/svr.js index 34c654b..ee188ca 100644 --- a/svr.js +++ b/svr.js @@ -1,4 +1,3 @@ - ///////////////////////////////////////// // // // S V R . J S // @@ -22,7 +21,7 @@ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ + */ // Check if SVR.JS is running on Node.JS-compatible runtime. If not, just error it out. if (typeof require === "undefined") { @@ -230,11 +229,11 @@ if (!singlethreaded) { } var oldLog = console.log; - console.log = function (a,b,c,d,e,f) { - if(a == "ChildProcess.prototype.send() - Sorry! Not implemented yet") { + console.log = function (a, b, c, d, e, f) { + if (a == "ChildProcess.prototype.send() - Sorry! Not implemented yet") { throw new Error("NOT IMPLEMENTED"); } else { - oldLog(a,b,c,d,e,f); + oldLog(a, b, c, d, e, f); } }; @@ -312,8 +311,9 @@ if (!singlethreaded) { // ETag-related var ETagDB = {}; + function generateETag(filePath, stat) { - if(!ETagDB[filePath + "-" + stat.size + "-" + stat.mtime]) ETagDB[filePath + "-" + stat.size + "-" + stat.mtime] = sha256(filePath + "-" + stat.size + "-" + stat.mtime); + if (!ETagDB[filePath + "-" + stat.size + "-" + stat.mtime]) ETagDB[filePath + "-" + stat.size + "-" + stat.mtime] = sha256(filePath + "-" + stat.size + "-" + stat.mtime); return ETagDB[filePath + "-" + stat.size + "-" + stat.mtime]; } @@ -348,7 +348,7 @@ function SVRJSFork() { if (SVRJSInitialized) serverconsole.locwarnmessage("SVR.JS limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer."); } } catch (err) { - if(err.name == "NotImplementedError") { + if (err.name == "NotImplementedError") { // If cluster.fork throws a NotImplementedError, shim cluster module cluster.bunShim(); if (!threadLimitWarned && cluster.__shimmed__ && process.isBun && process.versions.bun && process.versions.bun[0] != "0") { @@ -366,9 +366,9 @@ function SVRJSFork() { } // Add event listeners - if(newWorker.on) { + if (newWorker.on) { newWorker.on("error", function (err) { - if(!reallyExiting) serverconsole.locwarnmessage("There was a problem when handling SVR.JS worker! (from master process side) Reason: " + err.message); + if (!reallyExiting) serverconsole.locwarnmessage("There was a problem when handling SVR.JS worker! (from master process side) Reason: " + err.message); }); newWorker.on("exit", function () { if (!exiting && Object.keys(cluster.workers).length == 0) { @@ -507,9 +507,9 @@ function sizify(bytes, addI) { var 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; var prefixIndexTranslated = Math.pow(2, 10 * prefixIndex); - var decimalPoints = 2 - Math.floor(Math.log10 ? Math.log10(bytes/prefixIndexTranslated) : (Math.log(bytes/prefixIndexTranslated) / Math.log(10))); + var 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" : ""); + return (Math.ceil((bytes / prefixIndexTranslated) * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints)) + prefixes[prefixIndex] + ((prefixIndex > 0 && addI) ? "i" : ""); } function getOS() { @@ -566,17 +566,17 @@ function checkForEnabledDirectoryListing(hostname) { } var main = (configJSON.enableDirectoryListing || configJSON.enableDirectoryListing === undefined); - if(!configJSON.enableDirectoryListingVHost) return main; + if (!configJSON.enableDirectoryListingVHost) return main; var vhostP = null; - configJSON.enableDirectoryListingVHost.every(function(vhost) { - if(matchHostname(vhost.host)) { + configJSON.enableDirectoryListingVHost.every(function (vhost) { + if (matchHostname(vhost.host)) { vhostP = vhost; return false; } else { return true; } }); - if(!vhostP || vhostP.enabled === undefined) return main; + if (!vhostP || vhostP.enabled === undefined) return main; else return vhostP.enabled; } @@ -625,7 +625,7 @@ function ipBlockList(rawBlockList) { groupsPresent += side.split(":").length; }); fullAddress += sides[0] + ":"; - if(validGroupCount - groupsPresent > 1) { + if (validGroupCount - groupsPresent > 1) { fullAddress += "0000:".repeat(validGroupCount - groupsPresent); } fullAddress += sides[1]; @@ -813,8 +813,8 @@ function generateErrorStack(errorObject) { // If the error stack starts with the error name, return the original stack (it is V8-style then). if (errorStack.some(function (errorStackLine) { - return (errorStackLine.indexOf(errorObject.name) == 0); - })) { + return (errorStackLine.indexOf(errorObject.name) == 0); + })) { return errorObject.stack; } @@ -841,35 +841,35 @@ function generateErrorStack(errorObject) { function calculateBroadcastIPv4FromCidr(ipWithCidr) { // Check if CIDR notation is valid, if it's not, return null - if(!ipWithCidr) return null; + if (!ipWithCidr) return null; var ipCA = ipWithCidr.split("/"); - if(ipCA.length != 2) return null; + if (ipCA.length != 2) return null; // Extract IP and mask (numberic format) var ip = ipCA[0]; var mask = parseInt(ipCA[1]); - return ip.split(".").map(function(num, index) { + return ip.split(".").map(function (num, index) { // Calculate resulting 8-bit - var power = Math.max(Math.min(mask - (index*8), 8), 0); - return ((parseInt(num) & ((Math.pow(2,power)-1) << (8-power))) | Math.pow(2,8-power)-1).toString(); + var power = Math.max(Math.min(mask - (index * 8), 8), 0); + return ((parseInt(num) & ((Math.pow(2, power) - 1) << (8 - power))) | Math.pow(2, 8 - power) - 1).toString(); }).join("."); } function calculateNetworkIPv4FromCidr(ipWithCidr) { // Check if CIDR notation is valid, if it's not, return null - if(!ipWithCidr) return null; + if (!ipWithCidr) return null; var ipCA = ipWithCidr.split("/"); - if(ipCA.length != 2) return null; + if (ipCA.length != 2) return null; // Extract IP and mask (numberic format) var ip = ipCA[0]; var mask = parseInt(ipCA[1]); - return ip.split(".").map(function(num, index) { + return ip.split(".").map(function (num, index) { // Calculate resulting 8-bit - var power = Math.max(Math.min(mask - (index*8), 8), 0); - return ((parseInt(num) & ((Math.pow(2,power)-1) << (8-power)))).toString(); + var power = Math.max(Math.min(mask - (index * 8), 8), 0); + return ((parseInt(num) & ((Math.pow(2, power) - 1) << (8 - power)))).toString(); }).join("."); } @@ -957,13 +957,13 @@ if (host != "[offline]" || ifaceEx) { try { dns.reverse(pubip, function (err, hostnames) { - if(callbackDone) return; + if (callbackDone) return; clearTimeout(dnsTimeout); - if(!err && hostnames.length > 0) domain = hostnames[0]; + if (!err && hostnames.length > 0) domain = hostnames[0]; ipRequestCompleted = true; process.emit("ipRequestCompleted"); }); - } catch(err) { + } catch (err) { clearTimeout(dnsTimeout); callbackDone = true; ipRequestCompleted = true; @@ -1021,13 +1021,13 @@ if (host != "[offline]" || ifaceEx) { try { dns.reverse(pubip, function (err, hostnames) { - if(callbackDone) return; + if (callbackDone) return; clearTimeout(dnsTimeout); - if(!err && hostnames.length > 0) domain = hostnames[0]; + if (!err && hostnames.length > 0) domain = hostnames[0]; ipRequestCompleted = true; process.emit("ipRequestCompleted"); }); - } catch(err) { + } catch (err) { clearTimeout(dnsTimeout); callbackDone = true; ipRequestCompleted = true; @@ -1112,12 +1112,12 @@ if (configJSON.wwwredirect != undefined) wwwredirect = configJSON.wwwredirect; if (configJSON.port != undefined) port = configJSON.port; if (configJSON.pubport != undefined) pubport = configJSON.pubport; if (typeof port === "string") { - if(port.match(/^[0-9]+$/)) { + if (port.match(/^[0-9]+$/)) { port = parseInt(port); } else { var portLMatch = port.match(/^(\[[^ \]@\/\\]+\]|[^ \]\[:@\/\\]+):([0-9]+)$/); - if(portLMatch) { - listenAddress = portLMatch[1].replace(/^\[|\]$/g,"").replace(/^::ffff:/i,""); + if (portLMatch) { + listenAddress = portLMatch[1].replace(/^\[|\]$/g, "").replace(/^::ffff:/i, ""); port = parseInt(portLMatch[2]); } } @@ -1126,12 +1126,12 @@ if (configJSON.domian != undefined) domain = configJSON.domian; if (configJSON.domain != undefined) domain = configJSON.domain; if (configJSON.sport != undefined) sport = configJSON.sport; if (typeof sport === "string") { - if(sport.match(/^[0-9]+$/)) { + if (sport.match(/^[0-9]+$/)) { sport = parseInt(sport); } else { var sportLMatch = sport.match(/^(\[[^ \]@\/\\]+\]|[^ \]\[:@\/\\]+):([0-9]+)$/); - if(sportLMatch) { - sListenAddress = sportLMatch[1].replace(/^\[|\]$/g,"").replace(/^::ffff:/i,""); + if (sportLMatch) { + sListenAddress = sportLMatch[1].replace(/^\[|\]$/g, "").replace(/^::ffff:/i, ""); sport = parseInt(sportLMatch[2]); } } @@ -1162,15 +1162,15 @@ if (configJSON.environmentVariables != undefined) environmentVariables = configJ var wwwrootError = null; try { if (cluster.isPrimary || cluster.isPrimary === undefined) process.chdir(configJSON.wwwroot != undefined ? configJSON.wwwroot : __dirname); -} catch(err) { +} catch (err) { wwwrootError = err; } try { - Object.keys(environmentVariables).forEach(function(key) { + Object.keys(environmentVariables).forEach(function (key) { process.env[key] = environmentVariables[key]; }); -} catch(err) { +} catch (err) { // Failed to set environment variables. } @@ -1218,7 +1218,7 @@ function sanitizeURL(resource) { if (resource == "*") return "*"; if (resource == "") return ""; // Remove null characters - resource = resource.replace(/%00/ig, "").replace(/\0/g,""); + resource = resource.replace(/%00/ig, "").replace(/\0/g, ""); // Check if URL is malformed (e.g. %c0%af or %u002f or simply %as) if (resource.match(/%(?:c[01]|f[ef]|(?![0-9a-f]{2}).{2}|.{0,1}$)/gi)) throw new URIError("URI malformed"); // Decode URL-encoded characters while preserving certain characters @@ -1251,7 +1251,7 @@ function fixNodeMojibakeURL(string) { //Encode URLs Buffer.from(string, "latin1").forEach(function (value) { - if(value > 127) { + if (value > 127) { encoded += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase(); } else { encoded += String.fromCodePoint(value); @@ -1297,7 +1297,7 @@ if (secure) { key: fs.readFileSync((sni[sniName].key[0] != "/" && !sni[sniName].key.match(/^[A-Z0-9]:\\/)) ? __dirname + "/" + sni[sniName].key : sni[sniName].key).toString() }); }); - } catch(err) { + } catch (err) { certificateError = err; } } @@ -1558,7 +1558,10 @@ if (!disableMods) { } } } catch (err) { - modLoadingErrors.push({error: err, modName: modFileRaw}); + modLoadingErrors.push({ + error: err, + modName: modFileRaw + }); } } }); @@ -2039,7 +2042,7 @@ if (!cluster.isPrimary) { } else { try { process.send("\x12ERRLIST" + attmtsRedir + err.code); - } catch(ex) { + } catch (ex) { // Probably main process exited } } @@ -2049,12 +2052,12 @@ if (!cluster.isPrimary) { } else { try { if (cluster.isPrimary !== undefined) process.send("\x12ERRCRASH" + err.code); - } catch(ex) { + } catch (ex) { // Probably main process exited } setTimeout(function () { var errno = errors[err.code]; - if(errno) { + if (errno) { process.exit(errno); } else { process.exit(1); @@ -2327,45 +2330,46 @@ if (!cluster.isPrimary) { // Server error calling method function callServerError(errorCode, extName, stack, ch) { function getErrorFileName(list, callback, _i) { - if(err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) { + if (err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) { // Disable custom error page for HTTP SSL error callback(errorCode.toString() + ".html"); return; } + function medCallback(p) { - if(p) callback(p); + if (p) callback(p); else { - if(errorCode == 404) { - fs.access(page404, fs.constants.F_OK, function(err) { - if(err) { - fs.access("." + errorCode.toString(), fs.constants.F_OK, function(err) { + if (errorCode == 404) { + fs.access(page404, fs.constants.F_OK, function (err) { + if (err) { + fs.access("." + errorCode.toString(), fs.constants.F_OK, function (err) { try { - if(err) { + if (err) { callback(errorCode.toString() + ".html"); } else { callback("." + errorCode.toString()); } - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } }); } else { try { callback(page404); - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } } }); } else { - fs.access("." + errorCode.toString(), fs.constants.F_OK, function(err) { + fs.access("." + errorCode.toString(), fs.constants.F_OK, function (err) { try { - if(err) { + if (err) { callback(errorCode.toString() + ".html"); } else { callback("." + errorCode.toString()); } - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } }); @@ -2373,19 +2377,19 @@ if (!cluster.isPrimary) { } } - if(!_i) _i = 0; - if(_i >= list.length) { + if (!_i) _i = 0; + if (_i >= list.length) { medCallback(false); return; } - if(list[_i].scode != errorCode) { - getErrorFileName(list, callback, _i+1); + if (list[_i].scode != errorCode) { + getErrorFileName(list, callback, _i + 1); return; } else { - fs.access(list[_i].path, fs.constants.F_OK, function(err) { - if(err) { - getErrorFileName(list, callback, _i+1); + fs.access(list[_i].path, fs.constants.F_OK, function (err) { + if (err) { + getErrorFileName(list, callback, _i + 1); } else { medCallback(list[_i].path); } @@ -2393,7 +2397,7 @@ if (!cluster.isPrimary) { } } - getErrorFileName(errorPages, function(errorFile) { + getErrorFileName(errorPages, function (errorFile) { if (Object.prototype.toString.call(stack) === "[object Error]") stack = generateErrorStack(stack); if (stack === undefined) stack = generateErrorStack(new Error("Unknown error")); if (errorCode == 500 || errorCode == 502) { @@ -2421,7 +2425,7 @@ if (!cluster.isPrimary) { } cheaders["Content-Type"] = "text/html; charset=utf-8"; if (errorCode == 405 && !cheaders["Allow"]) cheaders["Allow"] = "GET, POST, HEAD, OPTIONS"; - if(err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) { + if (err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) { // Disable custom error page for HTTP SSL error res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.write(("{errorMessage}

{errorMessage}

{errorDesc}

{server}

").replace(/{errorMessage}/g, errorCode.toString() + " " + http.STATUS_CODES[errorCode]).replace(/{errorDesc}/g, serverErrorDescs[errorCode]).replace(/{stack}/g, stack.replace(/&/g, "&").replace(//g, ">").replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, "  ")).replace(/{server}/g, "" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + ((!exposeModsInErrorPages || extName == undefined) ? "" : " " + extName)).replace(/{contact}/g, serverAdmin.replace(/\./g, "[dot]").replace(/@/g, "[at]"))); @@ -2770,17 +2774,17 @@ if (!cluster.isPrimary) { function getCustomHeaders() { var ph = JSON.parse(JSON.stringify(customHeaders)); - if(configJSON.customHeadersVHost) { + if (configJSON.customHeadersVHost) { var vhostP = null; - configJSON.customHeadersVHost.every(function(vhost) { - if(matchHostname(vhost.host)) { + configJSON.customHeadersVHost.every(function (vhost) { + if (matchHostname(vhost.host)) { vhostP = vhost; return false; } else { return true; } }); - if(vhostP && vhostP.headers) { + if (vhostP && vhostP.headers) { var phNu = JSON.parse(JSON.stringify(vhostP.headers)); Object.keys(phNu).forEach(function (phNuK) { ph[phNuK] = phNu[phNuK]; @@ -2811,7 +2815,7 @@ if (!cluster.isPrimary) { delete table["connection"]; delete table["keep-alive"]; delete table["upgrade"]; - if(res.stream && res.stream.destroyed) { + if (res.stream && res.stream.destroyed) { return false; } else { return res.writeHeadNodeApi(a, table); @@ -2876,8 +2880,8 @@ if (!cluster.isPrimary) { } else { serverconsole.resmessage("Server responded with " + a.toString() + " code."); } - if(typeof b != "string" && http.STATUS_CODES[a]) { - if(!c) c = b; + if (typeof b != "string" && http.STATUS_CODES[a]) { + if (!c) c = b; b = http.STATUS_CODES[a]; } lastStatusCode = a; @@ -2913,11 +2917,11 @@ if (!cluster.isPrimary) { var oldip = ""; var oldport = ""; var isForwardedValid = true; - if(enableIPSpoofing) { + if (enableIPSpoofing) { if (req.headers["x-forwarded-for"] != undefined) { var preparedReqIP = req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, ""); var preparedReqIPvalid = net.isIP(preparedReqIP); - if(preparedReqIPvalid) { + if (preparedReqIPvalid) { if (preparedReqIPvalid == 4 && req.socket.remoteAddress && req.socket.remoteAddress.indexOf(":") > -1) preparedReqIP = "::ffff:" + preparedReqIP; reqip = preparedReqIP; reqport = null; @@ -2984,39 +2988,39 @@ if (!cluster.isPrimary) { // Determine error file function getErrorFileName(list, callback, _i) { function medCallback(p) { - if(p) callback(p); + if (p) callback(p); else { - if(errorCode == 404) { - fs.access(page404, fs.constants.F_OK, function(err) { - if(err) { - fs.access("." + errorCode.toString(), fs.constants.F_OK, function(err) { + if (errorCode == 404) { + fs.access(page404, fs.constants.F_OK, function (err) { + if (err) { + fs.access("." + errorCode.toString(), fs.constants.F_OK, function (err) { try { - if(err) { + if (err) { callback(errorCode.toString() + ".html"); } else { callback("." + errorCode.toString()); } - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } }); } else { try { callback(page404); - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } } }); } else { - fs.access("." + errorCode.toString(), fs.constants.F_OK, function(err) { + fs.access("." + errorCode.toString(), fs.constants.F_OK, function (err) { try { - if(err) { + if (err) { callback(errorCode.toString() + ".html"); } else { callback("." + errorCode.toString()); } - } catch(err2) { + } catch (err2) { callServerError(500, undefined, generateErrorStack(err2)); } }); @@ -3024,19 +3028,19 @@ if (!cluster.isPrimary) { } } - if(!_i) _i = 0; - if(_i >= list.length) { + if (!_i) _i = 0; + if (_i >= list.length) { medCallback(false); return; } - if(list[_i].scode != errorCode || !matchHostname(list[_i].host)) { - getErrorFileName(list, callback, _i+1); + if (list[_i].scode != errorCode || !matchHostname(list[_i].host)) { + getErrorFileName(list, callback, _i + 1); return; } else { - fs.access(list[_i].path, fs.constants.F_OK, function(err) { - if(err) { - getErrorFileName(list, callback, _i+1); + fs.access(list[_i].path, fs.constants.F_OK, function (err) { + if (err) { + getErrorFileName(list, callback, _i + 1); } else { medCallback(list[_i].path); } @@ -3044,7 +3048,7 @@ if (!cluster.isPrimary) { } } - getErrorFileName(errorPages, function(errorFile) { + getErrorFileName(errorPages, function (errorFile) { // Generate error stack if not provided if (Object.prototype.toString.call(stack) === "[object Error]") stack = generateErrorStack(stack); if (stack === undefined) stack = generateErrorStack(new Error("Unknown error")); @@ -3173,7 +3177,7 @@ if (!cluster.isPrimary) { form.parse(req, function (err, fields, files) { // If there was an error, call the server error function with status code determined by error if (err) { - if(err.httpCode) callServerError(err.httpCode); + if (err.httpCode) callServerError(err.httpCode); else callServerError(400); return; } @@ -3264,7 +3268,7 @@ if (!cluster.isPrimary) { var modFunction = ffinals; var useMods = mods.slice(); - if(isProxy) { + if (isProxy) { // Get list of forward proxy mods useMods = []; mods.forEach(function (mod) { @@ -3418,16 +3422,16 @@ if (!cluster.isPrimary) { fs.stat((readFrom + "/index.htm").replace(/\/+/g, "/"), function (e, s) { if (e || !s.isFile()) { fs.stat((readFrom + "/index.xhtml").replace(/\/+/g, "/"), function (e, s) { - if (e || !s.isFile()) { - properDirectoryListingAndStaticFileServe(); - } else { - stats = s; - pth = (pth + "/index.xhtml").replace(/\/+/g, "/"); - ext = "xhtml"; - readFrom = "./" + pth; - properDirectoryListingAndStaticFileServe(); - } - }); + if (e || !s.isFile()) { + properDirectoryListingAndStaticFileServe(); + } else { + stats = s; + pth = (pth + "/index.xhtml").replace(/\/+/g, "/"); + ext = "xhtml"; + readFrom = "./" + pth; + properDirectoryListingAndStaticFileServe(); + } + }); } else { stats = s; pth = (pth + "/index.htm").replace(/\/+/g, "/"); @@ -3457,28 +3461,28 @@ if (!cluster.isPrimary) { res.writeHead(200, http.STATUS_CODES[200], customHeaders); // Read custom header and footer content (if available) - var customDirListingHeader = fs.existsSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")) - ? fs.readFileSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")).toString() - : (fs.existsSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) - ? fs.readFileSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")).toString() - : ""; - var customDirListingFooter = fs.existsSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")) - ? fs.readFileSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")).toString() - : (fs.existsSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) - ? fs.readFileSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")).toString() - : ""; + var customDirListingHeader = fs.existsSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")) ? + fs.readFileSync(("." + decodeURIComponent(href) + "/.dirhead").replace(/\/+/g, "/")).toString() : + (fs.existsSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) ? + fs.readFileSync(("." + decodeURIComponent(href) + "/HEAD.html").replace(/\/+/g, "/")).toString() : + ""; + var customDirListingFooter = fs.existsSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")) ? + fs.readFileSync(("." + decodeURIComponent(href) + "/.dirfoot").replace(/\/+/g, "/")).toString() : + (fs.existsSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")) && (os.platform != "win32" || href != "/")) ? + fs.readFileSync(("." + decodeURIComponent(href) + "/FOOT.html").replace(/\/+/g, "/")).toString() : + ""; // Check if custom header has HTML tag var headerHasHTMLTag = customDirListingHeader.replace(/|$)/g, "").match(/])*(?:>|$)/i); // Generate HTML head and footer based on configuration and custom content - var htmlHead = (!configJSON.enableDirectoryListingWithDefaultHead || head == "" - ? (!headerHasHTMLTag - ? "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "" - : customDirListingHeader.replace(//i, "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "")) - : head.replace(//i, "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "")) + - (!headerHasHTMLTag ? customDirListingHeader : "") + - "

Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(//g, ">") + "

" + (checkPathLevel(decodeURIComponent(origHref)) < 1 ? "" : ""); + var htmlHead = (!configJSON.enableDirectoryListingWithDefaultHead || head == "" ? + (!headerHasHTMLTag ? + "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "" : + customDirListingHeader.replace(//i, "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "")) : + head.replace(//i, "Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "")) + + (!headerHasHTMLTag ? customDirListingHeader : "") + + "

Directory: " + decodeURIComponent(origHref).replace(/&/g, "&").replace(//g, ">") + "

Filename Size Date
\"[RET]\"Return
" + (checkPathLevel(decodeURIComponent(origHref)) < 1 ? "" : ""); var htmlFoot = "
Filename Size Date
\"[RET]\"Return

" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (req.headers.host == undefined ? "" : " on " + String(req.headers.host).replace(/&/g, "&").replace(//g, ">")) + "

" + customDirListingFooter + (!configJSON.enableDirectoryListingWithDefaultHead || foot == "" ? "" : foot); @@ -3550,7 +3554,7 @@ if (!cluster.isPrimary) { getStatsForAllFiles(list, "." + decodeURIComponent(href), function (filelist) { // Function to check file extension function checkEXT(filename, ext) { - return filename.match(new RegExp("\\." + ext.replace(/^\./,"").replace(/([.+*?^$()\[\]{}|\\])/,"\\$1") + "$","i")); + return filename.match(new RegExp("\\." + ext.replace(/^\./, "").replace(/([.+*?^$()\[\]{}|\\])/, "\\$1") + "$", "i")); } var directoryListingRows = []; @@ -3562,45 +3566,45 @@ if (!cluster.isPrimary) { if (estats) { directoryListingRows.push( "\"[BAD]\"" + - ename.replace(/&/g, "&").replace(//g, ">") + - "-" + - estats.mtime.toDateString() + - "\r\n" + (href + "/" + encodeURI(ename)).replace(/\/+/g, "/") + + "\">" + + ename.replace(/&/g, "&").replace(//g, ">") + + "-" + + estats.mtime.toDateString() + + "\r\n" ); } else { directoryListingRows.push( "\"[BAD]\"" + - ename.replace(/&/g, "&").replace(//g, ">") + - "--\r\n" + (href + "/" + encodeURI(ename)).replace(/\/+/g, "/") + + "\">" + + ename.replace(/&/g, "&").replace(//g, ">") + + "--\r\n" ); } } else { var entry = "\"[alt]\"" + - ename.replace(/&/g, "&").replace(//g, ">") + - "" + - (estats.isDirectory() ? "-" : sizify(estats.size.toString())) + - "" + - estats.mtime.toDateString() + - "\r\n"; + (origHref + "/" + encodeURIComponent(ename)).replace(/\/+/g, "/") + + (estats.isDirectory() ? "/" : "") + + "\">" + + ename.replace(/&/g, "&").replace(//g, ">") + + "" + + (estats.isDirectory() ? "-" : sizify(estats.size.toString())) + + "" + + estats.mtime.toDateString() + + "\r\n"; // Determine the file type and set the appropriate image and alt text if (estats.isDirectory()) { entry = entry.replace("[img]", "/.dirimages/directory.png").replace("[alt]", "[DIR]"); } else if (!estats.isFile()) { entry = "\"[alt]\"" + - ename.replace(/&/g, "&").replace(//g, ">") + - "-" + - estats.mtime.toDateString() + - "\r\n"; + (origHref + "/" + encodeURIComponent(ename)).replace(/\/+/g, "/") + + "\">" + + ename.replace(/&/g, "&").replace(//g, ">") + + "-" + + estats.mtime.toDateString() + + "\r\n"; // Determine the special file types (block device, character device, etc.) if (estats.isBlockDevice()) { @@ -3746,7 +3750,7 @@ if (!cluster.isPrimary) { if (ifMatchETag && ifMatchETag !== "*" && ifMatchETag !== fileETag) { var headers = getCustomHeaders(); headers.ETag = clientETag; - callServerError(412,undefined,undefined,headers); + callServerError(412, undefined, undefined, headers); return; } } @@ -3766,7 +3770,7 @@ if (!cluster.isPrimary) { var isCompressable = true; try { isCompressable = canCompress(href, dontCompress); - } catch(err) { + } catch (err) { callServerError(500, undefined, generateErrorStack(err)); return; } @@ -3812,7 +3816,7 @@ if (!cluster.isPrimary) { rhd["Content-Range"] = "bytes " + begin + "-" + end + "/" + filelen; rhd["Content-Length"] = end - begin + 1; if (!(mime.contentType(ext) == false) && ext != "") rhd["Content-Type"] = mime.contentType(ext); - if(fileETag) rhd["ETag"] = fileETag; + if (fileETag) rhd["ETag"] = fileETag; if (req.method != "HEAD") { var readStream = fs.createReadStream(readFrom, { @@ -3875,7 +3879,7 @@ if (!cluster.isPrimary) { if (ext != "html") hdhds["Accept-Ranges"] = "bytes"; delete hdhds["Content-Type"]; if (!(mime.contentType(ext) == false) && ext != "") hdhds["Content-Type"] = mime.contentType(ext); - if(fileETag) hdhds["ETag"] = fileETag; + if (fileETag) hdhds["ETag"] = fileETag; if (req.method != "HEAD") { var readStream = fs.createReadStream(readFrom); @@ -3989,7 +3993,7 @@ if (!cluster.isPrimary) { } // Check for invalid X-Forwarded-For header - if(!isForwardedValid) { + if (!isForwardedValid) { serverconsole.errmessage("X-Forwarded-For header is invalid."); callServerError(400); return; @@ -4012,7 +4016,7 @@ if (!cluster.isPrimary) { sanitizedURL.slashes = null; sanitizedURL = url.format(sanitizedURL); serverconsole.resmessage("URL sanitized: " + req.url + " => " + sanitizedURL); - if(rewriteDirtyURLs) { + if (rewriteDirtyURLs) { req.url = sanitizedURL; uobject = parseURL(req.url); search = uobject.search; @@ -4031,9 +4035,9 @@ if (!cluster.isPrimary) { redirect(sanitizedURL, false); return; } - } else if(req.url != preparedReqUrl && !isProxy) { + } else if (req.url != preparedReqUrl && !isProxy) { serverconsole.resmessage("URL sanitized: " + req.url + " => " + preparedReqUrl); - if(rewriteDirtyURLs) { + if (rewriteDirtyURLs) { req.url = preparedReqUrl; } else { redirect(preparedReqUrl, false); @@ -4042,7 +4046,7 @@ if (!cluster.isPrimary) { } // Handle redirects to HTTPS - if(secure && !fromMain && !disableNonEncryptedServer && !disableToHTTPSRedirect) { + if (secure && !fromMain && !disableNonEncryptedServer && !disableToHTTPSRedirect) { var hostx = req.headers.host; if (hostx === undefined) { serverconsole.errmessage("Host header is missing."); @@ -4134,7 +4138,7 @@ if (!cluster.isPrimary) { // Handle URL rewriting function rewriteURL(address, map) { var rewrittenAddress = address; - map.every(function(mapEntry) { + map.every(function (mapEntry) { if (matchHostname(mapEntry.host) && createRegex(mapEntry.definingRegex).test(address)) { mapEntry.replacements.forEach(function (replacement) { rewrittenAddress = rewrittenAddress.replace(createRegex(replacement.regex), replacement.replacement); @@ -4208,7 +4212,7 @@ if (!cluster.isPrimary) { // Set response headers if (!isProxy) { var hkh = getCustomHeaders(); - Object.keys(hkh).forEach(function(hkS) { + Object.keys(hkh).forEach(function (hkS) { try { res.setHeader(hkS, hkh[hkS]); } catch (err) { @@ -4246,7 +4250,7 @@ if (!cluster.isPrimary) { // Scan for non-standard codes if (!isProxy && nonStandardCodes != undefined) { for (var i = 0; i < nonStandardCodes.length; i++) { - if(matchHostname(nonStandardCodes[i].host)) { + if (matchHostname(nonStandardCodes[i].host)) { var isMatch = false; if (nonStandardCodes[i].regex) { // Regex match @@ -4312,13 +4316,13 @@ if (!cluster.isPrimary) { // Trailing slash redirection function redirectTrailingSlashes(callback) { - if (!disableTrailingSlashRedirects && href[href.length-1] != "/") { + if (!disableTrailingSlashRedirects && href[href.length - 1] != "/") { fs.stat("." + decodeURIComponent(href), function (err, stats) { - if(err || !stats.isDirectory()) { + if (err || !stats.isDirectory()) { try { callback(); - } catch(ex) { - callServerError(500, undefined, ex); + } catch (ex) { + callServerError(500, undefined, ex); } } else { var destinationURL = uobject; @@ -4338,64 +4342,74 @@ if (!cluster.isPrimary) { callback(); } } - + // Handle HTTP authentication if (authIndex > -1) { var authcode = nonStandardCodes[authIndex]; // Function to check if passwords match function checkIfPasswordMatches(list, password, callback, _i) { - if(!_i) _i = 0; + if (!_i) _i = 0; var cb = function (hash) { var matches = (hash == list[_i].pass); - if(matches) { + if (matches) { callback(true); - } else if(_i >= list.length-1) { + } else if (_i >= list.length - 1) { callback(false); } else { - checkIfPasswordMatches(list, password, callback, _i+1); + checkIfPasswordMatches(list, password, callback, _i + 1); } }; var hashedPassword = sha256(password + list[_i].salt); - if(list[_i].scrypt) { - if(!crypto.scrypt) { + if (list[_i].scrypt) { + if (!crypto.scrypt) { callServerError(500, undefined, new Error("SVR.JS doesn't support scrypt-hashed passwords on Node.JS versions without scrypt hash support.")); return; } else { var cacheEntry = scryptCache.find(function (entry) { return (entry.password == hashedPassword && entry.salt == list[_i].salt); }); - if(cacheEntry) { + if (cacheEntry) { cb(cacheEntry.hash); } else { crypto.scrypt(password, list[_i].salt, 64, function (err, derivedKey) { - if(err) { + if (err) { callServerError(500, undefined, err); } else { var key = derivedKey.toString("hex"); - scryptCache.push({hash: key, password: hashedPassword, salt: list[_i].salt, addDate: new Date()}); + scryptCache.push({ + hash: key, + password: hashedPassword, + salt: list[_i].salt, + addDate: new Date() + }); cb(key); } }); } } - } else if(list[_i].pbkdf2) { - if(crypto.__disabled__ !== undefined) { + } else if (list[_i].pbkdf2) { + if (crypto.__disabled__ !== undefined) { callServerError(500, undefined, new Error("SVR.JS doesn't support PBKDF2-hashed passwords on Node.JS versions without crypto support.")); return; } else { var cacheEntry = pbkdf2Cache.find(function (entry) { return (entry.password == hashedPassword && entry.salt == list[_i].salt); }); - if(cacheEntry) { + if (cacheEntry) { cb(cacheEntry.hash); } else { crypto.pbkdf2(password, list[_i].salt, 36250, 64, "sha512", function (err, derivedKey) { - if(err) { + if (err) { callServerError(500, undefined, err); } else { var key = derivedKey.toString("hex"); - pbkdf2Cache.push({hash: key, password: hashedPassword, salt: list[_i].salt, addDate: new Date()}); + pbkdf2Cache.push({ + hash: key, + password: hashedPassword, + salt: list[_i].salt, + addDate: new Date() + }); cb(key); } }); @@ -4432,13 +4446,17 @@ if (!cluster.isPrimary) { var username = decodedCredentialsMatch[1]; var password = decodedCredentialsMatch[2]; var usernameMatch = []; - if(!authcode.userList || authcode.userList.indexOf(username) > -1) { + if (!authcode.userList || authcode.userList.indexOf(username) > -1) { usernameMatch = users.filter(function (entry) { return entry.name == username; }); } - if(usernameMatch.length == 0) { - usernameMatch.push({name: username, pass: "FAKEPASS", salt: "FAKESALT"}); // Fake credentials + if (usernameMatch.length == 0) { + usernameMatch.push({ + name: username, + pass: "FAKEPASS", + salt: "FAKESALT" + }); // Fake credentials } checkIfPasswordMatches(usernameMatch, password, function (authorized) { try { @@ -4473,12 +4491,12 @@ if (!cluster.isPrimary) { modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData)); }); } - } catch(err) { + } catch (err) { callServerError(500, undefined, generateErrorStack(err)); return; } }); - } catch(err) { + } catch (err) { callServerError(500, undefined, generateErrorStack(err)); return; } @@ -4574,7 +4592,7 @@ if (!cluster.isPrimary) { } else { try { process.send("\x12ERRLIST" + attmts + err.code); - } catch(ex) { + } catch (ex) { // Probably main process exited } } @@ -4584,12 +4602,12 @@ if (!cluster.isPrimary) { } else { try { if (cluster.isPrimary !== undefined) process.send("\x12ERRCRASH" + err.code); - } catch(ex) { + } catch (ex) { // Probably main process exited } setTimeout(function () { var errno = errors[err.code]; - if(errno) { + if (errno) { process.exit(errno); } else { process.exit(1); @@ -4659,9 +4677,9 @@ function msgListener(msg) { } else if (msg == "\x12LISTEN" || msg.substr(0, 4) == "\x12AUTH") { // Do nothing! } else if (msg == "\x12KILLOK") { - if(typeof isWorkerHungUpBuff != "undefined") isWorkerHungUpBuff = false; + if (typeof isWorkerHungUpBuff != "undefined") isWorkerHungUpBuff = false; } else if (msg == "\x12PINGOK") { - if(typeof isWorkerHungUpBuff2 != "undefined") isWorkerHungUpBuff2 = false; + if (typeof isWorkerHungUpBuff2 != "undefined") isWorkerHungUpBuff2 = false; } else if (msg == "\x12KILLTERMMSG") { serverconsole.locmessage("Terminating unused worker process..."); } else if (msg == "\x12SAVEGOOD") { @@ -4716,7 +4734,7 @@ function msgListener(msg) { } if (msg.length >= 9 && msg.indexOf("\x12ERRCRASH") == 0) { var errno = errors[msg.substr(9)]; - if(errno) { + if (errno) { process.exit(errno); } else { process.exit(1); @@ -4731,7 +4749,7 @@ function msgListener(msg) { var messageTransmitted = false; function listeningMessage() { - if(typeof closedMaster !== "undefined") closedMaster = false; + if (typeof closedMaster !== "undefined") closedMaster = false; if (!cluster.isPrimary && cluster.isPrimary !== undefined) { process.send("\x12LISTEN"); return; @@ -4742,8 +4760,8 @@ function listeningMessage() { var sListenToAny = (!sListenAddress || sListenAddress.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) || sListenAddress.match(/^(?:0{0,4}:)+0{0,4}$/)); var accHost = host; var sAccHost = host; - if(!listenToAny) accHost = listenAddress; - if(!sListenToAny) sAccHost = sListenAddress; + if (!listenToAny) accHost = listenAddress; + if (!sListenToAny) sAccHost = sListenAddress; if (messageTransmitted) return; messageTransmitted = true; serverconsole.locmessage("Started server at: "); @@ -4789,8 +4807,12 @@ function start(init) { if (configJSON.enableHTTP2 && !secure) serverconsole.locwarnmessage("HTTP/2 without HTTPS may not work in web browsers. Web browsers only support HTTP/2 with HTTPS!"); if (process.isBun) { serverconsole.locwarnmessage("Bun support is experimental. Some features of SVR.JS, SVR.JS mods and SVR.JS server-side JavaScript may not work as expected."); - if(users.some(function (entry) {return entry.pbkdf2;})) serverconsole.locwarnmessage("PBKDF2 password hashing function in Bun blocks the event loop, which may result in denial of service."); - if(users.some(function (entry) {return entry.scrypt;})) serverconsole.locwarnmessage("scrypt password hashing function in Bun blocks the event loop, which may result in denial of service."); + if (users.some(function (entry) { + return entry.pbkdf2; + })) serverconsole.locwarnmessage("PBKDF2 password hashing function in Bun blocks the event loop, which may result in denial of service."); + if (users.some(function (entry) { + return entry.scrypt; + })) serverconsole.locwarnmessage("scrypt password hashing function in Bun blocks the event loop, which may result in denial of service."); } if (cluster.isPrimary === undefined) serverconsole.locwarnmessage("You're running SVR.JS on single thread. Reliability may suffer, as the server is stopped after crash."); if (crypto.__disabled__ !== undefined) serverconsole.locwarnmessage("Your Node.JS version doesn't have crypto support! The 'crypto' module is essential for providing cryptographic functionality in Node.JS. Without crypto support, certain security features may be unavailable, and some functionality may not work as expected. It's recommended to use a Node.JS version that includes crypto support to ensure the security and proper functioning of your server."); @@ -4808,18 +4830,18 @@ function start(init) { console.log(); // Display mod and server-side JavaScript errors - if(process.isPrimary || process.isPrimary === undefined) { - modLoadingErrors.forEach(function(modLoadingError) { + if (process.isPrimary || process.isPrimary === undefined) { + modLoadingErrors.forEach(function (modLoadingError) { serverconsole.locwarnmessage("There was a problem while loading a \"" + modLoadingError.modName + "\" mod."); serverconsole.locwarnmessage("Stack:"); serverconsole.locwarnmessage(generateErrorStack(modLoadingError.error)); }); - if(SSJSError) { + if (SSJSError) { serverconsole.locwarnmessage("There was a problem while loading server-side JavaScript."); serverconsole.locwarnmessage("Stack:"); serverconsole.locwarnmessage(generateErrorStack(SSJSError)); } - if(SSJSError || modLoadingErrors.length > 0) console.log(); + if (SSJSError || modLoadingErrors.length > 0) console.log(); } // Print info @@ -4841,8 +4863,8 @@ function start(init) { if (brdIPs.indexOf(listenAddress) > -1) throw new Error("SVR.JS can't listen on broadcast address."); if (netIPs.indexOf(listenAddress) > -1) throw new Error("SVR.JS can't listen on subnet address."); } - if(certificateError) throw new Error("There was a problem with SSL certificate/private key: " + certificateError.message); - if(wwwrootError) throw new Error("There was a problem with your web root: " + wwwrootError.message); + if (certificateError) throw new Error("There was a problem with SSL certificate/private key: " + certificateError.message); + if (wwwrootError) throw new Error("There was a problem with your web root: " + wwwrootError.message); } // Information about starting the server @@ -4853,23 +4875,23 @@ function start(init) { if (!cluster.isPrimary) { try { - if(typeof (secure ? sport : port) == "number" && (secure ? sListenAddress : listenAddress)) { + if (typeof (secure ? sport : port) == "number" && (secure ? sListenAddress : listenAddress)) { server.listen(secure ? sport : port, secure ? sListenAddress : listenAddress); } else { server.listen(secure ? sport : port); } - } catch(err) { - if(err.code != "ERR_SERVER_ALREADY_LISTEN") throw err; + } catch (err) { + if (err.code != "ERR_SERVER_ALREADY_LISTEN") throw err; } if (secure && !disableNonEncryptedServer) { try { - if(typeof port == "number" && listenAddress) { + if (typeof port == "number" && listenAddress) { server2.listen(port, listenAddress); } else { server2.listen(port); } - } catch(err) { - if(err.code != "ERR_SERVER_ALREADY_LISTEN") throw err; + } catch (err) { + if (err.code != "ERR_SERVER_ALREADY_LISTEN") throw err; } } } @@ -4894,13 +4916,13 @@ function start(init) { }, open: function () { try { - if(typeof (secure ? sport : port) == "number" && (secure ? sListenAddress : listenAddress)) { + if (typeof (secure ? sport : port) == "number" && (secure ? sListenAddress : listenAddress)) { server.listen(secure ? sport : port, secure ? sListenAddress : listenAddress); } else { server.listen(secure ? sport : port); } if (secure && !disableNonEncryptedServer) { - if(typeof port == "number" && listenAddress) { + if (typeof port == "number" && listenAddress) { server2.listen(port, listenAddress); } else { server2.listen(port); @@ -4934,13 +4956,13 @@ function start(init) { stop: function (retcode) { reallyExiting = true; clearInterval(pbkdf2CacheIntervalId); - if((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { + if ((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { try { server.close(function () { - if(server2.listening) { + if (server2.listening) { try { server2.close(function () { - if(!process.removeFakeIPC) { + if (!process.removeFakeIPC) { if (typeof retcode == "number") { process.exit(retcode); } else { @@ -4948,8 +4970,8 @@ function start(init) { } } }); - } catch(err) { - if(!process.removeFakeIPC) { + } catch (err) { + if (!process.removeFakeIPC) { if (typeof retcode == "number") { process.exit(retcode); } else { @@ -4958,7 +4980,7 @@ function start(init) { } } } else { - if(!process.removeFakeIPC) { + if (!process.removeFakeIPC) { if (typeof retcode == "number") { process.exit(retcode); } else { @@ -4967,14 +4989,14 @@ function start(init) { } } }); - } catch(err) { + } catch (err) { if (typeof retcode == "number") { process.exit(retcode); } else { process.exit(0); } } - if(process.removeFakeIPC) process.removeFakeIPC(); + if (process.removeFakeIPC) process.removeFakeIPC(); } else { if (typeof retcode == "number") { process.exit(retcode); @@ -5039,9 +5061,10 @@ function start(init) { setInterval(function () { var allClusters = Object.keys(cluster.workers); var goodWorkers = []; + function checkWorker(callback, _id) { - if(typeof _id === "undefined") _id = 0; - if(_id >= allClusters.length) { + if (typeof _id === "undefined") _id = 0; + if (_id >= allClusters.length) { callback(); return; } @@ -5052,14 +5075,14 @@ function start(init) { cluster.workers[allClusters[_id]].send("\x14PINGPING"); setTimeout(function () { if (isWorkerHungUpBuff2) { - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } else { goodWorkers.push(allClusters[_id]); - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } }, 250); } else { - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } } catch (err) { if (cluster.workers[allClusters[_id]]) { @@ -5067,25 +5090,25 @@ function start(init) { cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allClusters[_id]].on("message", listenConnListener); } - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } } checkWorker(function () { - var wN = Math.floor(Math.random() * goodWorkers.length); //Send a configuration saving message to a random worker. - try { - if (cluster.workers[goodWorkers[wN]]) { - isWorkerHungUpBuff2 = true; - cluster.workers[goodWorkers[wN]].on("message", msgListener); - cluster.workers[goodWorkers[wN]].send("\x14SAVECONF"); - } - } catch (err) { - if (cluster.workers[goodWorkers[wN]]) { - cluster.workers[goodWorkers[wN]].removeAllListeners("message"); - cluster.workers[goodWorkers[wN]].on("message", bruteForceListenerWrapper(cluster.workers[goodWorkers[wN]])); - cluster.workers[goodWorkers[wN]].on("message", listenConnListener); - } - serverconsole.locwarnmessage("There was a problem while saving configuration file. Reason: " + err.message); + var wN = Math.floor(Math.random() * goodWorkers.length); //Send a configuration saving message to a random worker. + try { + if (cluster.workers[goodWorkers[wN]]) { + isWorkerHungUpBuff2 = true; + cluster.workers[goodWorkers[wN]].on("message", msgListener); + cluster.workers[goodWorkers[wN]].send("\x14SAVECONF"); } + } catch (err) { + if (cluster.workers[goodWorkers[wN]]) { + cluster.workers[goodWorkers[wN]].removeAllListeners("message"); + cluster.workers[goodWorkers[wN]].on("message", bruteForceListenerWrapper(cluster.workers[goodWorkers[wN]])); + cluster.workers[goodWorkers[wN]].on("message", listenConnListener); + } + serverconsole.locwarnmessage("There was a problem while saving configuration file. Reason: " + err.message); + } }); }, 300000); } @@ -5115,19 +5138,19 @@ function start(init) { } process.send("\x12END"); } else if (line == "\x14KILLPING") { - if(!reallyExiting) { + if (!reallyExiting) { process.send("\x12KILLOK"); process.send("\x12END"); } // Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway... } else if (line == "\x14PINGPING") { - if(!reallyExiting) { + if (!reallyExiting) { process.send("\x12PINGOK"); process.send("\x12END"); } // Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway... } else if (line == "\x14KILLREQ") { - if(reqcounter - reqcounterKillReq < 2) { + if (reqcounter - reqcounterKillReq < 2) { process.send("\x12KILLTERMMSG"); process.nextTick(commands.stop); } else { @@ -5319,7 +5342,7 @@ function start(init) { } if (msg.length >= 9 && msg.indexOf("\x12ERRCRASH") == 0) { var errno = errors[msg.substr(9)]; - if(errno) { + if (errno) { process.exit(errno); } else { process.exit(1); @@ -5359,8 +5382,8 @@ function start(init) { }); } else if ((configJSON.enableHTTP2 == undefined ? false : configJSON.enableHTTP2) && !secure) { // It doesn't support through Unix sockets or Windows named pipes - var address = ((typeof port == "number" && listenAddress) ? listenAddress : "localhost").replace(/\/@/g,""); - if(address.indexOf(":") > -1) { + var address = ((typeof port == "number" && listenAddress) ? listenAddress : "localhost").replace(/\/@/g, ""); + if (address.indexOf(":") > -1) { address = "[" + address + "]"; } var connection = http2.connect("http://" + address + ":" + port.toString()); @@ -5419,18 +5442,19 @@ function start(init) { }, 4550); // Termination of unused good workers - if(!disableUnusedWorkerTermination && cluster.isPrimary !== undefined) { + if (!disableUnusedWorkerTermination && cluster.isPrimary !== undefined) { setTimeout(function () { setInterval(function () { if (!closedMaster && !exiting) { var allClusters = Object.keys(cluster.workers); var minClusters = 0; minClusters = Math.ceil(cpus * 0.625); - if(minClusters < 2) minClusters = 2; + if (minClusters < 2) minClusters = 2; var goodWorkers = []; + function checkWorker(callback, _id) { - if(typeof _id === "undefined") _id = 0; - if(_id >= allClusters.length) { + if (typeof _id === "undefined") _id = 0; + if (_id >= allClusters.length) { callback(); return; } @@ -5441,14 +5465,14 @@ function start(init) { cluster.workers[allClusters[_id]].send("\x14KILLPING"); setTimeout(function () { if (isWorkerHungUpBuff) { - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } else { goodWorkers.push(allClusters[_id]); - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } }, 250); } else { - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } } catch (err) { if (cluster.workers[allClusters[_id]]) { @@ -5456,7 +5480,7 @@ function start(init) { cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allClusters[_id]].on("message", listenConnListener); } - checkWorker(callback, _id+1); + checkWorker(callback, _id + 1); } } checkWorker(function () { @@ -5567,7 +5591,7 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) { }); process.on("exit", function (code) { try { - if(!configJSONRErr && !configJSONPErr) { + if (!configJSONRErr && !configJSONPErr) { saveConfig(); } } catch (err) { @@ -5648,7 +5672,7 @@ try { serverconsole.locerrmessage("There was a problem starting SVR.JS!!!"); serverconsole.locerrmessage("Stack:"); serverconsole.locerrmessage(generateErrorStack(err)); - setTimeout(function() { + setTimeout(function () { process.exit(err.errno ? err.errno : 1); }, 10); }