2

https://jsbin.com/nabapojupu/edit?js,console

Snippet which it runs:

var xhttp = new XMLHttpRequest();
xhttp.open('GET', window.origin, true);
xhttp.onreadystatechange = function () {
  if (this.readyState == 4) {
      console.log(this);
  }
};
xhttp.send();

The image below is a screenshot of the above link with the browser network tab open; Highlighted is the IP of the domain requested..

Is it possible to access the request IP in JavaScript?

enter image description here

John
  • 4,512
  • 8
  • 48
  • 98
  • 2
    Does this answer your question? [Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?](https://stackoverflow.com/questions/102605/can-i-perform-a-dns-lookup-hostname-to-ip-address-using-client-side-javascript) – TheMisir Nov 21 '20 at 16:34
  • There's XMLHttpRequest.getAllResponseHeaders() that displays the header fully, but I have no idea if the remote IP is part of it. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders –  Nov 21 '20 at 16:49
  • @TheMisir yes i think this answers my question. @fqrd that is good to know though thanks. My objective here was to attempt to detect if someone faking a website, ie using simple hack like `/etc/hosts` - seems like there is no 100% reliable way. – John Nov 21 '20 at 16:57
  • 1
    @John the standard solution to achieve this is HTTPS – Guerric P Nov 22 '20 at 11:20

1 Answers1

1

The Web Hypertext Application Technology Working Group (WHATWG) doesn't specify any API for DNS name resolution, only Firefox implements browser.dns.resolve() but this is very unlikely to become part of a standard specification.

Guerric P
  • 20,579
  • 2
  • 28
  • 66