node.js Express.js req.ip 正在返回 ::ffff:127.0.0.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29411551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express.js req.ip is returning ::ffff:127.0.0.1
提问by rockerBOO
I am currently trying to get the IP of the requested user. The problem is the IP is returning ::ffff:127.0.0.1instead of 127.0.0.1. I tried using trusted proxyoption (though not using a proxy) and the req.ipsis blank. Using 4.x Express.js.
我目前正在尝试获取请求用户的 IP。问题是 IP 返回::ffff:127.0.0.1而不是127.0.0.1. 我尝试使用trusted proxy选项(虽然没有使用代理)并且req.ips是空白的。使用 4.x Express.js。
router.get('/', function(req, res, next) {
console.log('ip', req.ip)
res.send({})
});
回答by Nick Steele
::ffff:is a subnet prefix for IPv4 (32 bit) addresses that are placed inside an IPv6 (128 bit) space. IPv6 is broken into two parts, the subnet prefix, and the interface suffix. Each one is 64 bits long, or, 4 groups of 4 hexadecimal characters.
::ffff:是放置在 IPv6(128 位)空间内的 IPv4(32 位)地址的子网前缀。IPv6 分为两部分,子网前缀和接口后缀。每个是 64 位长,或 4 组 4 个十六进制字符。
In IPv6, you are allowed to remove leading zeros, and then remove consecutive zeros, meaning ::ffff:actually translates to 0000:0000:ffff:0000, this address has been designated as the IPv4 to IPv6 subnet prefix, so any IPv6 processor will understand it's working with an IPv4 address and handle it accordingly.
在 IPv6 中,您可以删除前导零,然后删除连续的零,这意味着::ffff:实际上转换为0000:0000:ffff:0000,该地址已被指定为 IPv4 到 IPv6 子网前缀,因此任何 IPv6 处理器都会理解它正在使用 IPv4 地址并处理它因此。
In the near future, IP addresses will all be IPv6, this is because we are nearly out of numbers (4.2 billion, minus some space for misc. purposes) in the IPv4 address space.
在不久的将来,IP 地址都将是 IPv6,这是因为 IPv4 地址空间中的数字几乎用完了(42 亿,减去一些用于其他用途的空间)。
IPv6 allows for a much larger space. "340 undecillion ought to be enough for anyone" - Bill Gates speaking on IPv6.
IPv6 允许更大的空间。“340 undecillion 对任何人来说都应该足够了” - 比尔·盖茨 (Bill Gates) 谈到 IPv6。
It is important to start addressing IP addresses using the IPv6 namespace and therefore include the ::ffff:in your code because in the future there will be real hexadecimal data between those colons. If you strip it off for aesthetic reasons, your code will break when it switches to an IPv6 network or it's confronted with an IPv6 address.
开始使用 IPv6 命名空间寻址 IP 地址很重要,因此将 包含::ffff:在您的代码中,因为将来这些冒号之间将存在真正的十六进制数据。如果出于美观原因将其剥离,则当它切换到 IPv6 网络或遇到 IPv6 地址时,您的代码将会中断。
Some networks are currently running IPv6 and you will soon be confronted with IPv6 IP addresses; make the leap now or risk breaking your code in the future.
一些网络目前正在运行 IPv6,您很快就会遇到 IPv6 IP 地址;现在就进行飞跃,否则将来可能会破坏您的代码。
The TL;DR (short) version of the matter is: Everything is working fine. Don't change it, it's the IPv6 version of an IPv4 address.
问题的 TL;DR(简短)版本是:一切正常。不要更改它,它是 IPv4 地址的 IPv6 版本。
If you want to make your code compatible with IPv6, all you have to do is check for the ::ffff:prefix... if it exists, remove it and process the remainder as IPv4... if ::ffff:doesn't exist, it's an IPv6 address and needs to be processed as such.
You can double-check by seeing if periods are in the string, if so, it's IPv4.
如果你想让你的代码与 IPv6 兼容,你所要做的就是检查::ffff:前缀......如果存在,将其删除并将其余部分作为 IPv4 处理......如果::ffff:不存在,它是一个 IPv6 地址和需要这样处理。您可以通过查看句点是否在字符串中来仔细检查,如果是,则是 IPv4。
Keep in mind for everything but adjustments you need to make to IP addresses, you're just recording the IP, right? It's going to be important to the parser and log aggregates to expect ::ffff:127.0.0.1and such in the future. Unless you need to alter an IP, just leave it as what you receive.
请记住,除了需要对 IP 地址进行调整之外的所有内容,您只是在记录 IP,对吗?这对解析器和日志聚合来说很重要,::ffff:127.0.0.1在未来等。除非您需要更改 IP,否则将其保留为您收到的内容。
回答by anneb
This seems to be a quirk of ipv6: for ipv4 addresses, ipv6 seems to mix ipv6 notation with ipv4 notation.
这似乎是 ipv6 的一个怪癖:对于 ipv4 地址,ipv6 似乎将 ipv6 表示法与 ipv4 表示法混合在一起。
In order to get both ipv4 and ipv6 addresses in the simple, unmixed notation, I am using:
为了以简单的、非混合的表示法同时获得 ipv4 和 ipv6 地址,我使用:
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
}
回答by Your Friend Ken
If you just need IPv4, you could force the node server to listen using IPv4.
如果您只需要 IPv4,则可以强制节点服务器使用 IPv4 进行侦听。
For an express app edit /bin/www:
对于快速应用程序编辑/bin/www:
change
改变
server.listen(port);
to
到
server.listen(port, '0.0.0.0');
This worked for me at least.
这至少对我有用。
https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
回答by rockerBOO
Windows 7 has IPv6 enabled by default. Even though my server listens on IPv4 only, Windows 7 sends the ::ffff:prefix to the IPv4 as part of the transition to IPv6
Windows 7 默认启用 IPv6。即使我的服务器仅侦听 IPv4,Windows 7 也会将::ffff:前缀发送到 IPv4,作为向 IPv6 过渡的一部分
::ffff:0:0:0/96— A prefix used for IPv4-translated addresses which are used by the Stateless IP/ICMP Translation (SIIT) protocol.
::ffff:0:0:0/96— 用于无状态 IP/ICMP 转换 (SIIT) 协议使用的 IPv4 转换地址的前缀。
回答by Bryce
回答by MAREESKANNNAN RAJENDRAN
Try this to get exact ip address by removing subnetting,
尝试通过删除子网划分来获取确切的 IP 地址,
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
ip = ip.replace('::ffff:', '');
回答by Mateo Marin
var ip = req.ip.split(':').pop();
回答by muthukumar helius
You can Get your Ip address alone or with The specified family using sockets
您可以单独或与使用套接字的指定系列一起获取您的IP地址
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]);
}


