1 var net
= require('net');
3 var IdentdServer
= module
.exports = function(opts
) {
9 opts
.bind_addr
= opts
.bind_addr
|| '0.0.0.0';
10 opts
.bind_port
= opts
.bind_port
|| 113;
11 opts
.system_id
= opts
.system_id
|| 'UNIX-KiwiIRC';
12 opts
.user_id
= opts
.user_id
|| 'kiwi';
15 var server
= net
.createServer(function(socket
) {
18 socket
.on('data', function(data
){
19 var data_line
, response
;
21 buffer
+= data
.toString();
23 // Wait until we have a full line of data before processing it
24 if (buffer
.indexOf('\n') === -1)
27 // Get the first line of data and process it for a rsponse
28 data_line
= buffer
.split('\n')[0];
29 response
= that
.processLine(data_line
);
31 // Close down the socket while sending the response
32 socket
.removeAllListeners();
38 server
.on('listening', function() {
39 console
.log('Ident Server listening on ' + server
.address().address
+ ':' + server
.address().port
);
43 this.start = function() {
44 server
.listen(opts
.bind_port
, opts
.bind_addr
);
47 this.stop = function(callback
) {
48 server
.close(callback
);
53 * Process a line of data for an Identd response
55 * @param {String} The line of data to process
56 * @return {String} Data to send back to the Identd client
58 this.processLine = function(line
) {
59 var ports
= line
.split(','),
63 // We need 2 port number to make this work
67 port_here
= parseInt(ports
[0], 10);
68 port_there
= parseInt(ports
[1], 10);
70 // Make sure we have both ports to work with
71 if (!port_here
|| !port_there
)
74 if (typeof opts
.user_id
=== 'function') {
75 user
= opts
.user_id(port_here
, port_there
).toString();
77 user
= opts
.user_id
.toString();
80 if (typeof opts
.system_id
=== 'function') {
81 system
= opts
.system_id(port_here
, port_there
).toString();
83 system
= opts
.system_id
.toString();
86 return port_here
.toString() + ' , ' + port_there
.toString() + ' : USERID : ' + system
+ ' : ' + user
;