Express.js req.ip returns :: ffff: 127.0.0.1

I am currently trying to get the IP address of the requested user. The problem is that IP returns ::ffff:127.0.0.1 instead of 127.0.0.1 . I tried using the trusted proxy option (although not using a proxy), and req.ips empty. Using 4.x Express.js.

 router.get('/', function(req, res, next) { console.log('ip', req.ip) res.send({}) }); 
+72
express
Apr 02 '15 at 11:14
source share
8 answers

::ffff: This is the subnet prefix for IPv4 (32-bit) addresses that fit into IPv6 (128-bit) space. IPv6 is divided into two parts: subnet prefix and interface suffix. Each of them has a length of 64 bits or 4 groups of 4 hexadecimal characters.

In IPv6, you are allowed to remove leading zeros and then remove consecutive zeros, which means that ::ffff: actually translates to 0000:0000:ffff:0000 , this address was assigned as the IPv4 subnet prefix to IPv6, so any IPv6 processor will understand how it works with an IPv4 address, and will handle it accordingly.

In the near future, all IP addresses will be IPv6, because we have almost no numbers (4.2 billion minus some space for different purposes) in the IPv4 address space.

IPv6 allows much more space. "340 ounces should be enough for everyone," says Bill Gates, speaking on IPv6.

It is important to start addressing IP addresses using the IPv6 namespace and therefore include ::ffff: in your code, because in the future there will be real hexadecimal data between these colons. If you delete it for aesthetic reasons, your code will break when it switches to an IPv6 network or encounters an IPv6 address.

Some networks currently use IPv6, and soon you will come across IPv6 IP addresses; jump now or risk breaking your code in the future.

TL; DR (short) version of the question: everything is working fine. Do not change it, this is the IPv6 version of the IPv4 address.

IPv6 IPv4

If you want to make your code compatible with IPv6, all you have to do is check the ::ffff: prefix ::ffff: if it exists, delete it and treat the remainder as IPv4 ... if ::ffff: didn 'It exists, this is the address IPv6 and should be handled as such. You can double-check by looking to see if there are dots in the line, if so, then this is IPv4.

Don’t forget about everything except the IP address settings that you just write down, right? It will be important to analyze and register aggregates in order to expect ::ffff:127.0.0.1 and so on in the future. If you do not need to change the IP, just leave it as what you get.

+116
Nov 18 '15 at 21:05
source share

This seems to be the ipv6 quirk: for ipv4 addresses, ipv6 seems to mix ipv6 notation with ipv4 notation.

To get both ipv4 and ipv6 addresses in simple, unmixed notation, I use:

 var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; if (ip.substr(0, 7) == "::ffff:") { ip = ip.substr(7) } 
+15
Sep 13 '16 at 2:54 on
source share

In Windows 7, IPv6 is enabled by default. Although my server only listens for IPv4, Windows 7 sends the prefix ::ffff: to IPv4 as part of the transition to IPv6

::ffff:0:0:0/96 - the prefix used for IPv4-translated addresses that are used by the stateless protocol IGMP Translation (SIIT).

Transition from IPv4

+9
Apr 02 '15 at 15:20
source share

I had problems trying to compare the mapped ipv4 addresses and found a useful ipaddr.js library :-)

eg,

 _.isEqual(ipaddr.process('::ffff:127.0.0.1'), ipaddr.process('127.0.0.1')) === true 
+8
Jun 17 '15 at 23:57
source share

If you just need IPv4, you can force the host server to listen on IPv4.

For an express application, edit /bin/www :

change

 server.listen(port); 

at

 server.listen(port, '0.0.0.0'); 

It worked for me at least.

https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

0
09 Oct '18 at 15:04
source share
 var ip = req.ip.split(':').pop(); 
0
Nov 01 '18 at 17:12
source share

Try this to get the exact IP address by deleting the subnet,

 let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; ip = ip.replace('::ffff:', ''); 
0
Jan 19 '19 at 1:39
source share

You can get your IP address yourself or with the specified family using sockets

  var app = require('express')(); app.get("/ip", (req, res) => { console.log(req.ip) let ip = req.ip.split(':'); let ip_details = req.socket.address(); console.log(ip_details); // { address: '::ffff:127.0.0.1', family: 'IPv6', port: 3001 console.log(ip[3]);//127.0.0.1 res.json(ip[3]); } 
-one
Nov 17 '17 at 12:42 on
source share



All Articles