Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

dns

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?

like image 231
Noah Jacobson Avatar asked Sep 19 '08 15:09

Noah Jacobson


People also ask

How do I map a DNS name to an IP address?

Select Basic DNS Administration. Select Point http://www.yourdomainname.com to a new IP address and click Next. Enter the new IP address.

Is browser makes a DNS lookup to get the IP address of a corresponding website?

Browser looks up IP address for the domain To do that, it needs to look up the IP address of the server hosting the website using the domain you typed in. It does this using a DNS lookup.

Does DNS translate domain names into IP addresses?

DNS translates domain names to IP addresses so browsers can load Internet resources. Each device connected to the Internet has a unique IP address which other machines use to find the device. DNS servers eliminate the need for humans to memorize IP addresses such as 192.168.

Which tool can query a DNS server for an IP address?

The nslookup is a built-in command-line tool available in most Operating Systems. It is used for querying the DNS and obtaining domain names, IP addresses, and DNS resource record information.


2 Answers

Edit: This question gave me an itch, so I put up a JSONP webservice on Google App Engine that returns the clients ip address. Usage:

<script type="application/javascript"> function getip(json){   alert(json.ip); // alerts the ip address } </script>  <script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"> </script> 

Yay, no server proxies needed.


Pure JS can't. If you have a server script under the same domain that prints it out you could send a XMLHttpRequest to read it.

like image 73
Zach Avatar answered Sep 27 '22 22:09

Zach


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.

Another JavaScript library with similar features is found here - https://github.com/sc0Vu/doh-js-client. I haven't used this one personally, but I think it would work client side as well.

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://www.rfc-editor.org/rfc/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).

like image 40
kimbo Avatar answered Sep 27 '22 23:09

kimbo