2016-04-26 20:41:08 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
var Helper = require("./helper");
|
|
|
|
|
2016-06-11 20:03:59 +00:00
|
|
|
function OidentdFile(file) {
|
2016-04-26 20:41:08 +00:00
|
|
|
this.file = Helper.expandHome(file);
|
|
|
|
this.connectionId = 0;
|
|
|
|
this.connections = {};
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2016-06-11 20:03:59 +00:00
|
|
|
OidentdFile.prototype = {
|
2016-04-26 20:41:08 +00:00
|
|
|
hookSocket: function(socket, user) {
|
|
|
|
var that = this;
|
|
|
|
var id = null;
|
|
|
|
|
|
|
|
socket.on("connect", function() {
|
|
|
|
id = that.addSocket(socket, user);
|
|
|
|
that.refresh();
|
|
|
|
});
|
|
|
|
socket.on("close", function() {
|
|
|
|
that.removeConnection(id);
|
|
|
|
that.refresh();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addSocket: function(socket, user) {
|
|
|
|
var id = this.connectionId++;
|
|
|
|
this.connections[id] = {socket: socket, user: user};
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeSocket: function(socket) {
|
|
|
|
for (var id in this.connections) {
|
|
|
|
if (this.connections[id] === socket) {
|
|
|
|
delete this.connections[id];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeConnection: function(id) {
|
|
|
|
delete this.connections[id];
|
|
|
|
},
|
|
|
|
|
|
|
|
getSockets: function() {
|
|
|
|
return this.connections;
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function() {
|
|
|
|
var file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
|
|
|
|
|
|
|
|
function makeRule(connection) {
|
|
|
|
return "to " + connection.socket.remoteAddress
|
|
|
|
+ " lport " + connection.socket.localPort
|
|
|
|
+ " from " + connection.socket.localAddress
|
|
|
|
+ " fport " + connection.socket.remotePort
|
|
|
|
+ " { reply \"" + connection.user + "\" }\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var id in this.connections) {
|
|
|
|
file += makeRule(this.connections[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(this.file, file, {flag: "w+"}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
log.error("Failed to update oidentd file!", err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-06-11 20:03:59 +00:00
|
|
|
module.exports = OidentdFile;
|