title: Get public ip address in javascript
body: <p>I'm looking for a simple function (as simple as it can be) for returning the public ip address.
  I came across this function:</p>
  
  <pre><code>const clientsIpAdress = (onNewIP) =&gt; {
    const MyPeerConnection =
      window.RTCPeerConnection ||
      window.mozRTCPeerConnection ||
      window.webkitRTCPeerConnection;
    const pc = new MyPeerConnection({
      iceServers: []
    });
    const noop = () =&gt; {};
    const localIPs = {};
    const ipRegex =
      /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
  
    const iterateIP = (ip) =&gt; {
      if (!localIPs[ip]) onNewIP(ip);
      localIPs[ip] = true;
    };
    pc.createDataChannel('');
    pc.createOffer().then((sdp) =&gt; {
      sdp.sdp.split('\n').forEach((line) =&gt; {
        if (line.indexOf('candidate') &lt; 0) return;
        line.match(ipRegex).forEach(iterateIP);
      });
  
      pc.setLocalDescription(sdp, noop, noop);
    });
    pc.onicecandidate = (ice) =&gt; {
      if (!ice || !ice.candidate ||
        !ice.candidate.candidate ||
        !ice.candidate.candidate.match(ipRegex)) return;
      ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    };
  };
  export default clientsIpAddress;
  </code></pre>
  
  <p>but it is returning the local ipAddress. Any ideas?</p>