Refactoring

This commit is contained in:
Mattias Erming 2014-10-11 19:33:28 +02:00
parent 3918c0ab61
commit 95bebfe12c
4 changed files with 53 additions and 124 deletions

View File

@ -198,31 +198,26 @@ module.exports = {
}, },
// //
// Allows shout to run as an identd // Run Shout with identd support.
// //
// @type object // @type object
// @default {} // @default {}
//
identd: { identd: {
// //
// Enable Identd daemon // Run the identd daemon on server start.
// //
// @type boolean // @type boolean
// @default true // @default false
enable: true, //
enable: false,
// //
// Port to listen for ident requests // Port to listen for ident requests.
// //
// @type int // @type int
// @default 30113 // @default 113
port: 30113,
// //
// Default user to return on unknown request port: 113
// if false, will return NOUSER
//
// @type string
// @default shout
default: "shout"
} }
}; };

View File

@ -1,6 +1,7 @@
var _ = require("lodash"); var _ = require("lodash");
var Chan = require("./models/chan"); var Chan = require("./models/chan");
var crypto = require("crypto"); var crypto = require("crypto");
var identd = require("./identd");
var log = require("./log"); var log = require("./log");
var net = require("net"); var net = require("net");
var Msg = require("./models/msg"); var Msg = require("./models/msg");
@ -8,7 +9,6 @@ var Network = require("./models/network");
var slate = require("slate-irc"); var slate = require("slate-irc");
var tls = require("tls"); var tls = require("tls");
var Helper = require("./helper"); var Helper = require("./helper");
var identd = require("./identd");
module.exports = Client; module.exports = Client;
@ -126,9 +126,8 @@ Client.prototype.connect = function(args) {
rejectUnauthorized: false rejectUnauthorized: false
}; };
if(config.bind) { if (config.bind) {
server.localAddress = config.bind; server.localAddress = config.bind;
if(args.tls) { if(args.tls) {
var socket = net.connect(server); var socket = net.connect(server);
server.socket = socket; server.socket = socket;
@ -136,27 +135,8 @@ Client.prototype.connect = function(args) {
} }
var stream = args.tls ? tls.connect(server) : net.connect(server); 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); (stream.socket || stream).on("error", function(e) {
};
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); console.log("Client#connect():\n" + e);
stream.end(); stream.end();
var msg = new Msg({ var msg = new Msg({
@ -168,15 +148,12 @@ Client.prototype.connect = function(args) {
}); });
}); });
stream.on("close", function() {
identd.removeConnection(identdItem);
});
var nick = args.nick || "shout-user"; var nick = args.nick || "shout-user";
var username = args.username || nick; var username = args.username || nick;
var realname = args.realname || "Shout User"; var realname = args.realname || "Shout User";
var irc = slate(stream); var irc = slate(stream);
identd.hook(stream, username);
if (args.password) { if (args.password) {
irc.pass(args.password); irc.pass(args.password);

View File

@ -1,90 +1,47 @@
var _ = require("lodash"); var _ = require("lodash");
var net = require("net"); var net = require("net");
var Helper = require("./helper");
function Identd() { var users = {};
// used to store who is connecting...
this.connections = []; module.exports.start = function(port) {
this.server = null; var server = net.createServer(init).listen(port || 113);
this.config = {
enable: false,
port: 30113,
default: "shout"
};
}; };
Identd.prototype.respond = function(remoteIp, localPort, remotePort) { module.exports.hook = function(stream, user) {
// Build the first part (always the same) var id = "";
var response = localPort + ", " + remotePort + " : "; var socket = stream.socket || stream;
var params = {"remoteIp": remoteIp, "remotePort": remotePort, "localPort": localPort}; socket.on("connect", function() {
var connection = _.where(this.connections, params); var ports = _.pick(socket, "localPort", "remotePort");
if (connection.length == 0) { id = _.values(ports).join(", ");
params["localPort"] = undefined; users[id] = user;
connection = _.where(this.connections, params); });
} socket.on("close", function() {
delete users[id];
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 {
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";
}; };
Identd.prototype.parse = function(request) { function init(socket) {
// myPort, theirPort\r\n socket.on("data", function(data) {
request = request.toString().split(/,\s*/); respond(socket, data);
if (request.length == 2) { });
var localPort = parseInt(request[0]), }
remotePort = parseInt(request[1]);
function respond(socket, data) {
var id = parse(data);
var response = id + " : ";
if (users[id]) {
response += "USERID : UNIX : " + users[id];
} else {
response += "ERROR : NO-USER";
} }
return [localPort, remotePort]; response += "\r\n";
};
Identd.prototype.start = function(config) {
_.merge(this.config, config.identd);
if (this.config.enable) {
var self = this;
// create the server
this.server = net.createServer(function(socket) {
socket.on('data', function(data) {
var parsed = self.parse(data);
// parse and generate a response
var response = self.respond(socket.remoteAddress, parsed[0], parsed[1]);
socket.write(response); socket.write(response);
socket.end(); socket.end();
}); }
});
this.server.listen(this.config.port);
}
return self; function parse(data) {
}; data = data.toString();
data = data.split(",");
return parseInt(data[0]) + ", " + parseInt(data[1]);
}
Identd.prototype.addConnection = function(connection) {
if (this.config.enable) {
this.connections.push(connection);
}
};
Identd.prototype.removeConnection = function(connection) {
if (this.config.enable) {
this.connections = _.without(this.connections, connection);
}
};
module.exports = new Identd();

View File

@ -36,8 +36,8 @@ module.exports = function(options) {
}, app).listen(port, host) }, app).listen(port, host)
} }
if (config.identd && config.identd.enable) { if ((config.identd || {}).enable) {
require("./identd").start(config); require("./identd").start(config.identd.port);
} }
sockets = io(server); sockets = io(server);