How to get client's IP address using JavaScript?

Mahabubur Rahman
0

 If would like to find out the client IP address you need to call thirdparty api. Some thirdparty api provide api. I would like to use cludflare api to findout IP address. See the bellow example - 


    var xmlHttp = new XMLHttpRequest();

    xmlHttp.open("GET", 'https://www.cloudflare.com/cdn-cgi/trace', false);

    xmlHttp.send(null);

    let data = xmlHttp.responseText;

    data = data.trim().split('\n').reduce(function(obj, pair) {

        pair = pair.split('=');

        return obj[pair[0]] = pair[1],

        obj;

    }, {});

    console.log(data) ;

Output - 


{

  1. colo"SIN"
  1. fl"412f78"
  1. gateway"off"
  1. h"www.cloudflare.com"
  1. http"http/2"
  1. ip"103.148.178.2"
  1. loc"BD"
  1. sni"plaintext"
  1. tls"TLSv1.3"
  1. ts"1628099709.454"
  1. uag"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 CCleaner/91.1.10935.167"
  1. visit_scheme"https"
  1. warp"off"
}



Post a Comment

0Comments
Post a Comment (0)