From 9f6954dc1cca1651e5a7d96b2a6c21b442bce1a2 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Tue, 20 Sep 2011 17:56:43 +0100 Subject: [PATCH] Kiwi can now listen on multiple ports at the same time. Reverted previous change to index.html.jade --- node/app.js | 87 ++++++++++--------- node/client/index.html.jade | 165 ++++++++++++++++++------------------ node/config.json | 23 +++-- node/kiwi.js | 6 +- 4 files changed, 150 insertions(+), 131 deletions(-) diff --git a/node/app.js b/node/app.js index 633de39..94769a8 100644 --- a/node/app.js +++ b/node/app.js @@ -512,7 +512,8 @@ this.ircSocketDataHandler = function (data, websocket, ircSocket) { this.httpHandler = function (request, response) { var uri, uri_parts, subs, useragent, agent, server_set, server, nick, debug, touchscreen, hash, - min = {}, public_http_path; + min = {}, public_http_path, + secure = (typeof request.client.encrypted === 'object'); if (kiwi.config.handle_http) { uri = url.parse(request.url, true); uri_parts = uri.pathname.split('/'); @@ -588,13 +589,13 @@ this.httpHandler = function (request, response) { response.setHeader('X-Generated-By', 'KiwiIRC'); hash = crypto.createHash('md5').update(touchscreen ? 't' : 'f') - .update(debug ? 't' : 'f') - .update(server_set ? 't' : 'f') - .update(server) - .update(nick) - .update(agent) - .update(JSON.stringify(kiwi.config)) - .digest('base64'); + .update(debug ? 't' : 'f') + .update(server_set ? 't' : 'f') + .update(server) + .update(nick) + .update(agent) + .update(JSON.stringify(kiwi.config)) + .digest('base64'); if (kiwi.cache.html[hash]) { if (request.headers['if-none-match'] === kiwi.cache.html[hash].hash) { response.statusCode = 304; @@ -607,7 +608,7 @@ this.httpHandler = function (request, response) { fs.readFile(__dirname + '/client/index.html.jade', 'utf8', function (err, str) { var html, hash2; if (!err) { - html = kiwi.jade.compile(str)({ "touchscreen": touchscreen, "debug": debug, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": kiwi.config }); + html = kiwi.jade.compile(str)({ "touchscreen": touchscreen, "debug": debug, "secure": secure, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": kiwi.config }); hash2 = crypto.createHash('md5').update(html).digest('base64'); kiwi.cache.html[hash] = {"html": html, "hash": hash2}; if (request.headers['if-none-match'] === hash2) { @@ -634,33 +635,42 @@ this.httpHandler = function (request, response) { -this.websocketListen = function (port, host, handler, secure, key, cert) { - if (kiwi.httpServer) { - kiwi.httpServer.close(); +this.websocketListen = function (ports, host, handler, key, cert) { + if (kiwi.httpServers.length > 0) { + _.each(kiwi.httpServers, function (hs) { + hs.close(); + }); + kiwi.httpsServers = []; } - if (secure) { - kiwi.httpServer = https.createServer({key: fs.readFileSync(__dirname + '/' + key), cert: fs.readFileSync(__dirname + '/' + cert)}, handler); - kiwi.io = ws.listen(kiwi.httpServer, {secure: true}); - kiwi.httpServer.listen(port, host); - } else { - kiwi.httpServer = http.createServer(handler); - kiwi.io = ws.listen(kiwi.httpServer, {secure: false}); - kiwi.httpServer.listen(port, host); - } - - kiwi.io.set('log level', 1); - kiwi.io.enable('browser client minification'); - kiwi.io.enable('browser client etag'); - kiwi.io.set('transports', kiwi.config.transports); - - kiwi.io.of('/kiwi').authorization(function (handshakeData, callback) { - var address = handshakeData.address.address; - - if (typeof kiwi.connections[address] === 'undefined') { - kiwi.connections[address] = {count: 0, sockets: []}; + _.each(ports, function (port) { + var hs; + if (port.secure === true) { + hs = https.createServer({key: fs.readFileSync(__dirname + '/' + key), cert: fs.readFileSync(__dirname + '/' + cert)}, handler); + kiwi.io.push(ws.listen(hs, {secure: true})); + hs.listen(port.number); + console.log("Listening on %s, port %d with SSL", host, port.number); + } else { + hs = http.createServer(handler); + kiwi.io.push(ws.listen(hs, {secure: false})); + hs.listen(port.number); + console.log("Listening on %s, port %d without SSL", host, port.number); } - callback(null, true); - }).on('connection', kiwi.websocketConnection); + }); + + _.each(kiwi.io, function (io) { + io.set('log level', 1); + io.enable('browser client minification'); + io.enable('browser client etag'); + io.set('transports', kiwi.config.transports); + + io.of('/kiwi').authorization(function (handshakeData, callback) { + var address = handshakeData.address.address; + if (typeof kiwi.connections[address] === 'undefined') { + kiwi.connections[address] = {count: 0, sockets: []}; + } + callback(null, true); + }).on('connection', kiwi.websocketConnection); + }); }; @@ -670,6 +680,7 @@ this.websocketListen = function (port, host, handler, secure, key, cert) { this.websocketConnection = function (websocket) { var con; + console.log("New connection!"); websocket.kiwi = {address: websocket.handshake.address.address, buffer: {list: []}}; con = kiwi.connections[websocket.kiwi.address]; @@ -856,15 +867,13 @@ this.rehash = function () { console.log('%s config changes: \n', Object.keys(changes).length, changes); for (i in changes) { switch (i) { - case 'port': + case 'ports': case 'bind_address': - case 'listen_ssl': case 'ssl_key': case 'ssl_cert': - kiwi.websocketListen(kiwi.config.port, kiwi.config.bind_address, kiwi.httpHandler, kiwi.config.listen_ssl, kiwi.config.ssl_key, kiwi.config.ssl_cert); - delete changes.port; + kiwi.websocketListen(kiwi.config.ports, kiwi.config.bind_address, kiwi.httpHandler, kiwi.config.ssl_key, kiwi.config.ssl_cert); + delete changes.ports; delete changes.bind_address; - delete changes.listen_ssl; delete changes.ssl_key; delete changes.ssl_cert; break; diff --git a/node/client/index.html.jade b/node/client/index.html.jade index f671850..0ab1d6c 100644 --- a/node/client/index.html.jade +++ b/node/client/index.html.jade @@ -14,84 +14,6 @@ html(xmlns="http://www.w3.org/1999/xhtml", lang="en-gb") - if (touchscreen) link(rel="stylesheet", type="text/css", href="css/touchscreen_tweaks.css") - body - - div#kiwi - div.connectwindow - h1.logo Kiwi IRC - div#login - form.formconnectwindow - div.content.top - ul - li - label(for="nick") Your nickname: - input(type="text", id="nick", name="nick", class="nick", placeholder="Your nick..", value=nick) - a.connect(href="") Connect... - - - var display - - if (server_set) - - display = 'display:none' - - else - - display = ''; - div.more(style=display) - a(href="", class="more_link") more - div.content.bottom - ul - li - label(for="network") Server: - input(type="text", id="network", name="network", class="network", value=server) - li - label(for="channel") Channel: - input(type="text", id="channel", name="channel", class="channel", value="#kiwiirc") - li(class="section") - label(for="port") Port: - input(type="text", id="port", name="port", class="port", value="6667") - li - label(for="ssl") SSL: - input(type="checkbox", id="ssl", name="ssl", class="ssl") - a.connect(href="") Connect... - - - - div.toolbars - div.windowlist - div.poweredby Powered by Kiwi IRC - ul - - div.utilityviewlist - ul - - div.cur_topic(contenteditable="true", spellcheck="true") - // - ul.edit(style="float:right;") - li - img(src="img/more.png") - ul#kiwi_menu - li Item 1 - li Item 2 - li Item 3 - div.topic(style="margin-right:5em; overflow:hidden; white-space: pre-wrap; word-wrap: break-word;") - - div.userlist - ul - - div#windows.windows - div.scroller(style="width:100%;height:100%;") - - div.control - div.msginput - div.nick - a - | : - input(type="text", name="kiwi_msginput", id="kiwi_msginput") - div.plugins - ul - li - a.load_plugin_file Plugins - - if (debug) - li - a.reload_css Reload CSS - script(type="text/javascript", src="/socket.io/socket.io.js") script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js") script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js") @@ -110,9 +32,9 @@ html(xmlns="http://www.w3.org/1999/xhtml", lang="en-gb") - if (touchscreen) script(type="text/javascript", src="/js/touchscreen_tweaks.js") - + - var proto - - if (config.listen_ssl) + - if (secure) - proto = 'https' - else - proto = 'http' @@ -194,4 +116,85 @@ html(xmlns="http://www.w3.org/1999/xhtml", lang="en-gb") | | Message | Info - | \ No newline at end of file + | + + body + + div#kiwi + div.connectwindow + h1.logo Kiwi IRC + div#login + form.formconnectwindow + div.content.top + ul + li + label(for="nick") Your nickname: + input(type="text", id="nick", name="nick", class="nick", placeholder="Your nick..", value=nick) + a.connect(href="") Connect... + + - var display + - if (server_set) + - display = 'display:none' + - else + - display = ''; + div.more(style=display) + a(href="", class="more_link") more + div.content.bottom + ul + li + label(for="network") Server: + input(type="text", id="network", name="network", class="network", value=server) + li + label(for="channel") Channel: + input(type="text", id="channel", name="channel", class="channel", value="#kiwiirc") + li(class="section") + label(for="port") Port: + input(type="text", id="port", name="port", class="port", value="6667") + li + label(for="ssl") SSL: + input(type="checkbox", id="ssl", name="ssl", class="ssl") + a.connect(href="") Connect... + + + + div.toolbars + div.windowlist + div.poweredby Powered by Kiwi IRC + ul + + div.utilityviewlist + ul + + div.cur_topic(contenteditable="true", spellcheck="true") + // + ul.edit(style="float:right;") + li + img(src="img/more.png") + ul#kiwi_menu + li Item 1 + li Item 2 + li Item 3 + div.topic(style="margin-right:5em; overflow:hidden; white-space: pre-wrap; word-wrap: break-word;") + + div.userlist + ul + + div#windows.windows + div#panel1.panel + div.scroller(style="width:100%;height:100%;") + + div#panel2.panel + + div.control + div.msginput + div.nick + a + | : + input(type="text", name="kiwi_msginput", id="kiwi_msginput") + div.plugins + ul + li + a.load_plugin_file Plugins + - if (debug) + li + a.reload_css Reload CSS diff --git a/node/config.json b/node/config.json index 132c835..f59d364 100644 --- a/node/config.json +++ b/node/config.json @@ -1,11 +1,20 @@ { - "port": 7777, + "ports": [ + { + "secure": true, + "number": 7777 + }, + { + "secure": false, + "number": 7778 + } + + ], "bind_address": "0.0.0.0", - "user": "", - "group": "", + "user": "", + "group": "", - "listen_ssl": false, "ssl_key": "server.key", "ssl_cert": "cert.pem", @@ -13,7 +22,7 @@ "cap_options": [], "handle_http": true, - "public_http": "./../", + "public_http": "./../", "max_client_conns": 2, @@ -27,9 +36,7 @@ }, "transports": [ - "websocket", - "flashsocket", - "htmlfile", + "xhr-polling", "jsonp-polling" ] diff --git a/node/kiwi.js b/node/kiwi.js index e58607c..b6f04db 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -122,7 +122,7 @@ if (this.config.handle_http) { this.jade = require('jade'); this.cache = {alljs: '', html: []}; } -this.httpServer = null; +this.httpServers = []; this.httpHandler = function (request, response) { return app.httpHandler(request, response); } @@ -136,7 +136,7 @@ this.httpHandler = function (request, response) { * Websocket handling */ this.connections = {}; -this.io = null; +this.io = []; this.websocketListen = function (port, host, handler, secure, key, cert) { return app.websocketListen(port, host, handler, secure, key, cert); } @@ -199,7 +199,7 @@ this.kiwi_mod.printMods(); // Start the server up -this.websocketListen(this.config.port, this.config.bind_address, this.httpHandler, this.config.listen_ssl, this.config.ssl_key, this.config.ssl_cert); +this.websocketListen(this.config.ports, this.config.bind_address, this.httpHandler, this.config.ssl_key, this.config.ssl_cert); // Now we're listening on the network, set our UID/GIDs if required app.changeUser(); -- 2.25.1