Hopefully prevent more webserver crashes

This commit is contained in:
calzoneman 2014-02-28 09:04:41 -06:00
parent 5b793710c3
commit d75c8dd230
1 changed files with 37 additions and 23 deletions

View File

@ -138,10 +138,10 @@ function handleIndex(req, res) {
var channels = Server.getServer().packChannelList(true); var channels = Server.getServer().packChannelList(true);
channels.sort(function (a, b) { channels.sort(function (a, b) {
if (a.usercount === b.usercount) { 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", { sendJade(res, "index", {
@ -224,29 +224,34 @@ function handleContactPage(req, res) {
function static(dir) { function static(dir) {
dir = path.join(__dirname, dir); dir = path.join(__dirname, dir);
return function (req, res) { return function (req, res) {
if (isSuspicious(req)) { try {
logRequest(req, 403); if (isSuspicious(req)) {
res.status(403); logRequest(req, 403);
if (typeof req.header("user-agent") === "string" && res.status(403);
req.header("user-agent").toLowerCase() === "zmeu") { if (typeof req.header("user-agent") === "string" &&
res.send("This server disallows requests from ZmEu."); req.header("user-agent").toLowerCase() === "zmeu") {
} else { res.send("This server disallows requests from ZmEu.");
res.send("The request " + req.method.toUpperCase() + " " + } else {
req.path + " looks pretty fishy to me. Double check that " + res.send("The request " + req.method.toUpperCase() + " " +
"you typed it correctly."); req.path + " looks pretty fishy to me. Double check that " +
"you typed it correctly.");
}
return;
} }
return;
}
res.sendfile(req.path.replace(/^\//, ""), { res.sendfile(req.path.replace(/^\//, ""), {
maxAge: Config.get("http.cache-ttl") * 1000, maxAge: Config.get("http.cache-ttl") * 1000,
root: dir root: dir
}, function (err) { }, function (err) {
logRequest(req); logRequest(req);
if (err) { if (err) {
res.send(err.status); 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("./account").init(app);
require("./acp").init(app); require("./acp").init(app);
app.use(static(path.join("..", "..", "www"))); 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, logRequest: logRequest,