How to find out the server IP address (using JavaScript) that the browser is connected to?

Viewed 144178

Is there any way you can find the IP of the server that the browser is connected to? For e.g. if the browser is accessing http://www.google.com, can we tell in any way what IP it is connected to? This is very useful in cases where Round-robin DNS is implemented. So say, the first request to a.com results in 1.1.1.1 & subsequent request result in 1.1.1.2 & so on.

I couldn't find any way to do it with JavaScript. Is it even possible? If not is there any universal way to find this info out?

11 Answers

You could do a nslookup on location.hostname!

const oReq = new XMLHttpRequest();
oReq.onload = function () {
  const 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=" + encodeURIComponent(location.hostname), true);
oReq.send();

EDIT: Website doesn't work anymore and gets CORS error!

That is now broken, so you can try: Add this to the HTML: <script src="https://cdn.jsdelivr.net/npm/dohjs@latest/dist/doh.min.js"></script> and then in JS:

const resolver = new doh.DohResolver('https://1.1.1.1/dns-query');
resolver.query(location.hostname, 'A')
  .then(response => {
    response.answers.forEach(ans => console.log(ans.data));
  })
  .catch(err => console.error(err));

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>

Related