-var ws = require('socket.io'),
- events = require('events'),
- http = require('http'),
- https = require('https'),
- util = require('util'),
- fs = require('fs'),
- dns = require('dns'),
- url = require('url'),
- _ = require('lodash'),
- spdy = require('spdy'),
- ipaddr = require('ipaddr.js'),
- Client = require('./client.js').Client,
- HttpHandler = require('./httphandler.js').HttpHandler,
- rehash = require('./rehash.js');
+var engine = require('engine.io'),
+ WebsocketRpc = require('./websocketrpc.js');
+ events = require('events'),
+ http = require('http'),
+ https = require('https'),
+ util = require('util'),
+ fs = require('fs'),
+ dns = require('dns'),
+ url = require('url'),
+ _ = require('lodash'),
+ spdy = require('spdy'),
+ ipaddr = require('ipaddr.js'),
+ Client = require('./client.js').Client,
+ HttpHandler = require('./httphandler.js').HttpHandler,
+ rehash = require('./rehash.js');
hs = spdy.createServer(opts, handleHttpRequest);
- // Start socket.io listening on this weblistener
- this.ws = ws.listen(hs, _.extend({ssl: true}, ws_opts));
hs.listen(web_config.port, web_config.address, function () {
that.emit('listening');
});
// Start some plain-text server up
hs = http.createServer(handleHttpRequest);
- // Start socket.io listening on this weblistener
- this.ws = ws.listen(hs, _.extend({ssl: false}, ws_opts));
hs.listen(web_config.port, web_config.address, function () {
that.emit('listening');
});
that.emit('error', err);
});
- this.ws.enable('browser client minification');
- this.ws.enable('browser client etag');
- this.ws.set('transports', transports);
- this.ws.set('resource', (global.config.http_base_path || '') + '/transport');
+ this.ws = engine.attach(hs, {
+ transports: ['websocket', 'polling', 'flashsocket'],
+ path: (global.config.http_base_path || '') + '/transport'
+ });
+ console.log((global.config.http_base_path || '') + '/transport');
- this.ws.of('/kiwi').authorization(authoriseConnection)
- .on('connection', function () {
- newConnection.apply(that, arguments);
- }
- );
- this.ws.of('/kiwi').on('error', console.log);
+ //this.ws.enable('browser client minification');
+ //this.ws.enable('browser client etag');
+ //this.ws.set('transports', transports);
+ //this.ws.set('resource', (global.config.http_base_path || '') + '/transport');
+
+ this.ws.on('connection', function(socket) {
+ console.log('Connection!');
+ initialiseSocket(socket, function(err, authorised) {
+ var client;
+
+ if (!authorised) {
+ socket.close();
+ return;
+ }
+
+ client = new Client(socket);
+ client.on('dispose', function () {
+ that.emit('client_dispose', this);
+ });
+
+ that.emit('connection', client);
+ });
+ });
};
util.inherits(WebListener, events.EventEmitter);
* Get the reverse DNS entry for this connection.
* Used later on for webirc, etc functionality
*/
-function authoriseConnection(handshakeData, callback) {
- var address = handshakeData.address.address;
+function initialiseSocket(socket, callback) {
+ var request = socket.request,
+ address = request.connection.remoteAddress;
+
+ // Key/val data stored to the socket to be read later on
+ // May also be synced to a redis DB to lookup clients
+ socket.kiwi = {};
// If a forwarded-for header is found, switch the source address
- if (handshakeData.headers[global.config.http_proxy_ip_header || 'x-forwarded-for']) {
+ if (request.headers[global.config.http_proxy_ip_header || 'x-forwarded-for']) {
// Check we're connecting from a whitelisted proxy
if (!global.config.http_proxies || !rangeCheck(address, global.config.http_proxies)) {
console.log('Unlisted proxy:', address);
}
// We're sent from a whitelisted proxy, replace the hosts
- address = handshakeData.headers['x-forwarded-for'];
+ address = request.headers[global.config.http_proxy_ip_header || 'x-forwarded-for'];
}
- handshakeData.real_address = address;
+ socket.kiwi.real_address = address;
// If enabled, don't go over the connection limit
if (global.config.max_client_conns && global.config.max_client_conns > 0) {
try {
dns.reverse(address, function (err, domains) {
if (err || domains.length === 0) {
- handshakeData.revdns = address;
+ socket.kiwi.revdns = address;
} else {
- handshakeData.revdns = _.first(domains) || address;
+ socket.kiwi.revdns = _.first(domains) || address;
}
// All is well, authorise the connection
callback(null, true);
});
} catch (err) {
- handshakeData.revdns = address;
+ socket.kiwi.revdns = address;
callback(null, true);
}
}
-function newConnection(websocket) {
- var client, that = this;
- client = new Client(websocket);
- client.on('dispose', function () {
- that.emit('client_dispose', this);
- });
- this.emit('connection', client);
-}
-