Reverse DNS fix. Passing client IP via username #99
authorDarren <darren@darrenwhitlen.com>
Tue, 23 Oct 2012 22:42:54 +0000 (23:42 +0100)
committerDarren <darren@darrenwhitlen.com>
Tue, 23 Oct 2012 22:42:54 +0000 (23:42 +0100)
server/client.js
server/irc/connection.js
server/weblistener.js

index e597666751f77a574fa92a7188e4bea405e57b93..764d600ece700e61b7a5729f8b9b84e4e6e528c7 100755 (executable)
@@ -99,7 +99,7 @@ function kiwiCommand(command, callback) {
                        if (command.hostname && command.port && command.nick) {
                                var con = new IrcConnection(command.hostname, command.port, command.ssl,
                                        command.nick, {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.address.address},
-                                       command.password, null);
+                                       command.password);
 
                                var con_num = this.next_connection++;
                                this.irc_connections[con_num] = con;
index 7c3940b1ff2e3ba20f2ade815bc3e922ea4acd03..2ba7c163c4f54c41d7ba72ef09cb1e52efd4a2c2 100644 (file)
@@ -1,9 +1,11 @@
 var net     = require('net'),
     tls     = require('tls'),
     events  = require('events'),
-    util    = require('util');
+    util    = require('util'),
+    _       = require('underscore'),
+    config  = require('../configuration.js');
 
-var IrcConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
+var IrcConnection = function (hostname, port, ssl, nick, user, pass) {
     var that = this;
     events.EventEmitter.call(this);
     
@@ -34,10 +36,10 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
     this.registered = false;
     this.nick = nick;
     this.user = user;
+    this.irc_host = {hostname: hostname, port: port};
     this.ssl = !(!ssl);
     this.options = Object.create(null);
     
-    this.webirc = webirc;
     this.password = pass;
     this.hold_last = false;
     this.held_data = '';
@@ -66,23 +68,71 @@ var end = function (data, encoding, callback) {
 
 
 var connect_handler = function () {
-    if (this.webirc) {
-        this.write('WEBIRC ' + this.webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
+    var that = this,
+        connect_data;
+
+    // Build up data to be used for webirc/etc detection
+    connect_data = {
+        user: this.user,
+        nick: this.nick,
+        realname: '[www.kiwiirc.com] ' + this.nick,
+        username: this.nick.replace(/[^0-9a-zA-Z\-_.]/, ''),
+        irc_host: this.irc_host
+    };
+
+    // Let the webirc/etc detection modify any required parameters
+    connect_data = findWebIrc(connect_data);
+
+    // Send any initial data for webirc/etc
+    if (connect_data.prepend_data) {
+        _.each(connect_data.prepend_data, function(data) {
+            that.write(data);
+        });
     }
+
     if (this.password) {
         this.write('PASS ' + this.password);
     }
-    //this.write('CAP LS');
-    this.write('NICK ' + this.nick);
-    this.write('USER kiwi_' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick);
+    
+    this.write('NICK ' + connect_data.nick);
+    this.write('USER ' + connect_data.username + ' 0 0 :' + connect_data.realname);
     
     this.connected = true;
     console.log("IrcConnection.emit('connected')");
     this.emit('connected');
 };
 
-parse_regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
 
+
+
+function findWebIrc(connect_data) {
+    var webirc_pass = config.get().webirc_pass;
+    var ip_as_username = config.get().ip_as_username;
+    var tmp;
+
+    // Do we have a WEBIRC password for this?
+    if (webirc_pass && webirc_pass[connect_data.irc_host.hostname]) {
+        tmp = 'WEBIRC ' + webirc_pass[connect_data.irc_host.hostname] + ' KiwiIRC ';
+        tmp += connect_data.user.hostname + ' ' + connect_data.user.address;
+        connect_data.prepend_data = [tmp];
+    }
+
+
+    // Check if we need to pass the users IP as its username/ident
+    if (ip_as_username && ip_as_username.indexOf(connect_data.irc_host.hostname) > -1) {
+        // Get a hex value of the clients IP
+        connect_data.username = connect_data.user.address.split('.').map(function(i, idx){
+            return parseInt(i, 10).toString(16);
+        }).join('');
+
+    }
+
+    return connect_data;
+}
+
+
+
+parse_regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
 var parse = function (data) {
     var i,
         msg,
index 15fef4746a06b7e2c94fe533089e648f2e0a5eef..7b72a73ead465f47bc8b39d11297e7c6b9f899e6 100644 (file)
@@ -80,17 +80,26 @@ function handleHttpRequest(request, response) {
 }
 
 
+/**
+ * Get the reverse DNS entry for this connection.
+ * Used later on for webirc, etc functionality
+ */
 function authoriseConnection(handshakeData, callback) {
     dns.reverse(handshakeData.address.address, function (err, domains) {
-        handshakeData.revdns = (err) ? handshakeData.address.address : _.first(domains);
+        if (err || domains.length === 0) {
+            handshakeData.revdns = handshakeData.address.address;
+        } else {
+            handshakeData.revdns = _.first(domains);
+        }
+        
         callback(null, true);
     });
-};
+}
 
 function newConnection(websocket) {
     //console.log(websocket);
     this.emit('connection', new Client(websocket));
-};
+}