From 898fed76c6ccdc3cd175be96369a6a6126c37c2c Mon Sep 17 00:00:00 2001 From: David White Date: Thu, 9 Oct 2014 16:46:12 +0100 Subject: [PATCH 1/4] Add initial support for identd --- config.js | 29 +++++++++++++++++++ src/client.js | 17 ++++++++++++ src/identd.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/identd.js diff --git a/config.js b/config.js index 4e41abb..dc3840d 100644 --- a/config.js +++ b/config.js @@ -176,5 +176,34 @@ module.exports = { // @default "" // certificate: "" + }, + + // + // Allows shout to run as an identd + // + // @type object + // @default {} + identd: { + // + // Enable Identd daemon + // + // @type boolean + // @default true + enable: true, + + // + // Port to listen for ident requests + // + // @type int + // @default 30113 + port: 30113, + + // + // Default user to return on unknown request + // if false, will return NOUSER + // + // @type string + // @default shout + default: "shout" } }; diff --git a/src/client.js b/src/client.js index 55ef099..a842ac3 100644 --- a/src/client.js +++ b/src/client.js @@ -8,6 +8,7 @@ var Network = require("./models/network"); var slate = require("slate-irc"); var tls = require("tls"); var Helper = require("./helper"); +var identd = require("./identd"); module.exports = Client; @@ -137,9 +138,25 @@ Client.prototype.connect = function(args) { }); }); + var nick = args.nick || "shout-user"; var username = args.username || nick; var realname = args.realname || "Shout User"; + var identdItem = null; + + stream.on("connect", function() { + identdItem = { + "remoteIp": stream.remoteAddress, + "remotePort": stream.remotePort, + "localPort": stream.localPort, + "username": username + }; + identd.addConnection(identdItem); + }); + + stream.on("close", function() { + identd.addConnection(identdItem); + }); var irc = slate(stream); diff --git a/src/identd.js b/src/identd.js new file mode 100644 index 0000000..7b5b147 --- /dev/null +++ b/src/identd.js @@ -0,0 +1,77 @@ +var _ = require("lodash"); +var net = require("net"); +var Helper = require("./helper"); + +function Identd() { + // used to store who is connecting... + this.connections = []; + this.server = null; + this.config = { + enabled: false, + port: 30113, + default: "shout" + }; +}; + +Identd.prototype.respond = function(remoteIp, localPort, remotePort) { + // Build the first part (always the same) + var response = localPort + ", " + remotePort + " : "; + var connection = _.where(this.connections, {"remoteIp": remoteIp, "remotePort": remotePort, "localPort": localPort}); + + if (connection.length > 0) { + // We have some connections, but we only want the first + connection = _.first(connection); + response += " USERID : UNIX : " + connection.username; + // We're done with that connection. Remove it + this.removeConnection(connection); + } else { + response += " ERROR : NOUSER"; + } + // responses must end with CR+LF + return response + "\r\n"; +}; + +Identd.prototype.parse = function(request) { + // myPort, theirPort\r\n + request = request.split(/,\s*/); + if (request.length == 2) { + var localPort = parseInt(request[0]), + remotePort = parseInt(request[1]); + } + return [localPort, remotePort]; +}; + +Identd.prototype.start = function(config) { + _.merge(this.config, config.identd); + + if (this.config.enabled) { + var self = this; + + // create the server + this.server = net.createServer(function(socket) { + socket.on('data', function(data) { + var parsed = this.parse(data); + // parse and generate a response + var response = this.respond(socket.remoteAddress, parsed[0], parsed[1]); + socket.write(response); + socket.end(); + }); + }); + console.log("Starting identd on " + this.config.port); + this.server.listen(this.config.port); + } + + return self; +}; + +Identd.prototype.addConnection = function(connection) { + if (this.config.enabled) + this.connections.push(connection); +}; + +Identd.prototype.removeConnection = function(connection) { + if (this.config.enabled) + this.connections = _.without(this.connections, connection); +}; + +module.exports = new Identd().start(Helper.getConfig()); From 924bc39a9554f335414b7ff0bf34f65b1b4c483e Mon Sep 17 00:00:00 2001 From: David White Date: Thu, 9 Oct 2014 16:56:32 +0100 Subject: [PATCH 2/4] Fix cut/paste error in close event --- src/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index a842ac3..53a38d2 100644 --- a/src/client.js +++ b/src/client.js @@ -155,7 +155,7 @@ Client.prototype.connect = function(args) { }); stream.on("close", function() { - identd.addConnection(identdItem); + identd.removeConnection(identdItem); }); var irc = slate(stream); From ea0e66afd064bcbc27b167823259efa7cde26372 Mon Sep 17 00:00:00 2001 From: David White Date: Sat, 11 Oct 2014 11:09:27 +0100 Subject: [PATCH 3/4] Set up identd and make it work on connection :sunglasses: --- src/client.js | 40 +++++++++++++++++++++++----------------- src/identd.js | 40 ++++++++++++++++++++++++++++------------ src/server.js | 4 ++++ 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/client.js b/src/client.js index 53a38d2..5632cc9 100644 --- a/src/client.js +++ b/src/client.js @@ -125,7 +125,26 @@ Client.prototype.connect = function(args) { rejectUnauthorized: false }; - var stream = args.tls ? tls.connect(server) : net.connect(server); + var identdItem = null; + + var cb = function() { + identdItem = { + "remoteIp": stream.remoteAddress, + "remotePort": stream.remotePort, + "localPort": stream.localPort, + "username": username + }; + + identd.addConnection(identdItem); + }; + + var stream; + if (args.tls) { + stream = tls.connect(server); + stream.socket.on('connect', cb); + } else { + stream = net.connect(server, cb); + } stream.on("error", function(e) { console.log("Client#connect():\n" + e); stream.end(); @@ -137,26 +156,13 @@ Client.prototype.connect = function(args) { msg: msg }); }); - + stream.on("close", function() { + identd.removeConnection(identdItem); + }); var nick = args.nick || "shout-user"; var username = args.username || nick; var realname = args.realname || "Shout User"; - var identdItem = null; - - stream.on("connect", function() { - identdItem = { - "remoteIp": stream.remoteAddress, - "remotePort": stream.remotePort, - "localPort": stream.localPort, - "username": username - }; - identd.addConnection(identdItem); - }); - - stream.on("close", function() { - identd.removeConnection(identdItem); - }); var irc = slate(stream); diff --git a/src/identd.js b/src/identd.js index 7b5b147..d8d0142 100644 --- a/src/identd.js +++ b/src/identd.js @@ -7,7 +7,7 @@ function Identd() { this.connections = []; this.server = null; this.config = { - enabled: false, + enable: false, port: 30113, default: "shout" }; @@ -16,7 +16,12 @@ function Identd() { Identd.prototype.respond = function(remoteIp, localPort, remotePort) { // Build the first part (always the same) var response = localPort + ", " + remotePort + " : "; - var connection = _.where(this.connections, {"remoteIp": remoteIp, "remotePort": remotePort, "localPort": localPort}); + var params = {"remoteIp": remoteIp, "remotePort": remotePort, "localPort": localPort}; + var connection = _.where(this.connections, params); + if (connection.length == 0) { + params["localPort"] = undefined; + connection = _.where(this.connections, params); + } if (connection.length > 0) { // We have some connections, but we only want the first @@ -25,7 +30,14 @@ Identd.prototype.respond = function(remoteIp, localPort, remotePort) { // We're done with that connection. Remove it this.removeConnection(connection); } else { - response += " ERROR : NOUSER"; + if (this.config.default == null) { + response += " ERROR : NOUSER"; + } else { + var ident = this.config.default.replace(/\?/g, function(a) { + return Math.floor(Math.random() * 10); + }); + response += " USERID : UNIX : " + ident; + } } // responses must end with CR+LF return response + "\r\n"; @@ -33,7 +45,7 @@ Identd.prototype.respond = function(remoteIp, localPort, remotePort) { Identd.prototype.parse = function(request) { // myPort, theirPort\r\n - request = request.split(/,\s*/); + request = request.toString().split(/,\s*/); if (request.length == 2) { var localPort = parseInt(request[0]), remotePort = parseInt(request[1]); @@ -43,21 +55,21 @@ Identd.prototype.parse = function(request) { Identd.prototype.start = function(config) { _.merge(this.config, config.identd); - - if (this.config.enabled) { + + if (this.config.enable) { var self = this; // create the server this.server = net.createServer(function(socket) { socket.on('data', function(data) { - var parsed = this.parse(data); + var parsed = self.parse(data); // parse and generate a response - var response = this.respond(socket.remoteAddress, parsed[0], parsed[1]); + var response = self.respond(socket.remoteAddress, parsed[0], parsed[1]); socket.write(response); socket.end(); }); }); - console.log("Starting identd on " + this.config.port); + log("Starting identd on " + this.config.port); this.server.listen(this.config.port); } @@ -65,13 +77,17 @@ Identd.prototype.start = function(config) { }; Identd.prototype.addConnection = function(connection) { - if (this.config.enabled) + if (this.config.enable) { this.connections.push(connection); + log("Identd: adding:", connection); + } }; Identd.prototype.removeConnection = function(connection) { - if (this.config.enabled) + if (this.config.enable) { this.connections = _.without(this.connections, connection); + log("Identd: removing:", connection); + } }; -module.exports = new Identd().start(Helper.getConfig()); +module.exports = new Identd(); diff --git a/src/server.js b/src/server.js index 1292a21..10acdbc 100644 --- a/src/server.js +++ b/src/server.js @@ -36,6 +36,10 @@ module.exports = function(port, host, isPublic) { }, app).listen(port, host) } + if (config.identd && config.identd.enable) { + require("./identd").start(config); + } + sockets = io(server); sockets.on("connect", function(socket) { if (config.public) { From acd0b296712d666e2e842c3088c08e7ad96e52d7 Mon Sep 17 00:00:00 2001 From: David White Date: Sat, 11 Oct 2014 11:26:28 +0100 Subject: [PATCH 4/4] Remove old debug/logging lines --- src/identd.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/identd.js b/src/identd.js index d8d0142..5b35e86 100644 --- a/src/identd.js +++ b/src/identd.js @@ -69,7 +69,6 @@ Identd.prototype.start = function(config) { socket.end(); }); }); - log("Starting identd on " + this.config.port); this.server.listen(this.config.port); } @@ -79,14 +78,12 @@ Identd.prototype.start = function(config) { Identd.prototype.addConnection = function(connection) { if (this.config.enable) { this.connections.push(connection); - log("Identd: adding:", connection); } }; Identd.prototype.removeConnection = function(connection) { if (this.config.enable) { this.connections = _.without(this.connections, connection); - log("Identd: removing:", connection); } };