Identd in Proxy
authorDarren <darren@darrenwhitlen.com>
Wed, 22 Jan 2014 16:56:53 +0000 (16:56 +0000)
committerDarren <darren@darrenwhitlen.com>
Wed, 22 Jan 2014 16:56:53 +0000 (16:56 +0000)
proxytest.js
server/proxy.js

index 09687adb1abf4ec259dbf3fbefa9684bae5ddca2..58c155315081b3b660b49a0c47b5d3c69eacd71c 100644 (file)
@@ -1,4 +1,37 @@
-var Proxy = require('./server/proxy');
+var Proxy  = require('./server/proxy'),
+    Identd = require('./server/identd');
+
+var port_pairs = {};
+
 
 var serv = new Proxy.ProxyServer();
-serv.listen(7779, '127.0.0.1');
\ No newline at end of file
+serv.listen(7779, '127.0.0.1');
+
+serv.on('connection_open', function(pipe) {
+    pipe.identd_pair = pipe.irc_socket.localPort.toString() + '_' + pipe.irc_socket.remotePort.toString();
+    console.log('[IDENTD] opened ' + pipe.identd_pair);
+    port_pairs[pipe.identd_pair] = pipe.meta;
+});
+
+serv.on('connection_close', function(pipe) {
+    console.log('[IDENTD] closed ' + pipe.identd_pair);
+    delete port_pairs[pipe.identd_pair];
+});
+
+
+
+
+// Username lookup function for the identd
+var identdResolveUser = function(port_here, port_there, callback) {
+    var key = port_here.toString() + '_' + port_there.toString();
+    console.log('[IDENTD] lookup ' + key);
+    callback(port_pairs[key].username);
+};
+
+var identd_server = new Identd({
+        bind_addr: '0.0.0.0',
+        bind_port: 113,
+        user_id: identdResolveUser
+    });
+
+identd_server.start();
\ No newline at end of file
index ca55da19a99c49b563244928fa3b034ea92a70ef..8f43426670be87abfc5fd2c54c555c55a7ecb77f 100644 (file)
@@ -1,8 +1,8 @@
 var stream = require('stream'),
     util   = require('util'),
+    events = require('events'),
     net    = require("net"),
-    tls    = require("tls"),
-    Identd = require('./identd');
+    tls    = require("tls");
 
 
 module.exports = {
@@ -29,35 +29,21 @@ var RESPONSE_ETIMEDOUT     = '5';
  * Listens for connections from a kiwi server, dispatching ProxyPipe
  * instances for each connection
  */
-function ProxyServer() {}
+function ProxyServer() {
+    events.EventEmitter.call(this);
+}
+util.inherits(ProxyServer, events.EventEmitter);
 
 
 ProxyServer.prototype.listen = function(listen_port, listen_addr) {
     var that = this;
 
-    // Username lookup function for the identd
-    var identdResolveUser = function(port_here, port_there, callback) {
-        var key = port_here.toString() + '_' + port_there.toString();
-
-        global.data.get(key, function(err, val) {
-            callback(val);
-        });
-    };
-    /*
-    this.identd_server = new Identd({
-        bind_addr: global.config.identd.address,
-        bind_port: global.config.identd.port,
-        user_id: identdResolveUser
-    });
-
-    this.identd_server.start();
-    */
     // Start listening for proxy connections connections
     this.server = new net.Server();
     this.server.listen(listen_port, listen_addr);
 
     this.server.on('connection', function(socket) {
-        new ProxyPipe(socket);
+        new ProxyPipe(socket, that);
     });
 };
 
@@ -82,11 +68,12 @@ ProxyServer.prototype.close = function(callback) {
  * 3. Reply to the kiwi server with connection status
  * 4. If all ok, pipe data between the 2 sockets as a proxy
  */
-function ProxyPipe(kiwi_socket) {
-    this.kiwi_socket = kiwi_socket;
-    this.irc_socket  = null;
-    this.buffer      = '';
-    this.meta        = null;
+function ProxyPipe(kiwi_socket, proxy_server) {
+    this.kiwi_socket  = kiwi_socket;
+    this.proxy_server = proxy_server;
+    this.irc_socket   = null;
+    this.buffer       = '';
+    this.meta         = null;
 
     kiwi_socket.setEncoding('utf8');
     kiwi_socket.on('readable', this.kiwiSocketOnReadable.bind(this));
@@ -157,6 +144,8 @@ ProxyPipe.prototype.makeIrcConnection = function() {
 ProxyPipe.prototype._onSocketConnect = function() {
     debug('[KiwiProxy] ProxyPipe::_onSocketConnect()');
 
+    this.proxy_server.emit('connection_open', this);
+
     // Now that we're connected to the detination, return no
     // error back to the kiwi server and start piping
     this.kiwi_socket.write(new Buffer(RESPONSE_OK.toString()), this.startPiping.bind(this));
@@ -185,6 +174,7 @@ ProxyPipe.prototype._onSocketTimeout = function() {
 
 ProxyPipe.prototype._onSocketClose = function() {
     debug('[KiwiProxy] IRC Socket closed');
+    this.proxy_server.emit('connection_close', this);
     this.destroy();
 };