cb6e8d578276dde56281ffaaec9f5a62a5d364aa
1 var net
= require('net'),
2 winston
= require('winston');
4 var IdentdServer
= module
.exports = function(opts
) {
8 var default_user_id
= 'kiwi',
9 default_system_id
= 'UNIX-KiwiIRC';
13 opts
.bind_addr
= opts
.bind_addr
|| '0.0.0.0';
14 opts
.bind_port
= opts
.bind_port
|| 113;
15 opts
.system_id
= opts
.system_id
|| default_system_id
;
16 opts
.user_id
= opts
.user_id
|| default_user_id
;
19 var server
= net
.createServer(function(socket
) {
22 socket
.on('data', function(data
){
23 var data_line
, response
;
25 buffer
+= data
.toString();
27 // If we exceeed 512 bytes, presume a flood and disconnect
28 if (buffer
.length
< 512) {
30 // Wait until we have a full line of data before processing it
31 if (buffer
.indexOf('\n') === -1)
34 // Get the first line of data and process it for a rsponse
35 data_line
= buffer
.split('\n')[0];
36 response
= that
.processLine(data_line
);
40 // Close down the socket while sending the response
41 socket
.removeAllListeners();
47 server
.on('listening', function() {
48 var addr
= server
.address();
49 //console.log('Ident Server listening on ' + server.address().address + ':' + server.address().port);
50 winston
.info('Ident Server listening on %s:%s', addr
.address
, addr
.port
);
54 this.start = function() {
55 server
.listen(opts
.bind_port
, opts
.bind_addr
);
58 this.stop = function(callback
) {
59 server
.close(callback
);
64 * Process a line of data for an Identd response
66 * @param {String} The line of data to process
67 * @return {String} Data to send back to the Identd client
69 this.processLine = function(line
) {
70 var ports
= line
.split(','),
74 // We need 2 port number to make this work
78 port_here
= parseInt(ports
[0], 10);
79 port_there
= parseInt(ports
[1], 10);
81 // Make sure we have both ports to work with
82 if (!port_here
|| !port_there
)
85 if (typeof opts
.user_id
=== 'function') {
86 user
= (opts
.user_id(port_here
, port_there
) || '').toString() || default_user_id
;
88 user
= opts
.user_id
.toString();
91 if (typeof opts
.system_id
=== 'function') {
92 system
= (opts
.system_id(port_here
, port_there
) || '').toString() || default_system_id
;
94 system
= opts
.system_id
.toString();
97 return port_here
.toString() + ' , ' + port_there
.toString() + ' : USERID : ' + system
+ ' : ' + user
;