2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-08-14 17:25:22 +00:00
|
|
|
var _ = require("lodash");
|
2016-12-11 08:29:09 +00:00
|
|
|
var colors = require("colors/safe");
|
2014-08-13 23:43:11 +00:00
|
|
|
var fs = require("fs");
|
2014-08-14 01:51:54 +00:00
|
|
|
var Client = require("./client");
|
2014-09-13 12:23:17 +00:00
|
|
|
var Helper = require("./helper");
|
2014-08-13 23:43:11 +00:00
|
|
|
|
|
|
|
module.exports = ClientManager;
|
|
|
|
|
|
|
|
function ClientManager() {
|
2014-08-14 16:35:37 +00:00
|
|
|
this.clients = [];
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientManager.prototype.init = function(identHandler, sockets) {
|
|
|
|
this.sockets = sockets;
|
|
|
|
this.identHandler = identHandler;
|
|
|
|
|
|
|
|
if (!Helper.config.public) {
|
|
|
|
if ("autoload" in Helper.config) {
|
|
|
|
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
|
|
|
|
}
|
2016-04-26 20:41:08 +00:00
|
|
|
|
2016-12-17 09:51:33 +00:00
|
|
|
this.autoloadUsers();
|
2016-04-26 20:41:08 +00:00
|
|
|
}
|
2016-12-17 09:51:33 +00:00
|
|
|
};
|
2014-08-13 23:43:11 +00:00
|
|
|
|
2016-07-30 01:20:38 +00:00
|
|
|
ClientManager.prototype.findClient = function(name, token) {
|
2014-08-14 16:35:37 +00:00
|
|
|
for (var i in this.clients) {
|
|
|
|
var client = this.clients[i];
|
2016-08-10 06:26:47 +00:00
|
|
|
if (client.name === name || (token && token === client.config.token)) {
|
2014-08-14 16:35:37 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-12-07 05:50:11 +00:00
|
|
|
ClientManager.prototype.autoloadUsers = function() {
|
2017-04-08 12:34:31 +00:00
|
|
|
this.getUsers().forEach((name) => this.loadUser(name));
|
2016-12-07 05:50:11 +00:00
|
|
|
|
|
|
|
fs.watch(Helper.USERS_PATH, _.debounce(() => {
|
2017-04-08 12:34:31 +00:00
|
|
|
const loaded = this.clients.map((c) => c.name);
|
2016-12-07 05:50:11 +00:00
|
|
|
const updatedUsers = this.getUsers();
|
|
|
|
|
|
|
|
// New users created since last time users were loaded
|
2017-04-08 12:34:31 +00:00
|
|
|
_.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name));
|
2016-12-07 05:50:11 +00:00
|
|
|
|
|
|
|
// Existing users removed since last time users were loaded
|
2017-04-08 12:34:31 +00:00
|
|
|
_.difference(loaded, updatedUsers).forEach((name) => {
|
2016-12-07 05:50:11 +00:00
|
|
|
const client = _.find(this.clients, {name: name});
|
|
|
|
if (client) {
|
|
|
|
client.quit();
|
|
|
|
this.clients = _.without(this.clients, client);
|
2016-12-11 08:29:09 +00:00
|
|
|
log.info(`User ${colors.bold(name)} disconnected and removed`);
|
2016-12-07 05:50:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000, {maxWait: 10000}));
|
2014-08-14 01:51:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.loadUser = function(name) {
|
2016-10-09 08:54:44 +00:00
|
|
|
let json;
|
2014-08-14 01:51:54 +00:00
|
|
|
try {
|
2016-10-09 08:54:44 +00:00
|
|
|
json = this.readUserConfig(name);
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2016-04-16 11:32:38 +00:00
|
|
|
log.error("Failed to read user config", e);
|
2014-08-14 01:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-24 22:23:54 +00:00
|
|
|
if (!this.findClient(name)) {
|
|
|
|
this.clients.push(new Client(
|
2016-02-17 00:14:43 +00:00
|
|
|
this,
|
2014-09-24 22:23:54 +00:00
|
|
|
name,
|
|
|
|
json
|
|
|
|
));
|
|
|
|
}
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.getUsers = function() {
|
|
|
|
var users = [];
|
|
|
|
try {
|
2016-05-08 06:21:31 +00:00
|
|
|
var files = fs.readdirSync(Helper.USERS_PATH);
|
2017-04-08 12:34:31 +00:00
|
|
|
files.forEach((file) => {
|
2014-10-14 22:24:15 +00:00
|
|
|
if (file.indexOf(".json") !== -1) {
|
|
|
|
users.push(file.replace(".json", ""));
|
|
|
|
}
|
2014-10-14 20:05:16 +00:00
|
|
|
});
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2016-04-16 11:32:38 +00:00
|
|
|
log.error("Failed to get users", e);
|
2014-08-13 23:43:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return users;
|
|
|
|
};
|
|
|
|
|
2017-01-29 19:33:57 +00:00
|
|
|
ClientManager.prototype.addUser = function(name, password, enableLog) {
|
2014-08-13 23:43:11 +00:00
|
|
|
var users = this.getUsers();
|
|
|
|
if (users.indexOf(name) !== -1) {
|
2014-08-14 17:25:22 +00:00
|
|
|
return false;
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
|
|
|
try {
|
2016-05-08 06:21:31 +00:00
|
|
|
if (require("path").basename(name) !== name) {
|
2016-04-02 21:19:57 +00:00
|
|
|
throw new Error(name + " is an invalid username.");
|
|
|
|
}
|
|
|
|
|
2014-08-14 01:51:54 +00:00
|
|
|
var user = {
|
|
|
|
user: name,
|
|
|
|
password: password || "",
|
2017-01-29 19:33:57 +00:00
|
|
|
log: enableLog,
|
2017-04-28 15:58:14 +00:00
|
|
|
awayMessage: "",
|
2014-08-14 01:51:54 +00:00
|
|
|
networks: []
|
|
|
|
};
|
|
|
|
fs.writeFileSync(
|
2016-05-08 06:21:31 +00:00
|
|
|
Helper.getUserConfigPath(name),
|
2016-06-19 17:16:50 +00:00
|
|
|
JSON.stringify(user, null, "\t")
|
2014-08-14 01:51:54 +00:00
|
|
|
);
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2016-04-02 21:19:57 +00:00
|
|
|
log.error("Failed to add user " + name, e);
|
2014-08-13 23:43:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2014-08-14 17:25:22 +00:00
|
|
|
return true;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
2016-11-19 20:54:16 +00:00
|
|
|
ClientManager.prototype.updateUser = function(name, opts, callback) {
|
|
|
|
const users = this.getUsers();
|
2016-02-17 00:14:43 +00:00
|
|
|
if (users.indexOf(name) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof opts === "undefined") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
const user = this.readUserConfig(name);
|
2016-11-19 20:54:16 +00:00
|
|
|
const currentUser = JSON.stringify(user, null, "\t");
|
|
|
|
_.assign(user, opts);
|
|
|
|
const newUser = JSON.stringify(user, null, "\t");
|
|
|
|
|
|
|
|
// Do not touch the disk if object has not changed
|
|
|
|
if (currentUser === newUser) {
|
|
|
|
return callback ? callback() : true;
|
2016-02-17 00:14:43 +00:00
|
|
|
}
|
2016-11-19 20:54:16 +00:00
|
|
|
|
|
|
|
fs.writeFile(Helper.getUserConfigPath(name), newUser, (err) => {
|
|
|
|
if (err) {
|
|
|
|
log.error("Failed to update user", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
2016-02-17 00:14:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.readUserConfig = function(name) {
|
|
|
|
var users = this.getUsers();
|
|
|
|
if (users.indexOf(name) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-08 06:21:31 +00:00
|
|
|
var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
|
2016-06-12 06:43:35 +00:00
|
|
|
return JSON.parse(data);
|
2016-02-17 00:14:43 +00:00
|
|
|
};
|
|
|
|
|
2014-08-13 23:43:11 +00:00
|
|
|
ClientManager.prototype.removeUser = function(name) {
|
|
|
|
var users = this.getUsers();
|
|
|
|
if (users.indexOf(name) === -1) {
|
2014-08-14 17:25:22 +00:00
|
|
|
return false;
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
|
|
|
try {
|
2016-05-08 06:21:31 +00:00
|
|
|
fs.unlinkSync(Helper.getUserConfigPath(name));
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2014-08-13 23:43:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2014-08-14 17:25:22 +00:00
|
|
|
return true;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|