Express.js req.ip 正在返回 ::ffff:127.0.0.1 [英] Express.js req.ip is returning ::ffff:127.0.0.1

查看:35
本文介绍了Express.js req.ip 正在返回 ::ffff:127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试获取所请求用户的 IP.问题是 IP 返回 ::ffff:127.0.0.1 而不是 127.0.0.1.我尝试使用 trusted proxy 选项(虽然没有使用代理)并且 req.ips 是空白的.使用 4.x Express.js.

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

router.get('/', function(req, res, next) {
    console.log('ip', req.ip)
    res.send({})
});

推荐答案

::ffff: 是放置在 IPv6(128 位)空间内的 IPv4(32 位)地址的子网前缀.IPv6 分为两部分,子网前缀和接口后缀.每个长度为 64 位,即 4 组 4 个十六进制字符.

::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.

在 IPv6 中,您可以删除前导零,然后删除连续的零,这意味着 ::ffff: 实际上转换为 0000:0000:ffff:0000,该地址已被指定为 IPv4 到 IPv6 子网的前缀,因此任何 IPv6 处理器都会理解它正在使用 IPv4 地址并相应地处理它.

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.

在不久的将来,IP 地址都将是 IPv6,这是因为 IPv4 地址空间中的数字几乎用完了(42 亿,减去一些用于其他用途的空间).

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.

IPv6 允许更大的空间."340 十进制应该是对任何人来说都足够了"- 比尔盖茨就 IPv6 发表演讲.

IPv6 allows for a much larger space. "340 undecillion ought to be enough for anyone" - Bill Gates speaking on IPv6.

开始使用 IPv6 命名空间寻址 IP 地址很重要,因此在您的代码中包含 ::ffff: 因为将来这些冒号之间将有真正的十六进制数据.如果出于美观原因将其剥离,则当它切换到 IPv6 网络或遇到 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,您很快就会遇到 IPv6 IP 地址;现在就实现飞跃,否则将来可能会破坏您的代码.

问题的 TL;DR(简短)版本是:一切正常.不要更改它,它是 IPv4 地址的 IPv6 版本.

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.

如果您想让您的代码与 IPv6 兼容,您所要做的就是检查 ::ffff: 前缀...如果存在,请将其删除并将其余部分作为 IPv4 处理...如果 ::ffff: 不存在,它是一个 IPv6 地址,需要这样处理.您可以仔细检查字符串中是否有句点,如果是,则是 IPv4.

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.

请记住,除了需要对 IP 地址进行调整之外的所有内容,您只是在记录 IP,对吗?对于解析器和日志聚合来说,期待 ::ffff:127.0.0.1 等未来会很重要.除非您需要更改 IP,否则将其保留为您收到的内容.

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.1 and such in the future. Unless you need to alter an IP, just leave it as what you receive.

这篇关于Express.js req.ip 正在返回 ::ffff:127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆