使用 JavaScript 执行 DNS 查找以将主机名解析为 IP 地址

发布于 2024-11-30 05:14:00 字数 219 浏览 0 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏雪无痕 2024-12-07 05:14:00

虽然 JavaScript 中没有标准的 DNS 功能,但您始终可以调用执行 DNS 解析的第 3 方公共 API。

例如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();

当然,您应该获得自己的身份验证令牌。免费 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:

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.

If you just want the IP, make a GET request for http://api.konvert.me/forward-dns/yourdomain.com.

赴月观长安 2024-12-07 05:14:00

有一个相对较新(2018 年)提议的互联网标准,称为 DNS over HTTPS(也称为 DoH )那已经起飞了。它允许您通过 HTTPS 将线格式 DNS 查询发送到“DoH 服务器”。好处是,通过 DoH,您可以在 HTTPS 之上获得整个 DNS 协议。这意味着您可以获得很多有用的信息。

话虽这么说,有一个名为 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);

全面披露:我是 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:

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.

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).

撩人痒 2024-12-07 05:14:00

您需要回调到服务器端并从那里解析该值。 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.

千纸鹤带着心事 2024-12-07 05:14:00

否 - 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文