Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the real client ip for a request when tunneling with ngrok

Tags:

ngrok

How can I make sure that the client IP address is forwarded by ngrok?

My test code keeps insisting that all the requests are coming from 127.0.0.1 because of ngrok but i want to log the actual client IP instead. Load balancers usually set a header in X-Forwarded-For or x-real-ip but I'm not sure what the process for ngrok is ...

  console.log('req.headers[\'x-real-ip\']', req.headers['x-real-ip']);
  console.log('req.headers[\'X-Forwarded-For\']', req.headers['X-Forwarded-For']);
  console.log('req.ip', req.ip );
  console.log('req.connection.remoteAddress', req.connection.remoteAddress);
  console.log('req.connection.remoteAddress', req.connection.remoteAddress);
  console.log('req.socket.remoteAddress', (req.socket && req.socket.remoteAddress));
  console.log('req.socket.socket.remoteAddress', (req.socket.socket && req.socket.socket.remoteAddress));

Everything either prints undefined or 127.0.0.1 so far. Which means I need to configure ngrok somehow, I think.

like image 216
pulkitsinghal Avatar asked Jun 07 '16 00:06

pulkitsinghal


1 Answers

Unfortunately, all header names in Node.JS are lowercase. Use

req.headers['x-forwarded-for']

instead of

req.headers['X-Forwarded-For']
like image 93
Dima Karaush Avatar answered Oct 05 '22 03:10

Dima Karaush