2014-10-09 15:46:12 +00:00
|
|
|
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 = {
|
2014-10-11 10:09:27 +00:00
|
|
|
enable: false,
|
2014-10-09 15:46:12 +00:00
|
|
|
port: 30113,
|
|
|
|
default: "shout"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Identd.prototype.respond = function(remoteIp, localPort, remotePort) {
|
|
|
|
// Build the first part (always the same)
|
|
|
|
var response = localPort + ", " + remotePort + " : ";
|
2014-10-11 10:09:27 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-10-09 15:46:12 +00:00
|
|
|
|
|
|
|
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 {
|
2014-10-11 10:09:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-10-09 15:46:12 +00:00
|
|
|
}
|
|
|
|
// responses must end with CR+LF
|
|
|
|
return response + "\r\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
Identd.prototype.parse = function(request) {
|
|
|
|
// myPort, theirPort\r\n
|
2014-10-11 10:09:27 +00:00
|
|
|
request = request.toString().split(/,\s*/);
|
2014-10-09 15:46:12 +00:00
|
|
|
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);
|
2014-10-11 10:09:27 +00:00
|
|
|
|
|
|
|
if (this.config.enable) {
|
2014-10-09 15:46:12 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// create the server
|
|
|
|
this.server = net.createServer(function(socket) {
|
|
|
|
socket.on('data', function(data) {
|
2014-10-11 10:09:27 +00:00
|
|
|
var parsed = self.parse(data);
|
2014-10-09 15:46:12 +00:00
|
|
|
// parse and generate a response
|
2014-10-11 10:09:27 +00:00
|
|
|
var response = self.respond(socket.remoteAddress, parsed[0], parsed[1]);
|
2014-10-09 15:46:12 +00:00
|
|
|
socket.write(response);
|
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.server.listen(this.config.port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
Identd.prototype.addConnection = function(connection) {
|
2014-10-11 10:09:27 +00:00
|
|
|
if (this.config.enable) {
|
2014-10-09 15:46:12 +00:00
|
|
|
this.connections.push(connection);
|
2014-10-11 10:09:27 +00:00
|
|
|
}
|
2014-10-09 15:46:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Identd.prototype.removeConnection = function(connection) {
|
2014-10-11 10:09:27 +00:00
|
|
|
if (this.config.enable) {
|
2014-10-09 15:46:12 +00:00
|
|
|
this.connections = _.without(this.connections, connection);
|
2014-10-11 10:09:27 +00:00
|
|
|
}
|
2014-10-09 15:46:12 +00:00
|
|
|
};
|
|
|
|
|
2014-10-11 10:09:27 +00:00
|
|
|
module.exports = new Identd();
|