From 186531edd1358292aace8a7cb1a400ef8f3470a0 Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 21 Oct 2012 14:41:01 +0100 Subject: [PATCH] kiwi.js, WebListener and HttpHandler refactor. Remove socket.io works from HttpHandler --- server/http-handler.js | 31 +++++------------ server/kiwi.js | 55 ++++++++++++++++++++++++++----- server/{web.js => weblistener.js} | 47 +++++++++++++++++++------- 3 files changed, 91 insertions(+), 42 deletions(-) rename server/{web.js => weblistener.js} (74%) mode change 100755 => 100644 diff --git a/server/http-handler.js b/server/http-handler.js index 0393565..83d7d35 100755 --- a/server/http-handler.js +++ b/server/http-handler.js @@ -1,40 +1,27 @@ var url = require('url'), node_static = require ('node-static'); -var HTTPHandler = function (config) { - this.static_file_server = new StaticFileServer(config.public_html); -}; -module.exports.HTTPHandler = HTTPHandler; -var StaticFileServer = function (public_html) { - public_html = public_html || 'client_backbone/'; - this.fileServer = new node_static.Server(public_html); +var HttpHandler = function (config) { + var public_html = config.public_html || 'client/'; + this.file_server = new node_static.Server(public_html); }; -StaticFileServer.prototype.serve = function (request, response) { +module.exports.HttpHandler = HttpHandler; + + + +HttpHandler.prototype.serve = function (request, response) { // Any requests for /client to load the index file if (request.url.match(/^\/client/)) { request.url = '/'; } - this.fileServer.serve(request, response, function (err) { + this.file_server.serve(request, response, function (err) { if (err) { response.writeHead(err.status, err.headers); response.end(); } }); -}; - -HTTPHandler.prototype.handler = function (request, response) { - var uri, subs; - - uri = url.parse(request.url, true); - subs = uri.pathname.substr(0, 4); - - if (uri.pathname.substr(0, 10) === '/socket.io') { - return; - } else { - this.static_file_server.serve(request, response); - } }; \ No newline at end of file diff --git a/server/kiwi.js b/server/kiwi.js index f788a07..5bbf565 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -1,8 +1,12 @@ var fs = require('fs'), _ = require('underscore'), - WebListener = require('./web.js').WebListener; + WebListener = require('./weblistener.js'); -//load config + + +/* + * Config loading + */ var config_filename = 'config.json', config_dirs = ['/etc/kiwiirc/', __dirname + '/']; @@ -27,6 +31,7 @@ for (var i in config_dirs) { } } +// Make sure we have a valid config file and at least 1 server if (Object.keys(config).length === 0) { console.log('Couldn\'t find a valid config file!'); process.exit(1); @@ -37,9 +42,20 @@ if ((!config.servers) || (config.servers.length < 1)) { process.exit(2); } -//Create web listeners + + + +/* + * Web listeners + */ + +// Holder for all the connected clients +// TODO: Change from an array to an object. Generate sha1 hash within the client +// and use that as the key. (Much less work involved in removing a client) var clients = []; + +// Start up a weblistener for each found in the config _.each(config.servers, function (server) { var wl = new WebListener(server, config.transports); wl.on('connection', function (client) { @@ -53,10 +69,17 @@ _.each(config.servers, function (server) { }); -//Set process title -process.title = 'Kiwi IRC'; -//Change UID/GID + + +/* + * Process settings + */ + +// Set process title +process.title = 'kiwiirc'; + +// Change UID/GID if ((config.user) && (config.user !== '')) { process.setuid(config.user); } @@ -64,8 +87,22 @@ if ((config.group) && (config.group !== '')) { process.setgid(config.group); } -//Listen to STDIN + + +/* + * Listen for runtime commands + */ + process.stdin.resume(); -process.stdin.on('data', function (data) { - console.log(data.toString()); +process.stdin.on('data', function (buffered) { + var data = buffered.toString().trim(); + + switch (data) { + case 'stats': + console.log('Connected clients: ' + _.size(clients).toString()); + break; + + default: + console.log('Unrecognised command: ' + data); + } }); diff --git a/server/web.js b/server/weblistener.js old mode 100755 new mode 100644 similarity index 74% rename from server/web.js rename to server/weblistener.js index 3f14dae..e35a112 --- a/server/web.js +++ b/server/weblistener.js @@ -5,43 +5,52 @@ var ws = require('socket.io'), util = require('util'), fs = require('fs'), dns = require('dns'), + url = require('url'), _ = require('underscore'), Client = require('./client.js').Client, - HTTPHandler = require('./http-handler.js').HTTPHandler; + HttpHandler = require('./http-handler.js').HttpHandler; + +// Instance of HttpHandler +var http_handler; + var WebListener = function (config, transports) { - var handler, - hs, + var hs, opts, that = this; events.EventEmitter.call(this); - http_handler = new HTTPHandler(config); + http_handler = new HttpHandler(config); if (config.secure) { opts = { key: fs.readFileSync(__dirname + '/' + config.ssl_key), cert: fs.readFileSync(__dirname + '/' + config.ssl_cert) }; + // Do we have an intermediate certificate? if (typeof config.ssl_ca !== 'undefined') { opts.ca = fs.readFileSync(__dirname + '/' + config.ssl_ca); } - hs = https.createServer(opts, function (request, response) { - http_handler.handler(request, response); - }); + + + hs = https.createServer(opts, handleHttpRequest); + // Start socket.io listening on this weblistener this.ws = ws.listen(hs, {secure: true}); hs.listen(config.port, config.address); + console.log('Listening on ' + config.address + ':' + config.port.toString() + ' with SSL'); } else { + // Start some plain-text server up - hs = http.createServer(function (request, response) { - http_handler.handler(request, response); - }); + hs = http.createServer(handleHttpRequest); + + // Start socket.io listening on this weblistener this.ws = ws.listen(hs, {secure: false}); hs.listen(config.port, config.address); + console.log('Listening on ' + config.address + ':' + config.port.toString() + ' without SSL'); } @@ -57,7 +66,17 @@ var WebListener = function (config, transports) { }; util.inherits(WebListener, events.EventEmitter); -module.exports.WebListener = WebListener; + + +function handleHttpRequest(request, response) { + var uri = url.parse(request.url, true); + + // If this isn't a socket.io request, pass it onto the http handler + if (uri.pathname.substr(0, 10) !== '/socket.io') { + http_handler.serve(request, response); + } +} + var authorisation = function (handshakeData, callback) { dns.reverse(handshakeData.address.address, function (err, domains) { @@ -70,3 +89,9 @@ var connection = function (websocket) { //console.log(websocket); this.emit('connection', new Client(websocket)); }; + + + + + +module.exports = WebListener; \ No newline at end of file -- 2.25.1