hardlounge/src/clientManager.js

173 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-08-14 17:25:22 +00:00
var _ = require("lodash");
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-08-26 18:00:12 +00:00
var mkdirp = require("mkdirp");
var Helper = require("./helper");
2016-04-26 20:41:08 +00:00
var oidentd = require("./oidentd");
2014-08-13 23:43:11 +00:00
module.exports = ClientManager;
function ClientManager() {
2016-04-26 20:41:08 +00:00
var config = Helper.getConfig();
this.clients = [];
2016-04-26 20:41:08 +00:00
if (typeof config.oidentd === "string") {
this.identHandler = new oidentd(config.oidentd);
}
2014-08-13 23:43:11 +00:00
}
ClientManager.prototype.findClient = function(name) {
for (var i in this.clients) {
var client = this.clients[i];
2015-09-30 22:39:57 +00:00
if (client.name === name) {
return client;
}
}
return false;
};
2014-09-24 22:23:54 +00:00
ClientManager.prototype.loadUsers = function() {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
2014-08-14 01:51:54 +00:00
for (var i in users) {
2014-09-24 22:23:54 +00:00
this.loadUser(users[i]);
2014-08-14 01:51:54 +00:00
}
};
ClientManager.prototype.loadUser = function(name) {
try {
var 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(
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 = [];
mkdirp.sync(Helper.USERS_PATH);
2014-08-13 23:43:11 +00:00
try {
var files = fs.readdirSync(Helper.USERS_PATH);
2014-10-14 22:24:15 +00:00
files.forEach(function(file) {
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;
};
2014-08-14 01:51:54 +00:00
ClientManager.prototype.addUser = function(name, password) {
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 {
mkdirp.sync(Helper.USERS_PATH);
if (require("path").basename(name) !== name) {
throw new Error(name + " is an invalid username.");
}
2014-08-14 01:51:54 +00:00
var user = {
user: name,
password: password || "",
2014-10-14 19:25:04 +00:00
log: false,
2014-08-14 01:51:54 +00:00
networks: []
};
fs.writeFileSync(
Helper.getUserConfigPath(name),
JSON.stringify(user, null, " ")
2014-08-14 01:51:54 +00:00
);
2015-09-30 22:39:57 +00:00
} catch (e) {
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
};
ClientManager.prototype.updateUser = function(name, opts) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
return false;
}
if (typeof opts === "undefined") {
return false;
}
var user = {};
try {
user = this.readUserConfig(name);
_.assign(user, opts);
fs.writeFileSync(
Helper.getUserConfigPath(name),
JSON.stringify(user, null, " ")
);
} catch (e) {
2016-04-16 11:32:38 +00:00
log.error("Failed to update user", e);
return;
}
return true;
};
ClientManager.prototype.readUserConfig = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
return false;
}
var user = {};
var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
user = JSON.parse(data);
return user;
};
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 {
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
};
2014-09-24 22:23:54 +00:00
2015-09-30 22:39:57 +00:00
ClientManager.prototype.autoload = function(/* sockets */) {
var self = this;
2014-10-14 19:13:29 +00:00
setInterval(function() {
2016-02-14 17:09:51 +00:00
var loaded = _.map(
2014-10-14 19:13:29 +00:00
self.clients,
"name"
);
var added = _.difference(self.getUsers(), loaded);
_.each(added, function(name) {
self.loadUser(name);
});
var removed = _.difference(loaded, self.getUsers());
_.each(removed, function(name) {
var client = _.find(
self.clients, {
name: name
}
);
if (client) {
client.quit();
self.clients = _.without(self.clients, client);
2016-04-16 11:32:38 +00:00
log.info("User '" + name + "' disconnected");
2014-10-14 19:13:29 +00:00
}
});
}, 1000);
2014-09-24 22:23:54 +00:00
};