2014-01-23 03:12:43 +00:00
|
|
|
var path = require("path");
|
2014-02-25 00:32:54 +00:00
|
|
|
var fs = require("fs");
|
2014-01-23 03:12:43 +00:00
|
|
|
var net = require("net");
|
|
|
|
var express = require("express");
|
|
|
|
var webroot = path.join(__dirname, "..", "www");
|
|
|
|
var sendJade = require("./jade").sendJade;
|
|
|
|
var Server = require("../server");
|
|
|
|
var $util = require("../utilities");
|
|
|
|
var Logger = require("../logger");
|
|
|
|
var Config = require("../config");
|
2014-01-28 06:05:14 +00:00
|
|
|
var db = require("../database");
|
2014-08-19 05:07:24 +00:00
|
|
|
var bodyParser = require("body-parser");
|
|
|
|
var cookieParser = require("cookie-parser");
|
2014-08-19 05:21:32 +00:00
|
|
|
var static = require("serve-static");
|
|
|
|
var morgan = require("morgan");
|
2013-12-12 20:48:23 +00:00
|
|
|
|
2014-09-12 00:00:27 +00:00
|
|
|
const LOG_FORMAT = ':real-address - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"';
|
|
|
|
morgan.token('real-address', function (req) { return req._ip; });
|
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
/**
|
|
|
|
* Extracts an IP address from a request. Uses X-Forwarded-For if the IP is localhost
|
|
|
|
*/
|
|
|
|
function ipForRequest(req) {
|
|
|
|
var ip = req.ip;
|
2014-01-23 03:12:43 +00:00
|
|
|
if (ip === "127.0.0.1" || ip === "::1") {
|
|
|
|
var xforward = req.header("x-forwarded-for");
|
2014-04-17 05:03:32 +00:00
|
|
|
if (typeof xforward !== "string") {
|
|
|
|
xforward = [];
|
2013-12-12 20:48:23 +00:00
|
|
|
} else {
|
2014-04-17 05:03:32 +00:00
|
|
|
xforward = xforward.split(",");
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
2014-04-17 05:03:32 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < xforward.length; i++) {
|
|
|
|
if (net.isIP(xforward[i])) {
|
|
|
|
return xforward[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip;
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
/**
|
|
|
|
* Redirects a request to HTTPS if the server supports it
|
|
|
|
*/
|
|
|
|
function redirectHttps(req, res) {
|
|
|
|
if (!req.secure && Config.get("https.enabled")) {
|
2014-02-26 20:28:24 +00:00
|
|
|
var ssldomain = Config.get("https.full-address");
|
2014-01-23 03:12:43 +00:00
|
|
|
res.redirect(ssldomain + req.path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects a request to HTTP if the server supports it
|
|
|
|
*/
|
|
|
|
function redirectHttp(req, res) {
|
|
|
|
if (req.secure) {
|
2014-02-26 20:28:24 +00:00
|
|
|
var domain = Config.get("http.full-address");
|
2014-01-23 03:12:43 +00:00
|
|
|
res.redirect(domain + req.path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a GET request for /r/:channel - serves channel.html
|
|
|
|
*/
|
|
|
|
function handleChannel(req, res) {
|
|
|
|
if (!$util.isValidChannelName(req.params.channel)) {
|
|
|
|
res.status(404);
|
2014-01-23 03:12:43 +00:00
|
|
|
res.send("Invalid channel name '" + req.params.channel + "'");
|
2013-12-12 20:48:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-23 03:12:43 +00:00
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
var loginName = false;
|
|
|
|
if (req.cookies.auth) {
|
2014-01-23 03:12:43 +00:00
|
|
|
loginName = req.cookies.auth.split(":")[0];
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
2014-01-23 03:12:43 +00:00
|
|
|
|
2014-01-30 03:50:45 +00:00
|
|
|
var sio;
|
2014-05-21 02:30:14 +00:00
|
|
|
if (net.isIPv6(ipForRequest(req))) {
|
|
|
|
sio = Config.get("io.ipv6-default");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sio) {
|
|
|
|
sio = Config.get("io.ipv4-default");
|
2014-01-30 03:50:45 +00:00
|
|
|
}
|
2014-05-21 02:30:14 +00:00
|
|
|
|
2014-01-30 03:50:45 +00:00
|
|
|
sio += "/socket.io/socket.io.js";
|
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
sendJade(res, "channel", {
|
2013-12-12 20:48:23 +00:00
|
|
|
channelName: req.params.channel,
|
|
|
|
loggedIn: loginName !== false,
|
|
|
|
loginName: loginName,
|
2014-01-30 03:50:45 +00:00
|
|
|
sioSource: sio
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a request for the index page
|
|
|
|
*/
|
|
|
|
function handleIndex(req, res) {
|
|
|
|
var loginName = false;
|
|
|
|
if (req.cookies.auth) {
|
2014-01-23 03:12:43 +00:00
|
|
|
loginName = req.cookies.auth.split(":")[0];
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
2014-01-20 23:52:36 +00:00
|
|
|
|
2014-01-23 21:53:53 +00:00
|
|
|
var channels = Server.getServer().packChannelList(true);
|
|
|
|
channels.sort(function (a, b) {
|
|
|
|
if (a.usercount === b.usercount) {
|
2014-02-28 15:04:41 +00:00
|
|
|
return a.uniqueName > b.uniqueName ? -1 : 1;
|
2014-01-23 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 15:04:41 +00:00
|
|
|
return b.usercount - a.usercount;
|
2014-01-23 21:53:53 +00:00
|
|
|
});
|
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
sendJade(res, "index", {
|
2013-12-12 20:48:23 +00:00
|
|
|
loggedIn: loginName !== false,
|
2014-01-20 23:52:36 +00:00
|
|
|
loginName: loginName,
|
2014-02-26 17:04:22 +00:00
|
|
|
channels: channels
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
/**
|
|
|
|
* Handles a request for the socket.io information
|
|
|
|
*/
|
|
|
|
function handleSocketConfig(req, res) {
|
|
|
|
res.type("application/javascript");
|
|
|
|
|
2014-05-21 02:30:14 +00:00
|
|
|
var sioconfig = Config.get("sioconfig");
|
|
|
|
var iourl;
|
|
|
|
var ip = ipForRequest(req);
|
2014-06-13 03:29:12 +00:00
|
|
|
var ipv6 = false;
|
2014-05-21 02:30:14 +00:00
|
|
|
|
|
|
|
if (net.isIPv6(ip)) {
|
|
|
|
iourl = Config.get("io.ipv6-default");
|
2014-06-13 03:29:12 +00:00
|
|
|
ipv6 = true;
|
2014-05-21 02:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iourl) {
|
|
|
|
iourl = Config.get("io.ipv4-default");
|
|
|
|
}
|
|
|
|
sioconfig += "var IO_URL='" + iourl + "';";
|
2014-06-13 03:29:12 +00:00
|
|
|
sioconfig += "var IO_V6=" + ipv6 + ";";
|
2014-05-21 02:30:14 +00:00
|
|
|
res.send(sioconfig);
|
2014-02-13 06:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleUserAgreement(req, res) {
|
2014-02-15 06:12:11 +00:00
|
|
|
var loginName = false;
|
|
|
|
if (req.cookies.auth) {
|
|
|
|
loginName = req.cookies.auth.split(":")[0];
|
|
|
|
}
|
|
|
|
|
2014-02-13 06:12:17 +00:00
|
|
|
sendJade(res, "tos", {
|
2014-02-15 06:12:11 +00:00
|
|
|
loggedIn: loginName !== false,
|
|
|
|
loginName: loginName,
|
2014-02-13 06:12:17 +00:00
|
|
|
domain: Config.get("http.domain")
|
|
|
|
});
|
|
|
|
}
|
2014-01-23 03:12:43 +00:00
|
|
|
|
2014-02-14 00:15:22 +00:00
|
|
|
function handleContactPage(req, res) {
|
2014-02-15 06:12:11 +00:00
|
|
|
var loginName = false;
|
|
|
|
if (req.cookies.auth) {
|
|
|
|
loginName = req.cookies.auth.split(":")[0];
|
|
|
|
}
|
|
|
|
|
2014-02-14 00:15:22 +00:00
|
|
|
// Make a copy to prevent messing with the original
|
|
|
|
var contacts = Config.get("contacts").map(function (c) {
|
|
|
|
return {
|
|
|
|
name: c.name,
|
|
|
|
email: c.email,
|
|
|
|
title: c.title
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// Rudimentary hiding of email addresses to prevent spambots
|
|
|
|
contacts.forEach(function (c) {
|
|
|
|
c.emkey = $util.randomSalt(16)
|
|
|
|
var email = new Array(c.email.length);
|
|
|
|
for (var i = 0; i < c.email.length; i++) {
|
|
|
|
email[i] = String.fromCharCode(
|
|
|
|
c.email.charCodeAt(i) ^ c.emkey.charCodeAt(i % c.emkey.length)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
c.email = escape(email.join(""));
|
|
|
|
c.emkey = escape(c.emkey);
|
|
|
|
});
|
|
|
|
|
|
|
|
sendJade(res, "contact", {
|
2014-02-15 06:12:11 +00:00
|
|
|
loggedIn: loginName !== false,
|
|
|
|
loginName: loginName,
|
2014-02-14 00:15:22 +00:00
|
|
|
contacts: contacts
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Initializes webserver callbacks
|
|
|
|
*/
|
|
|
|
init: function (app) {
|
2014-09-11 23:56:33 +00:00
|
|
|
app.use(function (req, res, next) {
|
2014-09-12 00:00:27 +00:00
|
|
|
req._ip = ipForRequest(req);
|
2014-09-11 23:56:33 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-08-19 05:07:24 +00:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(cookieParser());
|
2014-09-12 00:00:27 +00:00
|
|
|
app.use(morgan(LOG_FORMAT, {
|
2014-11-17 03:06:10 +00:00
|
|
|
stream: require("fs").createWriteStream(path.join(__dirname, "..", "..",
|
2014-08-19 05:21:32 +00:00
|
|
|
"http.log"), {
|
|
|
|
flags: "a",
|
|
|
|
encoding: "utf-8"
|
|
|
|
})
|
|
|
|
}));
|
2014-02-04 17:32:52 +00:00
|
|
|
|
2014-11-17 03:06:10 +00:00
|
|
|
if (Config.get("http.gzip")) {
|
|
|
|
app.use(require("compression")({ threshold: Config.get("http.gzip-threshold") }));
|
|
|
|
Logger.syslog.log("Enabled gzip compression");
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:32:52 +00:00
|
|
|
if (Config.get("http.minify")) {
|
2014-02-25 00:32:54 +00:00
|
|
|
var cache = path.join(__dirname, "..", "..", "www", "cache")
|
|
|
|
if (!fs.existsSync(cache)) {
|
|
|
|
fs.mkdirSync(cache);
|
|
|
|
}
|
|
|
|
app.use(require("express-minify")({
|
|
|
|
cache: cache
|
|
|
|
}));
|
2014-02-04 17:32:52 +00:00
|
|
|
Logger.syslog.log("Enabled express-minify for CSS and JS");
|
|
|
|
}
|
2014-08-19 05:21:32 +00:00
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
app.get("/r/:channel", handleChannel);
|
|
|
|
app.get("/", handleIndex);
|
|
|
|
app.get("/sioconfig", handleSocketConfig);
|
2014-02-13 06:12:17 +00:00
|
|
|
app.get("/useragreement", handleUserAgreement);
|
2014-02-14 00:15:22 +00:00
|
|
|
app.get("/contact", handleContactPage);
|
2014-01-29 02:04:25 +00:00
|
|
|
require("./auth").init(app);
|
|
|
|
require("./account").init(app);
|
|
|
|
require("./acp").init(app);
|
2014-11-17 03:06:10 +00:00
|
|
|
app.use(static(path.join(__dirname, "..", "..", "www"), {
|
|
|
|
maxAge: Config.get("http.max-age") || Config.get("http.cache-ttl")
|
|
|
|
}));
|
2014-02-28 15:04:41 +00:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
if (err) {
|
2014-08-08 02:25:34 +00:00
|
|
|
if (err.message && err.message.match(/failed to decode param/i)) {
|
|
|
|
return res.status(400).send("Malformed path: " + req.path);
|
2014-09-01 19:36:24 +00:00
|
|
|
} else if (err.message && err.message.match(/requested range not/i)) {
|
|
|
|
return res.status(416).end();
|
2014-10-12 16:36:54 +00:00
|
|
|
} else if (err.message && err.message.match(/bad request/i)) {
|
|
|
|
return res.status(400).end("Bad Request");
|
2014-08-08 02:25:34 +00:00
|
|
|
}
|
2014-02-28 15:04:41 +00:00
|
|
|
Logger.errlog.log(err.stack);
|
2014-09-01 19:36:24 +00:00
|
|
|
res.status(500).end();
|
2014-02-28 15:04:41 +00:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
2013-12-12 20:48:23 +00:00
|
|
|
},
|
|
|
|
|
2014-01-23 03:12:43 +00:00
|
|
|
ipForRequest: ipForRequest,
|
|
|
|
|
|
|
|
redirectHttps: redirectHttps,
|
|
|
|
|
2014-02-25 00:25:49 +00:00
|
|
|
redirectHttp: redirectHttp,
|
2013-12-12 20:48:23 +00:00
|
|
|
};
|