Merge pull request #804 from thelounge/xpaw/new-identd

Rewrite identd server, combine with oidentd
This commit is contained in:
Jérémie Astori 2017-03-13 02:04:24 -04:00 committed by GitHub
commit ff72ebbb74
6 changed files with 150 additions and 152 deletions

View File

@ -5,18 +5,26 @@ var colors = require("colors/safe");
var fs = require("fs"); var fs = require("fs");
var Client = require("./client"); var Client = require("./client");
var Helper = require("./helper"); var Helper = require("./helper");
var Oidentd = require("./oidentd");
module.exports = ClientManager; module.exports = ClientManager;
function ClientManager() { function ClientManager() {
this.clients = []; this.clients = [];
if (typeof Helper.config.oidentd === "string") {
this.identHandler = new Oidentd(Helper.config.oidentd);
}
} }
ClientManager.prototype.init = function(identHandler, sockets) {
this.sockets = sockets;
this.identHandler = identHandler;
if (!Helper.config.public) {
if ("autoload" in Helper.config) {
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
}
this.autoloadUsers();
}
};
ClientManager.prototype.findClient = function(name, token) { ClientManager.prototype.findClient = function(name, token) {
for (var i in this.clients) { for (var i in this.clients) {
var client = this.clients[i]; var client = this.clients[i];

View File

@ -1,55 +0,0 @@
"use strict";
var _ = require("lodash");
var net = require("net");
var users = {};
var enabled = false;
module.exports.start = function(port) {
port = port || 113;
log.info("Starting identd server on port", port);
net.createServer(init).listen(port);
enabled = true;
};
module.exports.hook = function(stream, user) {
var socket = stream.socket || stream;
var ports = _.pick(socket, "localPort", "remotePort");
var id = _.values(ports).join(", ");
users[id] = user;
socket.on("close", function() {
delete users[id];
});
};
module.exports.isEnabled = function() {
return enabled;
};
function init(socket) {
socket.on("data", function(data) {
respond(socket, data);
});
}
function respond(socket, data) {
var id = parse(data);
var response = id + " : ";
if (users[id]) {
response += "USERID : UNIX : " + users[id];
} else {
response += "ERROR : NO-USER";
}
response += "\r\n";
socket.write(response);
socket.end();
}
function parse(data) {
data = data.toString();
data = data.split(",");
return parseInt(data[0]) + ", " + parseInt(data[1]);
}

108
src/identification.js Normal file
View File

@ -0,0 +1,108 @@
"use strict";
const fs = require("fs");
const net = require("net");
const colors = require("colors/safe");
const Helper = require("./helper");
class Identification {
constructor(startedCallback) {
this.connectionId = 0;
this.connections = new Map();
if (typeof Helper.config.oidentd === "string") {
this.oidentdFile = Helper.expandHome(Helper.config.oidentd);
log.info(`Oidentd file: ${colors.green(this.oidentdFile)}`);
this.refresh();
}
if (Helper.config.identd.enable) {
if (this.oidentdFile) {
log.warn("Using both identd and oidentd at the same time, this is most likely not intended.");
}
var server = net.createServer(this.serverConnection.bind(this));
server.listen({
port: Helper.config.identd.port || 113,
host: Helper.config.bind || Helper.config.host,
}, () => {
var address = server.address();
log.info(`Identd server available on ${colors.green(address.address + ":" + address.port)}`);
startedCallback();
});
} else {
startedCallback();
}
}
serverConnection(socket) {
socket.on("data", data => {
this.respondToIdent(socket, data);
socket.end();
});
}
respondToIdent(socket, data) {
data = data.toString().split(",");
const lport = parseInt(data[0]);
const fport = parseInt(data[1]);
if (lport < 1 || fport < 1 || lport > 65535 || fport > 65535) {
return;
}
for (var connection of this.connections.values()) {
if (connection.socket.remoteAddress === socket.remoteAddress
&& connection.socket.remotePort === fport
&& connection.socket.localPort === lport
&& connection.socket.localAddress === socket.localAddress) {
return socket.write(`${lport}, ${fport} : USERID : UNIX : ${connection.user}\r\n`);
}
}
socket.write(`${lport}, ${fport} : ERROR : NO-USER\r\n`);
}
addSocket(socket, user) {
const id = ++this.connectionId;
this.connections.set(id, {socket: socket, user: user});
if (this.oidentdFile) {
this.refresh();
}
return id;
}
removeSocket(id) {
this.connections.delete(id);
if (this.oidentdFile) {
this.refresh();
}
}
refresh() {
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.oidentdFile, file, {flag: "w+"}, function(err) {
if (err) {
log.error("Failed to update oidentd file!", err);
}
});
}
}
module.exports = Identification;

View File

@ -1,49 +0,0 @@
"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;

View File

@ -1,6 +1,5 @@
"use strict"; "use strict";
var identd = require("../../identd");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Helper = require("../../helper"); var Helper = require("../../helper");
@ -63,23 +62,18 @@ module.exports = function(irc, network) {
}), true); }), true);
}); });
if (identd.isEnabled()) { let identSocketId;
irc.on("raw socket connected", function(socket) {
identd.hook(socket, client.name || network.username);
});
}
if (identHandler) { irc.on("raw socket connected", function(socket) {
let identSocketId; identSocketId = identHandler.addSocket(socket, client.name || network.username);
});
irc.on("raw socket connected", function(socket) { irc.on("socket close", function() {
identSocketId = identHandler.addSocket(socket, client.name || network.username); if (identSocketId > 0) {
});
irc.on("socket close", function() {
identHandler.removeSocket(identSocketId); identHandler.removeSocket(identSocketId);
}); identSocketId = 0;
} }
});
if (Helper.config.debug.ircFramework) { if (Helper.config.debug.ircFramework) {
irc.on("debug", function(message) { irc.on("debug", function(message) {

View File

@ -11,12 +11,16 @@ var dns = require("dns");
var Helper = require("./helper"); var Helper = require("./helper");
var ldap = require("ldapjs"); var ldap = require("ldapjs");
var colors = require("colors/safe"); var colors = require("colors/safe");
const Identification = require("./identification");
let identHandler = null;
var manager = null; var manager = null;
var authFunction = localAuth; var authFunction = localAuth;
module.exports = function() { module.exports = function() {
manager = new ClientManager(); log.info(`The Lounge ${colors.green(Helper.getVersion())} \
(node ${colors.green(process.versions.node)} on ${colors.green(process.platform)} ${process.arch})`);
log.info(`Configuration file: ${colors.green(Helper.CONFIG_PATH)}`);
if (!fs.existsSync("client/js/bundle.js")) { if (!fs.existsSync("client/js/bundle.js")) {
log.error(`The client application was not built. Run ${colors.bold("NODE_ENV=production npm run build")} to resolve this.`); log.error(`The client application was not built. Run ${colors.bold("NODE_ENV=production npm run build")} to resolve this.`);
@ -37,7 +41,7 @@ module.exports = function() {
if (!config.https.enable) { if (!config.https.enable) {
server = require("http"); server = require("http");
server = server.createServer(app).listen(config.port, config.host); server = server.createServer(app);
} else { } else {
server = require("spdy"); server = require("spdy");
const keyPath = Helper.expandHome(config.https.key); const keyPath = Helper.expandHome(config.https.key);
@ -53,16 +57,18 @@ module.exports = function() {
server = server.createServer({ server = server.createServer({
key: fs.readFileSync(keyPath), key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath) cert: fs.readFileSync(certPath)
}, app).listen(config.port, config.host); }, app);
} }
if (config.identd.enable) { server.listen({
if (manager.identHandler) { port: config.port,
log.warn("Using both identd and oidentd at the same time!"); host: config.host,
} }, () => {
const protocol = config.https.enable ? "https" : "http";
require("./identd").start(config.identd.port); var address = server.address();
} log.info(`Available on ${colors.green(protocol + "://" + address.address + ":" + address.port + "/")} \
in ${config.public ? "public" : "private"} mode`);
});
if (!config.public && (config.ldap || {}).enable) { if (!config.public && (config.ldap || {}).enable) {
authFunction = ldapAuth; authFunction = ldapAuth;
@ -81,25 +87,11 @@ module.exports = function() {
} }
}); });
manager.sockets = sockets; manager = new ClientManager();
const protocol = config.https.enable ? "https" : "http"; identHandler = new Identification(() => {
const host = config.host || "*"; manager.init(identHandler, sockets);
});
log.info(`The Lounge ${colors.green(Helper.getVersion())} is now running \
using node ${colors.green(process.versions.node)} on ${colors.green(process.platform)} (${process.arch})`);
log.info(`Configuration file: ${colors.green(Helper.CONFIG_PATH)}`);
log.info(`Available on ${colors.green(protocol + "://" + host + ":" + config.port + "/")} \
in ${config.public ? "public" : "private"} mode`);
log.info("Press Ctrl-C to stop\n");
if (!config.public) {
if ("autoload" in config) {
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
}
manager.autoloadUsers();
}
}; };
function getClientIp(req) { function getClientIp(req) {