hardlounge/src/oidentd.js

50 lines
1.1 KiB
JavaScript

"use strict";
const fs = require("fs");
const Helper = require("./helper");
function OidentdFile(file) {
this.file = Helper.expandHome(file);
this.connectionId = 0;
this.connections = new Map();
this.refresh();
}
OidentdFile.prototype = {
addSocket: function(socket, user) {
const id = this.connectionId++;
this.connections.set(id, {socket: socket, user: user});
this.refresh();
return id;
},
removeSocket: function(id) {
this.connections.delete(id);
this.refresh();
},
refresh: function() {
let file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
this.connections.forEach((connection) => {
file += "to " + connection.socket.remoteAddress
+ " lport " + connection.socket.localPort
+ " from " + connection.socket.localAddress
+ " fport " + connection.socket.remotePort
+ " { reply \"" + connection.user + "\" }\n";
});
fs.writeFile(this.file, file, {flag: "w+"}, function(err) {
if (err) {
log.error("Failed to update oidentd file!", err);
}
});
},
};
module.exports = OidentdFile;