diff --git a/lib/api3/storageSocket.js b/lib/api3/storageSocket.js index 9171f834c44..d4ebb1254f3 100644 --- a/lib/api3/storageSocket.js +++ b/lib/api3/storageSocket.js @@ -3,6 +3,11 @@ const apiConst = require('./const'); const forwarded = require('forwarded-for'); +function getRemoteIP (req) { + const address = forwarded(req, req.headers); + return address.ip; +} + /** * Socket.IO broadcaster of any storage change */ @@ -29,8 +34,7 @@ function StorageSocket (app, env, ctx) { self.namespace = io.of(NAMESPACE); self.namespace.on('connection', function onConnected (socket) { - const address = forwarded(socket.request, socket.request.headers); - const remoteIP = address.ip; + const remoteIP = getRemoteIP(socket.request); console.log(LOG + 'Connection from client ID: ', socket.client.id, ' IP: ', remoteIP); socket.on('disconnect', function onDisconnect () { diff --git a/lib/server/websocket.js b/lib/server/websocket.js index 7bbdf13d575..08ae6d662c0 100644 --- a/lib/server/websocket.js +++ b/lib/server/websocket.js @@ -5,6 +5,11 @@ var calcData = require('../data/calcdelta'); var ObjectID = require('mongodb').ObjectID; const forwarded = require('forwarded-for'); +function getRemoteIP (req) { + const address = forwarded(req, req.headers); + return address.ip; +} + function init (env, ctx, server) { function websocket () { @@ -134,8 +139,7 @@ function init (env, ctx, server) { var timeDiff; var history; - const address = forwarded(socket.request, socket.request.headers); - const remoteIP = address.ip; + const remoteIP = getRemoteIP(socket.request); console.log(LOG_WS + 'Connection from client ID: ', socket.client.id, ' IP: ', remoteIP); io.emit('clients', ++watchers); @@ -531,7 +535,7 @@ function init (env, ctx, server) { // [, status : true ] // } socket.on('authorize', function authorize (message, callback) { - const remoteIP = socket.request.connection.remoteAddress; + const remoteIP = getRemoteIP(socket.request); verifyAuthorization(message, remoteIP, function verified (err, authorization) { if (err) {