2013-07-19 20:27:21 +00:00
|
|
|
var path = require("path");
|
2013-07-28 23:47:55 +00:00
|
|
|
var fs = require("fs");
|
2013-04-17 18:42:29 +00:00
|
|
|
var express = require("express");
|
2013-07-15 22:57:33 +00:00
|
|
|
var Config = require("./config");
|
|
|
|
var Logger = require("./logger");
|
|
|
|
var Channel = require("./channel");
|
|
|
|
var User = require("./user");
|
2013-04-22 19:42:39 +00:00
|
|
|
|
2013-08-01 02:27:08 +00:00
|
|
|
const VERSION = "2.2.0";
|
2013-04-25 16:00:06 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
function getIP(req) {
|
2013-07-06 17:00:02 +00:00
|
|
|
var raw = req.connection.remoteAddress;
|
2013-06-04 03:56:06 +00:00
|
|
|
var forward = req.header("x-forwarded-for");
|
2013-07-28 15:49:29 +00:00
|
|
|
if(Server.cfg["trust-x-forward"] && forward) {
|
2013-07-15 22:57:33 +00:00
|
|
|
var ip = forward.split(",")[0];
|
|
|
|
Logger.syslog.log("REVPROXY " + raw + " => " + ip);
|
2013-07-06 17:00:02 +00:00
|
|
|
return ip;
|
2013-06-04 03:56:06 +00:00
|
|
|
}
|
2013-07-06 17:00:02 +00:00
|
|
|
return raw;
|
2013-06-04 03:56:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:57:56 +00:00
|
|
|
function getSocketIP(socket) {
|
2013-07-06 17:00:02 +00:00
|
|
|
var raw = socket.handshake.address.address;
|
2013-07-28 15:49:29 +00:00
|
|
|
if(Server.cfg["trust-x-forward"]) {
|
2013-07-06 17:00:02 +00:00
|
|
|
if(typeof socket.handshake.headers["x-forwarded-for"] == "string") {
|
2013-07-15 22:57:33 +00:00
|
|
|
var ip = socket.handshake.headers["x-forwarded-for"]
|
|
|
|
.split(",")[0];
|
|
|
|
Logger.syslog.log("REVPROXY " + raw + " => " + ip);
|
2013-07-06 17:00:02 +00:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
return raw;
|
2013-06-26 14:57:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
var Server = {
|
|
|
|
channels: [],
|
|
|
|
channelLoaded: function (name) {
|
|
|
|
for(var i in this.channels) {
|
2013-07-27 14:40:49 +00:00
|
|
|
if(this.channels[i].canonical_name == name.toLowerCase())
|
2013-07-15 22:57:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
getChannel: function (name) {
|
|
|
|
for(var i in this.channels) {
|
2013-07-27 14:40:49 +00:00
|
|
|
if(this.channels[i].canonical_name == name.toLowerCase())
|
2013-07-15 22:57:33 +00:00
|
|
|
return this.channels[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
var c = new Channel(name, this);
|
|
|
|
this.channels.push(c);
|
|
|
|
return c;
|
|
|
|
},
|
|
|
|
unloadChannel: function(chan) {
|
|
|
|
if(chan.registered)
|
|
|
|
chan.saveDump();
|
|
|
|
chan.playlist.die();
|
2013-07-29 23:59:52 +00:00
|
|
|
chan.logger.close();
|
2013-07-15 22:57:33 +00:00
|
|
|
for(var i in this.channels) {
|
2013-07-27 14:40:49 +00:00
|
|
|
if(this.channels[i].canonical_name == chan.canonical_name) {
|
2013-07-15 22:57:33 +00:00
|
|
|
this.channels.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-27 14:40:49 +00:00
|
|
|
chan.name = "";
|
2013-07-15 22:57:33 +00:00
|
|
|
},
|
2013-07-16 18:57:34 +00:00
|
|
|
stats: null,
|
2013-07-15 22:57:33 +00:00
|
|
|
app: null,
|
|
|
|
io: null,
|
|
|
|
httpserv: null,
|
|
|
|
ioserv: null,
|
|
|
|
db: null,
|
|
|
|
ips: {},
|
2013-07-16 03:01:12 +00:00
|
|
|
acp: null,
|
2013-07-30 13:43:58 +00:00
|
|
|
httpaccess: null,
|
|
|
|
logHTTP: function (req, status) {
|
|
|
|
if(status === undefined)
|
|
|
|
status = 200;
|
|
|
|
var ip = req.connection.remoteAddress;
|
2013-07-30 16:26:08 +00:00
|
|
|
var ip2 = false;
|
|
|
|
if(this.cfg["trust-x-forward"])
|
|
|
|
ip2 = req.header("x-forwarded-for") || req.header("cf-connecting-ip");
|
|
|
|
var ipstr = !ip2 ? ip : ip + " (X-Forwarded-For " + ip2 + ")";
|
|
|
|
var url = req.url;
|
|
|
|
// Remove query
|
|
|
|
if(url.indexOf("?") != -1)
|
|
|
|
url = url.substring(0, url.lastIndexOf("?"));
|
|
|
|
this.httpaccess.log([ipstr, req.method, url, status, req.headers["user-agent"]].join(" "));
|
2013-07-30 13:43:58 +00:00
|
|
|
},
|
2013-07-15 22:57:33 +00:00
|
|
|
init: function () {
|
2013-07-30 13:43:58 +00:00
|
|
|
this.httpaccess = new Logger.Logger("httpaccess.log");
|
2013-07-15 22:57:33 +00:00
|
|
|
this.app = express();
|
|
|
|
// channel path
|
|
|
|
this.app.get("/r/:channel(*)", function (req, res, next) {
|
|
|
|
var c = req.params.channel;
|
2013-07-30 13:43:58 +00:00
|
|
|
if(!c.match(/^[\w-_]+$/)) {
|
2013-07-15 22:57:33 +00:00
|
|
|
res.redirect("/" + c);
|
2013-07-30 13:43:58 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.logHTTP(req);
|
2013-07-15 22:57:33 +00:00
|
|
|
res.sendfile(__dirname + "/www/channel.html");
|
2013-07-30 13:43:58 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-03-28 23:51:08 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// api path
|
|
|
|
this.api = require("./api")(this);
|
|
|
|
this.app.get("/api/:apireq(*)", function (req, res, next) {
|
2013-07-30 13:43:58 +00:00
|
|
|
this.logHTTP(req);
|
2013-07-15 22:57:33 +00:00
|
|
|
this.api.handle(req.url.substring(5), req, res);
|
2013-07-16 15:46:09 +00:00
|
|
|
}.bind(this));
|
2013-03-28 23:51:08 +00:00
|
|
|
|
2013-07-20 01:59:39 +00:00
|
|
|
this.app.get("/", function (req, res, next) {
|
2013-07-30 13:43:58 +00:00
|
|
|
this.logHTTP(req);
|
2013-07-20 01:59:39 +00:00
|
|
|
res.sendfile(__dirname + "/www/index.html");
|
2013-07-30 13:43:58 +00:00
|
|
|
}.bind(this));
|
2013-07-20 01:59:39 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// default path
|
|
|
|
this.app.get("/:thing(*)", function (req, res, next) {
|
2013-07-27 15:06:49 +00:00
|
|
|
var opts = {
|
|
|
|
root: __dirname + "/www",
|
2013-07-28 21:10:35 +00:00
|
|
|
maxAge: this.cfg["asset-cache-ttl"]
|
2013-07-27 15:06:49 +00:00
|
|
|
}
|
|
|
|
res.sendfile(req.params.thing, opts, function (err) {
|
|
|
|
if(err) {
|
2013-07-30 13:43:58 +00:00
|
|
|
this.logHTTP(req, err.status);
|
2013-07-27 15:06:49 +00:00
|
|
|
// Damn path traversal attacks
|
|
|
|
if(req.params.thing.indexOf("%2e") != -1) {
|
|
|
|
res.send("Don't try that again, I'll ban you");
|
|
|
|
Logger.syslog.log("WARNING: Attempted path "+
|
|
|
|
"traversal from /" + getIP(req));
|
|
|
|
Logger.syslog.log("URL: " + req.url);
|
|
|
|
}
|
|
|
|
// Something actually went wrong
|
|
|
|
else {
|
2013-07-28 01:14:19 +00:00
|
|
|
// Status codes over 500 are server errors
|
|
|
|
if(err.status >= 500)
|
|
|
|
Logger.errlog.log(err);
|
|
|
|
res.send(err.status);
|
2013-07-27 15:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-30 13:43:58 +00:00
|
|
|
else {
|
|
|
|
this.logHTTP(req);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-07-28 21:10:35 +00:00
|
|
|
}.bind(this));
|
2013-03-28 23:51:08 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// fallback
|
|
|
|
this.app.use(function (err, req, res, next) {
|
2013-07-30 13:43:58 +00:00
|
|
|
this.logHTTP(req, err.status);
|
2013-07-15 22:57:33 +00:00
|
|
|
if(err.status == 404) {
|
|
|
|
res.send(404);
|
|
|
|
} else {
|
|
|
|
next(err);
|
|
|
|
}
|
2013-07-30 13:43:58 +00:00
|
|
|
}.bind(this));
|
2013-05-03 18:15:05 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// bind servers
|
2013-07-28 15:49:29 +00:00
|
|
|
this.httpserv = this.app.listen(Server.cfg["web-port"],
|
|
|
|
Server.cfg["express-host"]);
|
|
|
|
this.ioserv = express().listen(Server.cfg["io-port"],
|
|
|
|
Server.cfg["express-host"]);
|
2013-07-15 22:57:33 +00:00
|
|
|
|
|
|
|
// init socket.io
|
|
|
|
this.io = require("socket.io").listen(this.ioserv);
|
|
|
|
this.io.set("log level", 1);
|
|
|
|
this.io.sockets.on("connection", function (socket) {
|
|
|
|
var ip = getSocketIP(socket);
|
|
|
|
socket._ip = ip;
|
|
|
|
if(this.db.checkGlobalBan(ip)) {
|
|
|
|
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
|
|
|
|
socket.emit("kick", {
|
|
|
|
reason: "You're globally banned."
|
|
|
|
});
|
|
|
|
socket.disconnect(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on("disconnect", function () {
|
|
|
|
this.ips[ip]--;
|
2013-07-16 15:46:09 +00:00
|
|
|
}.bind(this));
|
2013-07-15 22:57:33 +00:00
|
|
|
|
|
|
|
if(!(ip in this.ips))
|
|
|
|
this.ips[ip] = 0;
|
|
|
|
this.ips[ip]++;
|
|
|
|
|
2013-07-28 15:49:29 +00:00
|
|
|
if(this.ips[ip] > Server.cfg["ip-connection-limit"]) {
|
2013-07-15 22:57:33 +00:00
|
|
|
socket.emit("kick", {
|
|
|
|
reason: "Too many connections from your IP address"
|
|
|
|
});
|
|
|
|
socket.disconnect(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally a valid user
|
|
|
|
Logger.syslog.log("Accepted socket from /" + socket._ip);
|
|
|
|
new User(socket, this);
|
2013-07-16 03:01:12 +00:00
|
|
|
}.bind(this));
|
2013-07-14 02:19:47 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
// init database
|
|
|
|
this.db = require("./database");
|
2013-07-28 15:49:29 +00:00
|
|
|
this.db.setup(Server.cfg);
|
2013-07-15 22:57:33 +00:00
|
|
|
this.db.init();
|
2013-07-16 03:01:12 +00:00
|
|
|
|
|
|
|
// init ACP
|
|
|
|
this.acp = require("./acp")(this);
|
2013-07-16 18:57:34 +00:00
|
|
|
|
|
|
|
// init stats
|
|
|
|
this.stats = require("./stats")(this);
|
2013-07-15 22:57:33 +00:00
|
|
|
},
|
|
|
|
shutdown: function () {
|
|
|
|
Logger.syslog.log("Unloading channels");
|
|
|
|
for(var i in this.channels) {
|
2013-07-27 14:13:30 +00:00
|
|
|
if(this.channels[i].registered) {
|
|
|
|
Logger.syslog.log("Saving /r/" + this.channels[i].name);
|
2013-07-15 22:57:33 +00:00
|
|
|
this.channels[i].saveDump();
|
2013-07-27 14:13:30 +00:00
|
|
|
}
|
2013-07-15 22:57:33 +00:00
|
|
|
}
|
|
|
|
Logger.syslog.log("Goodbye");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
};
|
2013-07-14 02:19:47 +00:00
|
|
|
|
2013-07-15 22:57:33 +00:00
|
|
|
Logger.syslog.log("Starting CyTube v" + VERSION);
|
2013-07-28 23:47:13 +00:00
|
|
|
|
|
|
|
fs.exists("chanlogs", function (exists) {
|
|
|
|
exists || fs.mkdir("chanlogs");
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.exists("chandump", function (exists) {
|
|
|
|
exists || fs.mkdir("chandump");
|
|
|
|
});
|
|
|
|
|
2013-07-28 15:49:29 +00:00
|
|
|
Config.load(Server, "cfg.json", function () {
|
|
|
|
Server.init();
|
|
|
|
if(!Server.cfg["debug"]) {
|
|
|
|
process.on("uncaughtException", function (err) {
|
|
|
|
Logger.errlog.log("[SEVERE] Uncaught Exception: " + err);
|
|
|
|
Logger.errlog.log(err.stack);
|
|
|
|
});
|
2013-07-14 02:19:47 +00:00
|
|
|
|
2013-07-28 15:49:29 +00:00
|
|
|
process.on("SIGINT", function () {
|
|
|
|
Server.shutdown();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|