From 15ab9e0e1b06d2025528ae5aaedf411d908e0413 Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 4 Jun 2014 15:39:26 +0100 Subject: [PATCH] Work around for engine.io forgetting remoteAddress at times --- server/weblistener.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/server/weblistener.js b/server/weblistener.js index 7727ca4..02bdca3 100644 --- a/server/weblistener.js +++ b/server/weblistener.js @@ -53,7 +53,7 @@ var WebListener = module.exports = function (web_config) { } } - hs = spdy.createServer(opts, handleHttpRequest); + hs = spdy.createServer(opts); hs.listen(web_config.port, web_config.address, function () { that.emit('listening'); @@ -61,7 +61,7 @@ var WebListener = module.exports = function (web_config) { } else { // Start some plain-text server up - hs = http.createServer(handleHttpRequest); + hs = http.createServer(); hs.listen(web_config.port, web_config.address, function () { that.emit('listening'); @@ -72,8 +72,28 @@ var WebListener = module.exports = function (web_config) { that.emit('error', err); }); - this.ws = engine.attach(hs, { - path: (global.config.http_base_path || '') + '/transport' + this.ws = new engine.Server(); + + hs.on('upgrade', function(req, socket, head){ + that.ws.handleUpgrade(req, socket, head); + }); + + hs.on('request', function(req, res){ + var transport_url = (global.config.http_base_path || '') + '/transport'; + + // engine.io can sometimes "loose" the clients remote address. Keep note of it + req.meta = { + remote_address: req.connection.remoteAddress + }; + + // If the request is for our transport, pass it onto engine.io + if (req.url.toLowerCase().indexOf(transport_url.toLowerCase()) === 0) { + that.ws.handleRequest(req, res); + } else { + http_handler.serve(req, res); + } + + }); this.ws.on('connection', function(socket) { @@ -101,10 +121,6 @@ util.inherits(WebListener, events.EventEmitter); -function handleHttpRequest(request, response) { - http_handler.serve(request, response); -} - function rangeCheck(addr, range) { var i, ranges, parts; ranges = (!_.isArray(range)) ? [range] : range; @@ -124,12 +140,12 @@ function rangeCheck(addr, range) { */ function initialiseSocket(socket, callback) { var request = socket.request, - address = request.connection.remoteAddress, + address = request.meta.remote_address, revdns; // Key/val data stored to the socket to be read later on // May also be synced to a redis DB to lookup clients - socket.meta = {}; + socket.meta = socket.request.meta; // If a forwarded-for header is found, switch the source address if (request.headers[global.config.http_proxy_ip_header || 'x-forwarded-for']) { -- 2.25.1