Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?

后端 未结 16 1474
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 11:45

I would like to use client-side Javascript to perform a DNS lookup (hostname to IP address) as seen from the client\'s computer. Is that possible?

相关标签:
16条回答
  • 2020-11-22 11:57

    I know this question was asked a very long time ago, but I figured I'd offer a more recent answer.

    DNS over HTTPS (DoH)

    You can send DNS queries over HTTPS to DNS resolvers that support it. The standard for DOH is described in RFC 8484.

    This is a similar thing to what all the other answers suggest, only that DoH is actually the DNS protocol over HTTPS. It's also a "proposed" Internet standard and it's becoming quite popular. For example, some major browsers either support it or have plans to support it (Chrome, Edge, Firefox), and Microsoft is in the process of building it into their operating system.

    One of the purposes of DoH is:

    allowing web applications to access DNS information via existing browser APIs in a safe way consistent with Cross Origin Resource Sharing (CORS)

    There's an open source tool made especially for doing DNS lookups from web applications called dohjs. It does DNS over HTTPS (DoH) wireformat queries as described in RFC 8484. It supports both GET and POST methods.

    Full disclosure: I am a contributor to dohjs.

    DNS over HTTPS JSON APIs

    If you don't want to bother with DNS wireformat, both Google and Cloudflare offer JSON APIs for DNS over HTTPS.

    • Google's endpoint: https://dns.google/resolve?
    • Google's JSON API docs: https://developers.google.com/speed/public-dns/docs/doh/json
    • Cloudflare's endpoint: https://cloudflare-dns.com/dns-query?
    • Cloudflare's JSON API docs: https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/

    Example Javascript code to lookup example.com with Google's JSON DOH API:

    var response = await fetch('https://dns.google/resolve?name=example.com');
    var json = await response.json();
    console.log(json);
    

    Examples from the RFC for DOH GET and POST with wireformat

    Here are the examples the RFC gives for both GET and POST (see https://tools.ietf.org/html/rfc8484#section-4.1.1):

    GET example:

    The first example request uses GET to request "www.example.com".

    :method = GET
    :scheme = https
    :authority = dnsserver.example.net
    :path = /dns-query?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB
    accept = application/dns-message

    POST example:

    The same DNS query for "www.example.com", using the POST method would be:

    :method = POST
    :scheme = https
    :authority = dnsserver.example.net
    :path = /dns-query
    accept = application/dns-message
    content-type = application/dns-message
    content-length = 33

    <33 bytes represented by the following hex encoding> 00 00 01 00 00 01 00 00 00 00 00 00 03 77 77 77 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 00 01

    Other places to send DOH queries

    You can find a list of some public DNS resolvers that support DNS over HTTPS in a couple places:

    • DNSCrypt has a long list of public DoH and DNSCrypt resolver on their Github, and a nice interactive version of the list at https://dnscrypt.info/public-servers/
    • Wikipedia - comparison of public recursive nameservers
    • List on Curl's wiki
    • (short) list on dnsprivacy.org

    Of the above resources, I'd say that the list on Curl's wiki and the DNSCrypt list are are probably the most complete and the most frequently updated. Curl's page also includes a list of open source tools for DoH (servers, proxies, client libs, etc).

    0 讨论(0)
  • 2020-11-22 11:58

    There's a third-party service which provides a CORS-friendly REST API to perform DNS lookups from the browser - https://exana.io/tools/dns/

    0 讨论(0)
  • 2020-11-22 12:01

    I am aware this is an old question but my solution may assist others.

    I find that the JSON(P) services which make this easy do not last forever but the following JavaScript works well for me at the time of writing.

    <script type="text/javascript">function z (x){ document.getElementById('y').innerHTML=x.query }</script>
    <script type='text/javascript' src='http://ip-api.com/json/zero.eu.org?callback=z'></script>
    

    The above writes my server's IP on the page it is located but the script can be modified to find any IP by changing 'zero.eu.org' to another domain name. This can be seen in action on my page at: http://meon.zero.eu.org/

    0 讨论(0)
  • 2020-11-22 12:01

    I don't think this is allowed by most browsers for security reasons, in a pure JavaScript context as the question asks.

    0 讨论(0)
  • 2020-11-22 12:02

    There's no notion of hosts or ip-addresses in the javascript standard library. So you'll have to access some external service to look up hostnames for you.

    I recommend hosting a cgi-bin which looks up the ip-address of a hostname and access that via javascript.

    0 讨论(0)
  • 2020-11-22 12:04

    Firefox has a built-in API for this since v60, for WebExtensions:

    https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/dns/resolve

    0 讨论(0)
提交回复
热议问题