From d75c8dd230d48226e428ee5d699d11410d747889 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Fri, 28 Feb 2014 09:04:41 -0600 Subject: [PATCH] Hopefully prevent more webserver crashes --- lib/web/webserver.js | 60 +++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/web/webserver.js b/lib/web/webserver.js index 818d74f8..0ef95f0f 100644 --- a/lib/web/webserver.js +++ b/lib/web/webserver.js @@ -138,10 +138,10 @@ function handleIndex(req, res) { var channels = Server.getServer().packChannelList(true); channels.sort(function (a, b) { if (a.usercount === b.usercount) { - return a.uniqueName > b.uniqueName ? 1 : -1; + return a.uniqueName > b.uniqueName ? -1 : 1; } - return a.usercount - b.usercount; + return b.usercount - a.usercount; }); sendJade(res, "index", { @@ -224,29 +224,34 @@ function handleContactPage(req, res) { function static(dir) { dir = path.join(__dirname, dir); return function (req, res) { - if (isSuspicious(req)) { - logRequest(req, 403); - res.status(403); - if (typeof req.header("user-agent") === "string" && - req.header("user-agent").toLowerCase() === "zmeu") { - res.send("This server disallows requests from ZmEu."); - } else { - res.send("The request " + req.method.toUpperCase() + " " + - req.path + " looks pretty fishy to me. Double check that " + - "you typed it correctly."); + try { + if (isSuspicious(req)) { + logRequest(req, 403); + res.status(403); + if (typeof req.header("user-agent") === "string" && + req.header("user-agent").toLowerCase() === "zmeu") { + res.send("This server disallows requests from ZmEu."); + } else { + res.send("The request " + req.method.toUpperCase() + " " + + req.path + " looks pretty fishy to me. Double check that " + + "you typed it correctly."); + } + return; } - return; - } - res.sendfile(req.path.replace(/^\//, ""), { - maxAge: Config.get("http.cache-ttl") * 1000, - root: dir - }, function (err) { - logRequest(req); - if (err) { - res.send(err.status); - } - }); + res.sendfile(req.path.replace(/^\//, ""), { + maxAge: Config.get("http.cache-ttl") * 1000, + root: dir + }, function (err) { + logRequest(req); + if (err) { + res.send(err.status); + } + }); + } catch (e) { + Logger.errlog.log(e); + Logger.errlog.log(e.trace); + } }; } @@ -284,6 +289,15 @@ module.exports = { require("./account").init(app); require("./acp").init(app); app.use(static(path.join("..", "..", "www"))); + app.use(function (err, req, res, next) { + if (err) { + Logger.errlog.log(err); + Logger.errlog.log(err.stack); + res.send(500); + } else { + next(); + } + }); }, logRequest: logRequest,