Convert server definitions to be more flexible

This commit is contained in:
calzoneman 2014-04-11 00:14:52 -05:00
parent 04dbb3444b
commit fb0533bd94
4 changed files with 67 additions and 50 deletions

View File

@ -22,9 +22,19 @@ var defaults = {
user: "cytube3", user: "cytube3",
password: "" password: ""
}, },
listen: [
{
ip: "",
port: 8080,
http: true,
},
{
ip: "",
port: 1337,
io: true
}
],
http: { http: {
host: "",
port: 8080,
domain: "http://localhost", domain: "http://localhost",
"root-domain": "localhost", "root-domain": "localhost",
"alt-domains": ["127.0.0.1"], "alt-domains": ["127.0.0.1"],
@ -32,8 +42,6 @@ var defaults = {
"cache-ttl": 0 "cache-ttl": 0
}, },
https: { https: {
enabled: false,
port: 8443,
domain: "https://localhost", domain: "https://localhost",
keyfile: "localhost.key", keyfile: "localhost.key",
passphrase: "", passphrase: "",
@ -42,7 +50,6 @@ var defaults = {
}, },
io: { io: {
domain: "http://localhost", domain: "http://localhost",
port: 1337,
"ip-connection-limit": 10 "ip-connection-limit": 10
}, },
mail: { mail: {

View File

@ -663,9 +663,8 @@ module.exports.loadAnnouncement = function () {
var sv = Server.getServer(); var sv = Server.getServer();
sv.announcement = announcement; sv.announcement = announcement;
sv.io.sockets.emit("announcement", announcement); for (var id in sv.ioServers) {
if (sv.ioSecure) { sv.ioServers[id].sockets.emit("announcement", announcement);
sv.ioSecure.sockets.emit("announcement", announcement);
} }
}); });
}; };

View File

@ -119,27 +119,29 @@ function handleConnection(sock) {
module.exports = { module.exports = {
init: function (srv) { init: function (srv) {
var ioport = Config.get("io.port"); Config.get("listen").forEach(function (bind) {
var webport = Config.get("http.port"); if (!bind.io) {
var app; return;
if (ioport !== webport) { }
app = require("express")().listen(ioport, Config.get("http.host")); var id = bind.ip + ":" + bind.port;
srv.ioWeb = app; if (id in srv.ioServers) {
} else { Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id);
app = srv.express; return;
} }
srv.io = sio.listen(app); var io = null;
srv.io.set("log level", 1); if (id in srv.servers) {
srv.io.set("authorization", handleAuth); io = srv.ioServers[id] = sio.listen(srv.servers[id]);
srv.io.on("connection", handleConnection); } else {
io = srv.ioServers[id] = sio.listen(bind.port, bind.ip);
}
if (Config.get("https.enabled")) { if (io) {
srv.ioSecure = sio.listen(srv.https); io.set("log level", 1);
srv.ioSecure.set("log level", 1); io.set("authorization", handleAuth);
srv.ioSecure.set("authorization", handleAuth); io.on("connection", handleConnection);
srv.ioSecure.on("connection", handleConnection); }
} });
} }
}; };

View File

@ -60,6 +60,8 @@ var Server = function () {
self.httplog = null; self.httplog = null;
self.infogetter = null; self.infogetter = null;
self.torblocker = null; self.torblocker = null;
self.servers = {};
self.ioServers = {};
// database init ------------------------------------------------------ // database init ------------------------------------------------------
var Database = require("./database"); var Database = require("./database");
@ -73,30 +75,37 @@ var Server = function () {
require("./web/webserver").init(self.express); require("./web/webserver").init(self.express);
// http/https/sio server init ----------------------------------------- // http/https/sio server init -----------------------------------------
if (Config.get("https.enabled")) { var key = fs.readFileSync(path.resolve(__dirname, "..",
var key = fs.readFileSync(path.resolve(__dirname, "..", Config.get("https.keyfile")));
Config.get("https.keyfile"))); var cert = fs.readFileSync(path.resolve(__dirname, "..",
var cert = fs.readFileSync(path.resolve(__dirname, "..", Config.get("https.certfile")));
Config.get("https.certfile"))); var ca = undefined;
var ca = undefined; if (Config.get("https.cafile")) {
if (Config.get("https.cafile")) { ca = fs.readFileSync(path.resolve(__dirname, "..",
ca = fs.readFileSync(path.resolve(__dirname, "..", Config.get("https.cafile")));
Config.get("https.cafile")));
}
var opts = {
key: key,
cert: cert,
passphrase: Config.get("https.passphrase"),
ca: ca
};
self.https = https.createServer(opts, self.express)
.listen(Config.get("https.port"));
} }
self.http = self.express.listen(Config.get("http.port"), var opts = {
Config.get("http.host") || undefined); key: key,
cert: cert,
passphrase: Config.get("https.passphrase"),
ca: ca
};
Config.get("listen").forEach(function (bind) {
var id = bind.ip + ":" + bind.port;
if (id in self.servers) {
Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id);
return;
}
if (bind.https) {
self.servers[id] = https.createServer(opts, self.express)
.listen(bind.port, bind.ip);
} else if (bind.http) {
self.servers[id] = self.express.listen(bind.port, bind.ip);
}
});
require("./io/ioserver").init(self); require("./io/ioserver").init(self);