使用 JavaScript 执行 DNS 查找以将主机名解析为 IP 地址
是否可以使用 Javascript 解析主机名?
这是假设的代码:
var hostname = "www.yahoo.com";
var ipAddress = DnsLookup(hostname);
console.log(ipAddress);
我正在寻找那个神奇的 DnsLookup()
函数。 :-)
Is it possible to resolve a hostname using 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. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 JavaScript 中没有标准的 DNS 功能,但您始终可以调用执行 DNS 解析的第 3 方公共 API。
例如Encloud就提供了这样的API,你可以为其创建一个XMLHttpRequest:
当然,您应该获得自己的身份验证令牌。免费 Enclout 帐户每分钟限制为 6 个请求。
如果您只需要 IP,请对 http://api.konvert.me 发出 GET 请求/forward-dns/yourdomain.com。
While there is no standard DNS functionality in JavaScript, you can always call a 3rd party public API that does DNS resolution.
For example, Encloud provides such an API, and you can make an XMLHttpRequest for it:
Of course, you should get your own Auth token. Free Enclout accounts are limited to 6 request per minute.
If you just want the IP, make a GET request for http://api.konvert.me/forward-dns/yourdomain.com.
有一个相对较新(2018 年)提议的互联网标准,称为 DNS over HTTPS(也称为 DoH )那已经起飞了。它允许您通过 HTTPS 将线格式 DNS 查询发送到“DoH 服务器”。好处是,通过 DoH,您可以在 HTTPS 之上获得整个 DNS 协议。这意味着您可以获得很多有用的信息。
话虽这么说,有一个名为 dohjs 的开源 JavaScript 库,可以很容易地执行在浏览器中进行 DNS 查找。下面是一个快速示例代码片段:
全面披露:我是 dohjs 的贡献者。
cURL's DNS over HTTPS wiki 页面上有很多资源,包括公共 DoH 服务器列表和 DoH 工具列表(主要是服务器和客户端软件)。
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.
That being said, there's an open source JavaScript library called dohjs that makes it pretty easy to do a DNS lookup in the browser. Here's a quick example code snippet:
Full disclosure: I'm a contributor to dohjs.
There are a lot of resources on cURL's DNS over HTTPS wiki page including a list of public DoH servers and a list of DoH tools (mainly server and client software).
您需要回调到服务器端并从那里解析该值。 Javascript 中没有可用的标准 DNS 查找功能。
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 被阻止发出跨域请求。那里可能有一些黑客可能能够帮助您(this 一个看起来很有希望),但默认情况下你不能这样做。
您也许能够请求某些内容并确保返回 HTTP 200。
No - javascript is blocked from making cross domain requests. There are potentially some hacks out there that might be able to help you (this one looked kinda promising), but by default you can't do that.
You might be able to request something and make sure you get back a HTTP 200.