使用 JavaScript 执行 DNS 查找以将主机名解析为 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7113072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Perform a DNS lookup to resolve a hostname to an IP address using JavaScript
提问by Amir
Is it possible to resolve a hostname using Javascript?
是否可以使用 Javascript 解析主机名?
Here would be hypothetical code:
这是假设的代码:
var hostname = "www.yahoo.com";
var ipAddress = DnsLookup(hostname);
console.log(ipAddress);
I am looking for that magic DnsLookup()
function. :-)
我正在寻找那个神奇的DnsLookup()
功能。:-)
回答by Dan Dascalescu
While there is no standard DNS functionality in JavaScript, you can always call a 3rd party public API that does DNS resolution.
虽然 JavaScript 中没有标准的 DNS 功能,但您始终可以调用进行 DNS 解析的第 3 方公共 API。
For example, Encloud provides such an API, and you can make an XMLHttpRequest for it:
比如Encloud提供了这样一个API,你可以为其创建一个XMLHttpRequest:
var oReq = new XMLHttpRequest();
oReq.onload = function () {
var response = JSON.parse(this.responseText);
alert(JSON.stringify(response.dns_entries));
}
oReq.open("get", "https://www.enclout.com/api/v1/dns/show.json?auth_token=rN4oqCyJz9v2RRNnQqkx&url=stackoverflow.com", true);
oReq.send();
Of course, you should get your own Auth token. Free Enclout accounts are limited to 6 request per minute.
当然,您应该获得自己的 Auth 令牌。免费 Enclout 帐户每分钟限制为 6 个请求。
If you just want the IP, make a GET request for http://api.konvert.me/forward-dns/yourdomain.com.
如果您只想要 IP,请为http://api.konvert.me/forward-dns/yourdomain.com发出 GET 请求。
回答by kimbo
There's a relatively new (2018) proposed Internet standard called DNS over HTTPS(also called DoH) that's been taking off. It allows you to send wireformat DNS queries over HTTPS to "DoH servers". The nice thing is, with DoH you get the whole DNS protocol on top of HTTPS. That means you can obtain a lot useful information.
有一个相对较新(2018 年)提议的互联网标准,称为DNS over HTTPS(也称为 DoH),并且已经开始流行。它允许您通过 HTTPS 向“DoH 服务器”发送线格式 DNS 查询。好消息是,使用 DoH,您可以在 HTTPS 之上获得整个 DNS 协议。这意味着您可以获得很多有用的信息。
That being said, there's an open source JavaScript library called dohjsthat makes it pretty easy to do a DNS lookup in the browser. Here's a quick example code snippet:
话虽如此,有一个名为dohjs 的开源 JavaScript 库可以让在浏览器中进行 DNS 查找变得非常容易。这是一个快速示例代码片段:
const resolver = new doh.DohResolver('https://1.1.1.1/dns-query')
resolver.query('www.yahoo.com')
.then(console.log)
.catch(console.error);
Full disclosure: I'm a contributor to dohjs.
完全披露:我是 dohjs 的贡献者。
There are a lot of resources on cURL's DNS over HTTPS wiki pageincluding a list of public DoH servers and a list of DoH tools (mainly server and client software).
cURL 的 DNS over HTTPS wiki 页面上有很多资源,包括公共 DoH 服务器列表和 DoH 工具列表(主要是服务器和客户端软件)。
回答by Matthew Abbott
You'll need to callback to server-side and resolve the value from there. There is no standard DNS lookup functionality available in Javascript.
您需要回调到服务器端并从那里解析值。Javascript 中没有可用的标准 DNS 查找功能。
回答by Tejs
No - javascript is blocked from making cross domain requests. There are potentially some hacks out there that might be able to help you (thisone looked kinda promising), but by default you can't do that.
否 - javascript 被阻止进行跨域请求。可能有一些黑客可以帮助你(这个看起来很有希望),但默认情况下你不能这样做。
You might be able to request something and make sure you get back a HTTP 200.
您也许能够请求某些东西并确保您得到 HTTP 200。