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

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

问题描述

我目前正在尝试获取所请求的用户的IP。问题是IP正在返回 :: ffff:127.0.0.1 而不是 127.0.0.1 。我尝试使用信任代理选项(虽然不使用代理),而 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: / code>是位于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中,您可以删除前导零,然后删除连续的零, code> :: 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 aught to be enough for anyone" - Bill Gates speaking on IPv6.

开始寻址很重要IP地址使用IPv6命名空间,因此在您的代码中包含 :: 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 alter 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.01 将非常重要。除非您需要更改知识产权,否则只要将其视为您收到的。

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 parser and log aggregates to expect ::ffff:127.0.01 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天全站免登陆