From 90842267e81d58434285a630c6339a0c72011200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:03:36 -0400 Subject: [PATCH 1/6] Do not prevent server to run when there are no users in private mode --- src/command-line/start.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/command-line/start.js b/src/command-line/start.js index 4da4b499..e93366ae 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -29,8 +29,6 @@ program if (!mode && !users.length && !Helper.config.ldap.enable) { log.warn("No users found."); log.info(`Create a new user with ${colors.bold("lounge add ")}.`); - - return; } Helper.config.host = options.host || Helper.config.host; From f221121998eab79f58c5039071fbef1fd52eac91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:11:28 -0400 Subject: [PATCH 2/6] Inform when no users during autoload to take into account removing the last user --- src/clientManager.js | 15 +++++++++++++-- src/command-line/list.js | 2 +- src/command-line/start.js | 9 --------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/clientManager.js b/src/clientManager.js index bec7c314..ba56d6c1 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -18,7 +18,7 @@ ClientManager.prototype.init = function(identHandler, sockets) { this.identHandler = identHandler; this.webPush = new WebPush(); - if (!Helper.config.public) { + if (!Helper.config.public && !Helper.config.ldap.enable) { if ("autoload" in Helper.config) { log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`); } @@ -32,12 +32,23 @@ ClientManager.prototype.findClient = function(name) { }; ClientManager.prototype.autoloadUsers = function() { - this.getUsers().forEach((name) => this.loadUser(name)); + const users = this.getUsers(); + const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add ")}.`; + + if (!users.length) { + log.info(noUsersWarning); + } + + users.forEach((name) => this.loadUser(name)); fs.watch(Helper.USERS_PATH, _.debounce(() => { const loaded = this.clients.map((c) => c.name); const updatedUsers = this.getUsers(); + if (!updatedUsers.length) { + log.info(noUsersWarning); + } + // New users created since last time users were loaded _.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name)); diff --git a/src/command-line/list.js b/src/command-line/list.js index 8ca63ea7..4ef58ed6 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -12,7 +12,7 @@ program .action(function() { var users = new ClientManager().getUsers(); if (!users.length) { - log.warn("No users found."); + log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); } else { log.info("Users:"); for (var i = 0; i < users.length; i++) { diff --git a/src/command-line/start.js b/src/command-line/start.js index e93366ae..db33e127 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -1,8 +1,6 @@ "use strict"; -var ClientManager = new require("../clientManager"); var program = require("commander"); -var colors = require("colors/safe"); var server = require("../server"); var Helper = require("../helper"); const Utils = require("./utils"); @@ -17,8 +15,6 @@ program .description("Start the server") .on("--help", Utils.extraHelp) .action(function(options) { - var users = new ClientManager().getUsers(); - var mode = Helper.config.public; if (options.public) { mode = true; @@ -26,11 +22,6 @@ program mode = false; } - if (!mode && !users.length && !Helper.config.ldap.enable) { - log.warn("No users found."); - log.info(`Create a new user with ${colors.bold("lounge add ")}.`); - } - Helper.config.host = options.host || Helper.config.host; Helper.config.port = options.port || Helper.config.port; Helper.config.bind = options.bind || Helper.config.bind; From aa498564466f9b5547d7772493b71f77d658cf4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:15:01 -0400 Subject: [PATCH 3/6] Warn when adding or listing users in public mode --- src/command-line/add.js | 10 ++++++++-- src/command-line/list.js | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/command-line/add.js b/src/command-line/add.js index 184292c3..198e6f4c 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -11,12 +11,18 @@ program .description("Add a new user") .on("--help", Utils.extraHelp) .action(function(name) { - var manager = new ClientManager(); - var users = manager.getUsers(); + if (Helper.config.public) { + log.warn(`Users have no effect in ${colors.bold("public")} mode.`); + } + + const manager = new ClientManager(); + const users = manager.getUsers(); + if (users.indexOf(name) !== -1) { log.error(`User ${colors.bold(name)} already exists.`); return; } + log.prompt({ text: "Enter password:", silent: true diff --git a/src/command-line/list.js b/src/command-line/list.js index 4ef58ed6..df447c94 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -3,6 +3,7 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); var colors = require("colors/safe"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -10,6 +11,10 @@ program .description("List all users") .on("--help", Utils.extraHelp) .action(function() { + if (Helper.config.public) { + log.warn(`Users have no effect in ${colors.bold("public")} mode.`); + } + var users = new ClientManager().getUsers(); if (!users.length) { log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); From 684f1a641dfd2b12c9a9abdcf84826bdeebed4fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:19:50 -0400 Subject: [PATCH 4/6] Make sure server is running before loading users --- src/server.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/server.js b/src/server.js index d8c4626b..6eb6ee7d 100644 --- a/src/server.js +++ b/src/server.js @@ -89,28 +89,32 @@ module.exports = function() { }, () => { const protocol = config.https.enable ? "https" : "http"; var address = server.address(); - log.info(`Available on ${colors.green(protocol + "://" + address.address + ":" + address.port + "/")} \ -in ${config.public ? "public" : "private"} mode`); - }); - var sockets = io(server, { - serveClient: false, - transports: config.transports - }); + log.info( + "Available at " + + colors.green(`${protocol}://${address.address}:${address.port}/`) + + ` in ${colors.bold(config.public ? "public" : "private")} mode` + ); - sockets.on("connect", function(socket) { - if (config.public) { - performAuthentication.call(socket, {}); - } else { - socket.emit("auth", {success: true}); - socket.on("auth", performAuthentication); - } - }); + const sockets = io(server, { + serveClient: false, + transports: config.transports + }); - manager = new ClientManager(); + sockets.on("connect", (socket) => { + if (config.public) { + performAuthentication.call(socket, {}); + } else { + socket.emit("auth", {success: true}); + socket.on("auth", performAuthentication); + } + }); - new Identification((identHandler) => { - manager.init(identHandler, sockets); + manager = new ClientManager(); + + new Identification((identHandler) => { + manager.init(identHandler, sockets); + }); }); }; From cdbefd390544eac6c2edb3844dfd9170a8515165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:23:24 -0400 Subject: [PATCH 5/6] Make The Lounge private mode by default --- defaults/config.js | 4 ++-- test/fixtures/.lounge/config.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/defaults/config.js b/defaults/config.js index b103593d..3c863917 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -8,9 +8,9 @@ module.exports = { // Set to 'false' to enable users. // // @type boolean - // @default true + // @default false // - public: true, + public: false, // // IP address or hostname for the web server to listen on. diff --git a/test/fixtures/.lounge/config.js b/test/fixtures/.lounge/config.js index c04fbb18..cbc7b926 100644 --- a/test/fixtures/.lounge/config.js +++ b/test/fixtures/.lounge/config.js @@ -2,6 +2,7 @@ var config = require("../../../defaults/config.js"); +config.public = true; config.prefetch = true; config.host = config.bind = "127.0.0.1"; config.port = 61337; From ed68ff4a34ce7c4d6d3b931420df65495d420979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:42:59 -0400 Subject: [PATCH 6/6] Make sure CLI does not crash on `undefined.length` when we figured out users could not be read already --- src/clientManager.js | 8 +++++++- src/command-line/add.js | 4 ++++ src/command-line/edit.js | 5 +++++ src/command-line/list.js | 5 +++++ src/command-line/remove.js | 15 ++++++++++----- src/command-line/reset.js | 5 +++++ 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/clientManager.js b/src/clientManager.js index ba56d6c1..c076494f 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -35,6 +35,12 @@ ClientManager.prototype.autoloadUsers = function() { const users = this.getUsers(); const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add ")}.`; + // There was an error, already logged, but we have to crash the server as + // user directory could not be accessed + if (users === undefined) { + process.exit(1); + } + if (!users.length) { log.info(noUsersWarning); } @@ -91,7 +97,7 @@ ClientManager.prototype.getUsers = function() { } }); } catch (e) { - log.error("Failed to get users", e); + log.error(`Failed to get users (${e})`); return; } return users; diff --git a/src/command-line/add.js b/src/command-line/add.js index 198e6f4c..c88833d5 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -18,6 +18,10 @@ program const manager = new ClientManager(); const users = manager.getUsers(); + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) !== -1) { log.error(`User ${colors.bold(name)} already exists.`); return; diff --git a/src/command-line/edit.js b/src/command-line/edit.js index eb6ec671..b2da9450 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -13,6 +13,11 @@ program .on("--help", Utils.extraHelp) .action(function(name) { var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) === -1) { log.error(`User ${colors.bold(name)} does not exist.`); return; diff --git a/src/command-line/list.js b/src/command-line/list.js index df447c94..e10a7375 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -16,6 +16,11 @@ program } var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (!users.length) { log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); } else { diff --git a/src/command-line/remove.js b/src/command-line/remove.js index e0a971bc..e15f13ff 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -10,10 +10,15 @@ program .description("Remove an existing user") .on("--help", Utils.extraHelp) .action(function(name) { - var manager = new ClientManager(); - if (manager.removeUser(name)) { - log.info(`User ${colors.bold(name)} removed.`); - } else { - log.error(`User ${colors.bold(name)} does not exist.`); + const manager = new ClientManager(); + + try { + if (manager.removeUser(name)) { + log.info(`User ${colors.bold(name)} removed.`); + } else { + log.error(`User ${colors.bold(name)} does not exist.`); + } + } catch (e) { + // There was an error, already logged } }); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 9546435e..6721ff4b 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -13,6 +13,11 @@ program .on("--help", Utils.extraHelp) .action(function(name) { var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) === -1) { log.error(`User ${colors.bold(name)} does not exist.`); return;