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
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 + '/'];
}
}
+// 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);
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) {
});
-//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);
}
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);
+ }
});
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');
}
};
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) {
//console.log(websocket);
this.emit('connection', new Client(websocket));
};
+
+
+
+
+
+module.exports = WebListener;
\ No newline at end of file