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");
|
2014-09-24 22:23:54 +00:00
|
|
|
var moment = require("moment");
|
2014-08-13 23:43:11 +00:00
|
|
|
|
|
|
|
module.exports = ClientManager;
|
|
|
|
|
|
|
|
function ClientManager() {
|
2014-10-12 06:52:51 +00:00
|
|
|
var self = this;
|
2014-08-14 16:35:37 +00:00
|
|
|
this.clients = [];
|
2014-10-12 06:52:51 +00:00
|
|
|
if(!/^win/.test(process.platform)) {
|
|
|
|
process.on('SIGHUP', function() {
|
|
|
|
console.log("Received 'SIGHUP'. Reloading Users.");
|
2014-10-12 08:30:22 +00:00
|
|
|
self.reloadUsers();
|
2014-10-12 06:52:51 +00:00
|
|
|
});
|
|
|
|
}
|
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];
|
|
|
|
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 {
|
2014-08-26 18:00:12 +00:00
|
|
|
var json = fs.readFileSync(
|
2014-10-03 23:33:44 +00:00
|
|
|
Helper.HOME + "/users/" + name + "/user.json",
|
2014-08-26 18:00:12 +00:00
|
|
|
"utf-8"
|
|
|
|
);
|
2014-08-14 01:51:54 +00:00
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 22:23:54 +00:00
|
|
|
if (!json) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.findClient(name)) {
|
|
|
|
this.clients.push(new Client(
|
|
|
|
this.sockets,
|
|
|
|
name,
|
|
|
|
json
|
|
|
|
));
|
2014-10-12 08:30:22 +00:00
|
|
|
console.log("User '%s' loaded.", name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.reloadUsers = function() {
|
|
|
|
var users = this.getUsers();
|
|
|
|
for (var i in users) {
|
|
|
|
this.reloadUser(users[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.reloadUser = function(name) {
|
|
|
|
var client = this.findClient(name);
|
|
|
|
if (client) {
|
|
|
|
try {
|
|
|
|
var json = fs.readFileSync(
|
|
|
|
Helper.HOME + "/users/" + name + "/user.json",
|
|
|
|
"utf-8"
|
|
|
|
);
|
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client.config = json;
|
|
|
|
console.log("User '%s' reloaded.", name);
|
2014-09-24 22:23:54 +00:00
|
|
|
}
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.getUsers = function() {
|
|
|
|
var users = [];
|
2014-10-03 23:33:44 +00:00
|
|
|
var path = Helper.HOME + "/users";
|
2014-08-26 18:00:12 +00:00
|
|
|
mkdirp.sync(path);
|
2014-08-13 23:43:11 +00:00
|
|
|
try {
|
2014-08-26 18:00:12 +00:00
|
|
|
users = fs.readdirSync(path);
|
2014-08-13 23:43:11 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
2014-08-14 17:25:22 +00:00
|
|
|
users = _.without(
|
|
|
|
users,
|
|
|
|
"example"
|
|
|
|
);
|
2014-08-13 23:43:11 +00:00
|
|
|
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 {
|
2014-10-04 22:05:19 +00:00
|
|
|
var path = Helper.HOME + "/users/" + name;
|
2014-08-14 01:51:54 +00:00
|
|
|
var user = {
|
|
|
|
user: name,
|
|
|
|
password: password || "",
|
|
|
|
networks: []
|
|
|
|
};
|
2014-08-13 23:43:11 +00:00
|
|
|
fs.mkdirSync(path);
|
2014-08-14 01:51:54 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
path + "/user.json",
|
2014-08-18 23:54:14 +00:00
|
|
|
JSON.stringify(user, null, " "),
|
|
|
|
{mode: "0777"}
|
2014-08-14 01:51:54 +00:00
|
|
|
);
|
2014-08-13 23:43:11 +00:00
|
|
|
} catch(e) {
|
|
|
|
throw e;
|
|
|
|
}
|
2014-08-14 17:25:22 +00:00
|
|
|
return true;
|
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 {
|
2014-10-03 23:33:44 +00:00
|
|
|
var path = Helper.HOME + "/users/" + name;
|
2014-08-13 23:43:11 +00:00
|
|
|
fs.unlinkSync(path + "/user.json");
|
|
|
|
fs.rmdirSync(path);
|
|
|
|
} catch(e) {
|
|
|
|
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
|
|
|
|
2014-10-12 08:30:22 +00:00
|
|
|
ClientManager.prototype.watchUser = function(name) {
|
2014-09-24 22:23:54 +00:00
|
|
|
var self = this;
|
2014-10-12 08:30:22 +00:00
|
|
|
var client = this.findClient(name);
|
|
|
|
if(!client || client.watcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var path = Helper.HOME + "/users/" + client.name + "/user.json";
|
|
|
|
var lastReload = Date.now();
|
|
|
|
client.watcher = fs.watch(path, {persistent: false}, function(event, filename) {
|
|
|
|
switch (event) {
|
|
|
|
case "change":
|
|
|
|
// user.json modified
|
|
|
|
if(Date.now() - lastReload > 50) {
|
|
|
|
self.reloadUser(client.name);
|
|
|
|
lastReload = Date.now();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-09-24 22:23:54 +00:00
|
|
|
|
2014-10-12 08:30:22 +00:00
|
|
|
ClientManager.prototype.autoload = function(sockets) {
|
|
|
|
var self = this;
|
2014-09-24 22:23:54 +00:00
|
|
|
|
2014-10-12 08:30:22 +00:00
|
|
|
// Listen to new users being added/removed
|
|
|
|
fs.watch(Helper.HOME + "/users/", { persistent: false }, function(event, filename) {
|
|
|
|
switch (event) {
|
|
|
|
case "rename":
|
|
|
|
if(filename === null) {
|
|
|
|
// User removed.
|
|
|
|
var removed = _(self.clients)
|
|
|
|
.pluck('name')
|
|
|
|
.difference(self.getUsers())
|
|
|
|
.value();
|
|
|
|
_.each(removed, function(name) {
|
|
|
|
var client = self.findClient(name);
|
|
|
|
if (client) {
|
|
|
|
client.quit();
|
|
|
|
if(client.watcher) {
|
|
|
|
client.watcher.close();
|
|
|
|
}
|
|
|
|
self.clients = _.without(self.clients, client);
|
|
|
|
console.log("User '%s' disconnected.", name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// User created.
|
|
|
|
self.loadUser(filename);
|
|
|
|
self.watchUser(filename);
|
2014-09-24 22:23:54 +00:00
|
|
|
}
|
2014-10-12 08:30:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen to user modification
|
|
|
|
_.each(this.clients, function(client) {
|
|
|
|
self.watchUser(client.name);
|
|
|
|
});
|
2014-09-24 22:23:54 +00:00
|
|
|
};
|