From b16b06017ec542aa5e79a2638e437784b3928f72 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Sun, 31 Jul 2011 06:33:32 +0100 Subject: [PATCH] move too many connections error from authentication to connection handling --- js/gateway.js | 5 +- node/kiwi.js | 174 +++++++++++++++++++++++++------------------------- 2 files changed, 91 insertions(+), 88 deletions(-) diff --git a/js/gateway.js b/js/gateway.js index cdeac1b..9d406c5 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -24,7 +24,7 @@ var gateway = { gateway.socket = io.connect(kiwi_server, {'max reconnection attempts': 3}); gateway.socket.of('/kiwi').on('connect_failed', function (reason) { console.debug('Unable to connect Socket.IO', reason); - front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\nYour IP address has too many open connections to Kiwi', 'error'); + front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error'); gateway.socket.disconnect(); $(gateway).trigger("ondisconnect", {}); gateway.sendData = function () {}; @@ -42,6 +42,9 @@ var gateway = { }); gateway.socket.emit('irc connect', gateway.nick, host, port, ssl, callback); }); + gateway.socket.on('too_many_connections', function () { + front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\nYour IP address has too many connections to Kiwi IRC', 'error'); + }); } }, diff --git a/node/kiwi.js b/node/kiwi.js index c5f4ce3..9cd38c1 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -478,104 +478,104 @@ io.of('/kiwi').authorization(function (handshakeData, callback) { if (typeof connections[address] === 'undefined') { connections[address] = {count: 0, sockets: []}; } - connection = connections[address]; - if (connection.count >= config.max_client_conns) { - callback(null, false); - return; - } - connection.count += 1; callback(null, true); }).on('connection', function (websocket) { var con; websocket.kiwi = {address: websocket.handshake.address.address}; con = connections[websocket.kiwi.address]; - con.sockets.push(websocket); - websocket.on('irc connect', function (nick, host, port, ssl, callback) { - var ircSocket; - //setup IRC connection - if (!ssl) { - ircSocket = net.createConnection(port, host); - } else { - ircSocket = tls.connect(port, host); - } - ircSocket.setEncoding('ascii'); - ircSocket.IRC = {options: {}, CAP: {negotiating: true, requested: [], enabled: []}}; - websocket.ircSocket = ircSocket; - ircSocket.holdLast = false; - ircSocket.held = ''; - - ircSocket.on('data', function (data) { - ircSocketDataHandler(data, websocket, ircSocket); + if (con.count >= config.max_client_conns) { + websocket.emit('too_many_connections'); + websocket.disconnect(); + } else { + con.count += 1 + con.sockets.push(websocket); + websocket.on('irc connect', function (nick, host, port, ssl, callback) { + var ircSocket; + //setup IRC connection + if (!ssl) { + ircSocket = net.createConnection(port, host); + } else { + ircSocket = tls.connect(port, host); + } + ircSocket.setEncoding('ascii'); + ircSocket.IRC = {options: {}, CAP: {negotiating: true, requested: [], enabled: []}}; + websocket.ircSocket = ircSocket; + ircSocket.holdLast = false; + ircSocket.held = ''; + + ircSocket.on('data', function (data) { + ircSocketDataHandler(data, websocket, ircSocket); + }); + + ircSocket.IRC.nick = nick; + // Send the login data + ircSocket.write('CAP LS\r\n'); + ircSocket.write('NICK ' + nick + '\r\n'); + ircSocket.write('USER ' + nick + '_kiwi 0 0 :' + nick + '\r\n'); + + if ((callback) && (typeof (callback) === 'function')) { + callback(); + } }); - - ircSocket.IRC.nick = nick; - // Send the login data - ircSocket.write('CAP LS\r\n'); - ircSocket.write('NICK ' + nick + '\r\n'); - ircSocket.write('USER ' + nick + '_kiwi 0 0 :' + nick + '\r\n'); - - if ((callback) && (typeof (callback) === 'function')) { - callback(); - } - }); - websocket.on('message', function (msg, callback) { - var args; - try { - msg.data = JSON.parse(msg.data); - args = msg.data.args; - switch (msg.data.method) { - case 'msg': - if ((args.target) && (args.msg)) { - websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n'); - } - break; - case 'action': - if ((args.target) && (args.msg)) { - websocket.ircSocket.write('PRIVMSG ' + args.target + ' :ACTION ' + args.msg + '\r\n'); + websocket.on('message', function (msg, callback) { + var args; + try { + msg.data = JSON.parse(msg.data); + args = msg.data.args; + switch (msg.data.method) { + case 'msg': + if ((args.target) && (args.msg)) { + websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n'); + } + break; + case 'action': + if ((args.target) && (args.msg)) { + websocket.ircSocket.write('PRIVMSG ' + args.target + ' :ACTION ' + args.msg + '\r\n'); + } + break; + case 'raw': + websocket.ircSocket.write(args.data + '\r\n'); + break; + case 'join': + if (args.channel) { + _.each(args.channel.split(","), function (chan) { + websocket.ircSocket.write('JOIN ' + chan + '\r\n'); + }); + } + break; + case 'quit': + websocket.ircSocket.end('QUIT :' + args.message + '\r\n'); + websocket.sentQUIT = true; + websocket.ircSocket.destroySoon(); + websocket.disconnect(); + break; + case 'notice': + if ((args.target) && (args.msg)) { + websocket.ircSocket.write('NOTICE ' + args.target + ' :' + args.msg + '\r\n'); + } + break; + default: } - break; - case 'raw': - websocket.ircSocket.write(args.data + '\r\n'); - break; - case 'join': - if (args.channel) { - _.each(args.channel.split(","), function (chan) { - websocket.ircSocket.write('JOIN ' + chan + '\r\n'); - }); + if ((callback) && (typeof (callback) === 'function')) { + callback(); } - break; - case 'quit': - websocket.ircSocket.end('QUIT :' + args.message + '\r\n'); + } catch (e) { + console.log("Caught error: " + e); + } + }); + websocket.on('disconnect', function () { + var con; + if ((!websocket.sentQUIT) && (websocket.ircSocket)) { + websocket.ircSocket.end('QUIT :' + config.quit_message + '\r\n'); websocket.sentQUIT = true; websocket.ircSocket.destroySoon(); - websocket.disconnect(); - break; - case 'notice': - if ((args.target) && (args.msg)) { - websocket.ircSocket.write('NOTICE ' + args.target + ' :' + args.msg + '\r\n'); - } - break; - default: - } - if ((callback) && (typeof (callback) === 'function')) { - callback(); } - } catch (e) { - console.log("Caught error: " + e); - } - }); - websocket.on('disconnect', function () { - var con; - if ((!websocket.sentQUIT) && (websocket.ircSocket)) { - websocket.ircSocket.end('QUIT :' + config.quit_message + '\r\n'); - websocket.sentQUIT = true; - websocket.ircSocket.destroySoon(); - } - con = connections[websocket.kiwi.address]; - con.count -=1; - con.sockets = _.reject(con.sockets, function (sock) { - return sock === websocket; + con = connections[websocket.kiwi.address]; + con.count -=1; + con.sockets = _.reject(con.sockets, function (sock) { + return sock === websocket; + }); }); - }); + } }); -- 2.25.1