Skip to content

Commit

Permalink
Forwarded fix (nightscout#7593)
Browse files Browse the repository at this point in the history
* Use 'forwarded' always

Getting the remote IP from the socket doesn't work behind a reverse proxy. 'Forwarded' is there to fix there, but wasn't used here.

* Consistently use getRemoteIP() throughout

Stay in line with other modules that do this

* Use getRemoteIP consistently

One more missing case; in this file it isn't helpful, but it is consistent with other files.

Co-authored-by: Sulka Haro <sulka@sulka.net>
  • Loading branch information
dnicolaas and sulkaharo committed Jan 1, 2023
1 parent ba76940 commit 2dfc471
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/api3/storageSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 () {
Expand Down
10 changes: 7 additions & 3 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2dfc471

Please sign in to comment.