node.js http.request和ipv6与ipv4 [英] node.js http.request and ipv6 vs ipv4
问题描述
在node.js 4.x中,该函数http.request
解决方案
TL; DR::Node似乎将其留给了操作系统.
更长的故事:http.request()
将使用引擎盖下的net
模块.在此处:,如果family不是4
,family不是6
,并且没有特殊的getaddrinfo
提示,请使用dns.ADDRCONFIG
作为默认查找提示" .
dns.ADDRCONFIG
的意思是:
返回的地址类型由当前系统支持的地址类型确定.例如,仅当当前系统配置了至少一个IPv4地址时,才返回IPv4地址.不考虑环回地址.
这仍然没有说明将选择IP地址的顺序,但是据我所知,这留给了getaddrinfo
的实现. /p>
AFAIK,如果同时具有IPv6和IPv4接口,则将执行两个DNS查询以查找主机名:A
用于IPv4,AAAA
用于IPv6.可能是(但我在这里猜),第一个成功的查询将提供传递回Node的IP地址.
有没有办法让node.js在可用时选择ipv6,还是我必须手动处理?
您似乎无法从http.request()
中执行此操作,所以,是的,我认为您将必须通过使用family : 6
执行DNS请求来手动执行此操作.
In node.js 4.x for the function http.request
the docs say
Options
- family: IP address family to use when resolving host and hostname. Valid values are 4 or 6. When unspecified, both IP v4 and v6 will be used.
What is the logic behind when node.js will choose ipv6 or ipv4 when family
is not specified?
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
Is there a way to get node.js to choose ipv6 when available or do I have to do with manually?
If I was to do it manually what should do?
pass in
family: 6
, if timeout then pass infamily: 4
?Make a DNS request and check if I get an ipv6 address back, then set the family?
TL;DR: it seems that Node leaves this up to the OS.
Longer story: http.request()
will use the net
module under the hood. 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.ADDRCONFIG
means:
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. 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
.
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. 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.
Is there a way to get node.js to choose ipv6 when available or do I have to do with manually?
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
.
这篇关于node.js http.request和ipv6与ipv4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!