sync/server.js

255 lines
7.9 KiB
JavaScript
Raw Normal View History

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-08-08 22:28:24 +00:00
const VERSION = "2.3.2";
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;
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-07-06 17:00:02 +00:00
return raw;
}
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-07-15 22:57:33 +00:00
var Server = {
channels: [],
channelLoaded: function (name) {
for(var i in this.channels) {
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) {
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();
chan.logger.close();
2013-07-15 22:57:33 +00:00
for(var i in this.channels) {
if(this.channels[i].canonical_name == chan.canonical_name) {
2013-07-15 22:57:33 +00:00
this.channels.splice(i, 1);
break;
}
}
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,
2013-08-17 23:44:48 +00:00
actionlog: null,
2013-07-30 13:43:58 +00:00
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 () {
// init database
var Database = require("./database");
this.db = new Database(this.cfg);
this.db.init();
2013-08-17 23:44:48 +00:00
this.actionlog = require("./actionlog")(this);
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();
2013-08-08 03:44:41 +00:00
this.app.use(express.bodyParser());
2013-07-15 22:57:33 +00:00
// 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-07-15 22:57:33 +00:00
// api path
this.api = require("./api")(this);
2013-08-08 03:44:41 +00:00
/*
2013-07-15 22:57:33 +00:00
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-08-08 03:44:41 +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 {
// 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-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;
2013-08-17 23:44:48 +00:00
this.db.isGlobalIPBanned(ip, function (err, bant) {
if(bant) {
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
socket.emit("kick", {
reason: "You're globally banned."
});
socket.disconnect(true);
}
});
2013-07-15 22:57:33 +00:00
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-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-08-03 14:52:24 +00:00
// init media retriever
this.infogetter = require("./get-info")(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-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-28 15:49:29 +00:00
process.on("SIGINT", function () {
Server.shutdown();
});
}
});