From c6f9b1611e88fe0dc6eb9345f59bf8bfb07a0a04 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Sun, 10 Feb 2019 10:22:16 -0800 Subject: [PATCH] Add some sanity checks for common first-startup issues --- package.json | 2 +- src/config.js | 29 +++++++++++++++++++++++++++++ src/io/ioserver.js | 12 ++++++++++++ src/server.js | 25 +++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 324fc715..ad7391a8 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.63.1", + "version": "3.63.2", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/config.js b/src/config.js index 005effb2..b13111ee 100644 --- a/src/config.js +++ b/src/config.js @@ -245,6 +245,28 @@ function loadEmailConfig() { function preprocessConfig(cfg) { // Root domain should start with a . for cookies var root = cfg.http["root-domain"]; + if (/127\.0\.0\.1|localhost/.test(root)) { + LOGGER.warn( + "Detected 127.0.0.1 or localhost in root-domain '%s'. This server " + + "will not work from other computers! Set root-domain to the domain " + + "the website will be accessed from (e.g. example.com)", + root + ); + } + if (/^http/.test(root)) { + LOGGER.warn( + "root-domain '%s' should not contain http:// or https://, removing it", + root + ); + root = root.replace(/^https?:\/\//, ""); + } + if (/:\d+$/.test(root)) { + LOGGER.warn( + "root-domain '%s' should not contain a trailing port, removing it", + root + ); + root = root.replace(/:\d+$/, ""); + } root = root.replace(/^\.*/, ""); cfg.http["root-domain"] = root; if (root.indexOf(".") !== -1 && !net.isIP(root)) { @@ -328,6 +350,13 @@ function preprocessConfig(cfg) { cfg.io["ipv4-default"] = cfg.io["ipv4-ssl"] || cfg.io["ipv4-nossl"]; cfg.io["ipv6-default"] = cfg.io["ipv6-ssl"] || cfg.io["ipv6-nossl"]; + if (/127\.0\.0\.1|localhost/.test(cfg.io["ipv4-default"])) { + LOGGER.warn( + "socket.io is bound to localhost, this server will be inaccessible " + + "from other computers!" + ); + } + // Generate RegExps for reserved names var reserved = cfg["reserved-names"]; for (var key in reserved) { diff --git a/src/io/ioserver.js b/src/io/ioserver.js index 47c92133..3c0a50f7 100644 --- a/src/io/ioserver.js +++ b/src/io/ioserver.js @@ -488,6 +488,18 @@ module.exports = { } else { const server = http.createServer().listen(bind.port, bind.ip); servers.push(server); + server.on("error", error => { + if (error.code === "EADDRINUSE") { + LOGGER.fatal( + "Could not bind %s: address already in use. Check " + + "whether another application has already bound this " + + "port, or whether another instance of this server " + + "is running.", + id + ); + process.exit(1); + } + }); } uniqueListenAddresses.add(id); diff --git a/src/server.js b/src/server.js index 33d5227e..4f999153 100644 --- a/src/server.js +++ b/src/server.js @@ -24,6 +24,7 @@ module.exports = { fs.exists(gdvttpath, function (exists) { exists || fs.mkdirSync(gdvttpath); }); + singleton = new Server(); return singleton; }, @@ -161,6 +162,18 @@ var Server = function () { if (bind.https && Config.get("https.enabled")) { self.servers[id] = https.createServer(opts, self.express) .listen(bind.port, bind.ip); + self.servers[id].on("error", error => { + if (error.code === "EADDRINUSE") { + LOGGER.fatal( + "Could not bind %s: address already in use. Check " + + "whether another application has already bound this " + + "port, or whether another instance of this server " + + "is running.", + id + ); + process.exit(1); + } + }); self.servers[id].on("clientError", function (err, socket) { try { socket.destroy(); @@ -170,6 +183,18 @@ var Server = function () { }); } else if (bind.http) { self.servers[id] = self.express.listen(bind.port, bind.ip); + self.servers[id].on("error", error => { + if (error.code === "EADDRINUSE") { + LOGGER.fatal( + "Could not bind %s: address already in use. Check " + + "whether another application has already bound this " + + "port, or whether another instance of this server " + + "is running.", + id + ); + process.exit(1); + } + }); self.servers[id].on("clientError", function (err, socket) { try { socket.destroy();