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");
|
2014-09-13 12:23:17 +00:00
|
|
|
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();
|
|
|
|
|
2014-08-14 16:35:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-14 16:35:37 +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) {
|
2014-08-14 16:35:37 +00:00
|
|
|
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 {
|
2016-02-17 00:14:43 +00:00
|
|
|
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(
|
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 = [];
|
2016-05-08 06:21:31 +00:00
|
|
|
mkdirp.sync(Helper.USERS_PATH);
|
2014-08-13 23:43:11 +00:00
|
|
|
try {
|
2016-05-08 06:21:31 +00:00
|
|
|
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 {
|
2016-05-08 06:21:31 +00:00
|
|
|
mkdirp.sync(Helper.USERS_PATH);
|
2016-04-02 21:19:57 +00:00
|
|
|
|
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 || "",
|
2014-10-14 19:25:04 +00:00
|
|
|
log: false,
|
2014-08-14 01:51:54 +00:00
|
|
|
networks: []
|
|
|
|
};
|
|
|
|
fs.writeFileSync(
|
2016-05-08 06:21:31 +00:00
|
|
|
Helper.getUserConfigPath(name),
|
2016-02-28 02:04:09 +00:00
|
|
|
JSON.stringify(user, null, " ")
|
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-02-17 00:14:43 +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;
|
|
|
|
}
|
|
|
|
|
2016-05-08 06:21:31 +00:00
|
|
|
var user = {};
|
2016-02-17 00:14:43 +00:00
|
|
|
try {
|
|
|
|
user = this.readUserConfig(name);
|
2016-04-02 08:16:31 +00:00
|
|
|
_.assign(user, opts);
|
2016-03-05 18:31:29 +00:00
|
|
|
fs.writeFileSync(
|
2016-05-08 06:21:31 +00:00
|
|
|
Helper.getUserConfigPath(name),
|
2016-03-05 18:31:29 +00:00
|
|
|
JSON.stringify(user, null, " ")
|
|
|
|
);
|
2016-02-17 00:14:43 +00:00
|
|
|
} catch (e) {
|
2016-04-16 11:32:38 +00:00
|
|
|
log.error("Failed to update user", e);
|
2016-02-17 00:14:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.readUserConfig = function(name) {
|
|
|
|
var users = this.getUsers();
|
|
|
|
if (users.indexOf(name) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var user = {};
|
2016-05-08 06:21:31 +00:00
|
|
|
var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
|
2016-02-17 00:14:43 +00:00
|
|
|
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 {
|
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
|
|
|
};
|
2014-09-24 22:23:54 +00:00
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
ClientManager.prototype.autoload = function(/* sockets */) {
|
2014-10-12 08:30:22 +00:00
|
|
|
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
|
|
|
};
|