user15317824
user15317824

Reputation: 470

Return ip address of site visitors instead of Heroku server

So am trying to get the IP address of the visitors to my app on Heroku, am using an API for this, the API works well locally but when I deploy the app on Heroku, I get the IP of the Heroku server, it is an express.js app and is using this setting

app.set('trust proxy', true);

I have seen a lot of solutions to this problem but none of the solutions works for me.

when i use

var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

i get this result

clientIp: '::1',

Upvotes: 2

Views: 867

Answers (3)

FAYEMI BOLUWATIFE
FAYEMI BOLUWATIFE

Reputation: 118

I will assume the Op is using this API - http://geo.ipify.org/api/v2/country

The API is actually working well both locally and in production (on Heroku). The real issue or the reason why the Heroku server's IP address is showing instead of the visitor's IP address is that you are now calling the API from the Heroku server as mentioned by @SuperStormer.

As the Op pointed out, there are different ways to obtain a user's Ip address in express.js.

I ran a check on using these three both locally and on Heroku

  • req.ip
  • req.headers.["x-forwarded-for"]
  • req.connection.remoteAddress

Here is what it looks like on Heroku logs heroku logs of IP address

PS: Trust proxy has been set to true which is why req.ip has the same value as req.headers.["x-forwarded-for"].

The IP address returned by req.connection.remoteAddress is an IPv4 address expressed in IPv6 notation and since it does not conform with the previous values it attests to the fact that there exists a proxy between the client and the server.

When you are on localhost: the IP address usually returned from accessing the req object is ::1.

::1 is actually an IP Address and is IPV6 notation for localhost. The loopback address in IPv4 is 127.0.01. In IPv6, the loopback address is 0:0:0:0:0:0:0:1 or ::1.

You can read this how-to-guide for more information on the different ways to obtain IP address.

Upvotes: 0

Allaye
Allaye

Reputation: 950

First you will have to install this module npm i ipware

var get_ip = require('ipware')().get_ip;


app.use(function(req, res, next) {
    var ip_info = get_ip(req);
    console.log(ip_info);
    // { clientIp: '127.0.0.1', clientIpRoutable: false }
    next();
});

Mind you the result is in an object form, getting the ipaddress should be simple as doing

ip_info.clientIp

So i guess this should help.

Upvotes: 2

yahya007
yahya007

Reputation: 168

var ip = req.headers['x-forwarded-for'] || 
     req.connection.remoteAddress || 
     req.socket.remoteAddress ||
     (req.connection.socket ? req.connection.socket.remoteAddress : null);

But sometimes you can get more than one IP address with this req.headers['x-forwarded-for']

      var ipAddr = req.headers["x-forwarded-for"];
      if (ipAddr){
        var list = ipAddr.split(",");
        ipAddr = list[list.length-1];
      } else {
        ipAddr = req.connection.remoteAddress;
      }

You control with basic if else.

Or you can use this one and replace with req.headers['x-forwarded-for'] on top of the answer

let ip = request.headers['x-forwarded-for'].split(',')[0]

Upvotes: 1

Related Questions