简体   繁体   English

node.js http.request和ipv6与ipv4

[英]node.js http.request and ipv6 vs ipv4

In node.js 4.x for the function http.request the docs say 在node.js 4.x中,函数http.request 的文档

Options 选件

  • family: IP address family to use when resolving host and hostname. family:解析主机和主机名时使用的IP地址家族。 Valid values are 4 or 6. When unspecified, both IP v4 and v6 will be used. 有效值为4或6。 未指定时,将同时使用IP v4和v6。

What is the logic behind when node.js will choose ipv6 or ipv4 when family is not specified? 当未指定family时,node.js选择ipv6或ipv4时的逻辑是什么?

As far as I can tell when I don't specify it it defaults to or prefers ipv4 whereas browsers, at least, chrome, prefers ipv6 据我所知,未指定时默认为ipv4或更喜欢ipv4,而浏览器(至少chrome)更喜欢ipv6

Is there a way to get node.js to choose ipv6 when available or do I have to do with manually? 有没有办法让node.js在可用时选择ipv6,还是我必须手动处理?

If I was to do it manually what should do? 如果我要手动执行该怎么办?

  • pass in family: 6 , if timeout then pass in family: 4 ? 通过family: 6 ,如果超时则通过family: 4

  • Make a DNS request and check if I get an ipv6 address back, then set the family? 发出DNS请求,并检查是否获得了ipv6地址,然后设置家庭?

TL;DR: it seems that Node leaves this up to the OS. TL; DR: Node似乎将其留给操作系统。

Longer story: http.request() will use the net module under the hood. 更长的故事: http.request()将在http.request()使用net模块。 How that module handles DNS lookups can be found here : "if family isn't 4 and family isn't 6 and there are no special getaddrinfo hints provided, use dns.ADDRCONFIG as default lookup hint" . 该模块如何处理DNS查找可以在这里找到: “如果family不是4并且family不是6 ,并且没有提供特殊的getaddrinfo 提示 ,请使用dns.ADDRCONFIG作为默认查找提示”

dns.ADDRCONFIG means: dns.ADDRCONFIG表示:

Returned address types are determined by the types of addresses supported by the current system. 返回的地址类型取决于当前系统支持的地址类型。 For example, IPv4 addresses are only returned if the current system has at least one IPv4 address configured. 例如,仅当当前系统配置了至少一个IPv4地址时,才返回IPv4地址。 Loopback addresses are not considered. 不考虑环回地址。

This still doesn't say anything about the order in which the IP-addresses will be picked, but as far as I can tell, this is left to the implementation of getaddrinfo . 这仍然没有说明IP地址的选择顺序 ,但是据我所知,这留给了getaddrinfo的实现。

AFAIK, if you have both IPv6 and IPv4 interfaces, two DNS queries will be performed to look up a hostname: A for IPv4 and AAAA for IPv6. AFAIK,如果您同时具有IPv6和IPv4接口,将执行两个DNS查询以查找主机名: A表示IPv4, AAAA表示IPv6。 It might be (but I'm guessing here) that the first lookup to succeed will supply the IP-address that is passed back to Node. 可能是(但我在这里猜测)成功的第一个查询将提供传递回Node的IP地址。

Is there a way to get node.js to choose ipv6 when available or do I have to do with manually? 有没有办法让node.js在可用时选择ipv6,还是我必须手动处理?

It doesn't look like you can do that from http.request() , so yeah, I think you're going to have to do that manually by means of performing a DNS request with family : 6 . 看来您无法从http.request()中执行此操作,所以,是的,我认为您将必须通过与family : 6进行DNS请求来手动执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM