简体   繁体   English

如何在nodejs expressjs中将ipv6地址转换为ipv4地址?

[英]how to convert ipv6 address to ipv4 address in nodejs expressjs?

的的NodeJS Request对象提供了这种方法req.connection.remoteAddres获取客户端的IP地址,但它给人以IPv6格式的地址,我怎么把它转换成IPv4格式哪个更可读?

If the IPv6 address starts with ::ffff: then the client is communicating with IPv4 to an IPv6 application.如果 IPv6 地址以::ffff:开头,则客户::ffff:在使用 IPv4 与 IPv6 应用程序通信。 In that case the remainder of the address is the IPv4 address.在这种情况下,地址的其余部分是 IPv4 地址。 It might we written as ::ffff:10.11.12.13 , in which case you can easily see the IPv4 address.我们可能会写成::ffff:10.11.12.13 ,在这种情况下,您可以轻松看到 IPv4 地址。 It can also be written as ::ffff:0a0b:0c0d or ::ffff:a0b:c0d , in which case you need to convert the last part of the address from hexadecimal to decimal.它也可以写成::ffff:0a0b:0c0d::ffff:a0b:c0d ,在这种情况下,您需要将地址的最后一部分从十六进制转换为十进制。

If the IPv6 address doesn't start with ::ffff: then the client is really communicating with IPv6, and no conversion is possible because IPv4 and IPv6 are different protocols with different addresses.如果 IPv6 地址不以::ffff:开头,则客户端实际上是在与 IPv6 通信,并且无法进行转换,因为 IPv4 和 IPv6 是具有不同地址的不同协议。 Systems can have only IPv4, only IPv6 or a combination of both.系统可以只有 IPv4、只有 IPv6 或两者的组合。 There is no way for you to know that by looking at the address.您无法通过查看地址知道这一点。

The problem is because by default, NodeJS listens on IPv6 so it returns IPv6 addresses.问题是因为默认情况下,NodeJS 侦听 IPv6,因此它返回 IPv6 地址。 If you tell it to only listen on IPv4 then you will only get IPv4 addresses, and they will be in the format you expect (no ::ffff: prefix).如果您告诉它仅侦听 IPv4,那么您将只会获得 IPv4 地址,并且它们将采用您期望的格式(没有::ffff:前缀)。

How you do this depends on the library you're using, but typically where you specify the port to listen on, you can also specify a host/interface/IP and here you'd put in 0.0.0.0 to say "IPv4 only", as opposed the default :: which means IPv6+IPv4.您如何执行此操作取决于您使用的库,但通常您指定要侦听的端口的位置,您还可以指定主机/接口/IP,并在此处输入0.0.0.0以表示“仅限 IPv4” ,而不是默认的:: ,这意味着 IPv6+IPv4。

For example with the NodeJS socket library:例如使用 NodeJS 套接字库:

server.listen({
    port: 80,
    host: '0.0.0.0',
})

For WebSockets:对于 WebSocket:

...listen(80, '0.0.0.0');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ipv4 / ipv6网络地址匹配node-js - ipv4/ipv6 network address match in node-js 如何使用Sockets.io + Node.js服务器获取客户端IPv4地址(而不是IPv6) - How to get client IPv4 address (not IPv6) using Sockets.io + Node.js server 本地主机默认为 ipv6 地址,而不是 fedora linux 上的 ipv4。 无法从nodejs连接到mongodb - localhost defaults to ipv6 address instead of ipv4 on fedora linux. Can't connect to mongodb from nodejs 如何从一个字符串中拆分出 ipv6 和 ipv4? - How to split up ipv6 and ipv4 from one string? 如何拆分 IPV6 地址以获取 Javascript 中的 Network ID 子网? - How to split IPV6 address to get the Network ID subnet in Javascript? IPv6 地址过滤行为异常 - IPv6 Address filtering behaving unusually 如何让 Nginx 服务器 NodeJS 应用程序在端口 8080 上运行以与 EC2 公共 IPv4 地址的 React 客户端通信 - How to get Nginx server NodeJS application running on port 8080 to talk to React Client at EC2 public IPv4 address 将 IPv4 IP 地址与存储在 MYSQL 上的网络地址进行比较 - Comparing IPv4 IP address with Network address stored on MYSQL 如何使用 nodejs 获取公共 Ipv6 - How to get public Ipv6 with nodejs 在socket.io中获取客户端的IPv4地址 - Get IPv4 Address of client in socket.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM