Jump to content

How to get client IP address?


showme

Recommended Posts

  • 11 years later...
4 hours ago, jackamin said:

is there a way to also get the local ip or some other identification to identify the pc.

this work for me on Chromehttps://stackoverflow.com/questions/20194722/can-you-get-a-users-local-lan-ip-address-via-javascript

"Last year I used Linblow's answer (2018-Oct-19) to successfully discover my local IP via javascript. However, recent Chrome updates (76?) have wonked this method so that it now returns an obfuscated IP, such as: 1f4712db-ea17-4bcf-a596-105139dfd8bf.local

If you have full control over your browser, you can undo this behavior by turning it off in Chrome Flags, by typing this into your address bar:

chrome://flags/#enable-webrtc-allow-input-volume-adjustment

and DISABLING the flag Anonymize local IPs exposed by WebRTC

In my case, I require the IP for a TamperMonkey script to determine my present location and do different things based on my location. I also have full control over my own browser settings (no Corporate Policies, etc). So for me, changing the chrome://flags setting does the trick."

 

and use this code in uniButtonClick

uniSession.AddJS ('const findLocalIp = (logInfo = true) => new Promise( (resolve, reject) => {'
                  + '    window.RTCPeerConnection = window.RTCPeerConnection'
                  + '                            || window.mozRTCPeerConnection'
                  + '                            || window.webkitRTCPeerConnection;'

                  + '    if ( typeof window.RTCPeerConnection == ''undefined'' )'
                  + '        return reject(''WebRTC not supported by browser'');'

                  + '    let pc = new RTCPeerConnection();'
                  + '    let ips = [];'

                  + '    pc.createDataChannel("");'
                  + '    pc.createOffer()'
                  + '     .then(offer => pc.setLocalDescription(offer))'
                  + '     .catch(err => reject(err));'
                  + '    pc.onicecandidate = event => {'
                  + '        if ( !event || !event.candidate ) {'
                  // All ICE candidates have been sent.
                  + '            if ( ips.length == 0 )'
                  + '                return reject(''WebRTC disabled or restricted by browser'');'

                  + '            return resolve(ips);'
                  + '        }'

                  + '        let parts = event.candidate.candidate.split('' '');'
                  + '        let [base,componentId,protocol,priority,ip,port,,type,...attr] = parts;'
                  + '        let component = [''rtp'', ''rtpc''];'

                  + '        if ( ! ips.some(e => e == ip) )'
                  + '            ips.push(ip);'

                  + '        if ( ! logInfo )'
                  + '            return;'

//                  + '        console.log(" candidate: " + base.split('':'')[1]);'
//                  + '        console.log(" component: " + component[componentId - 1]);'
//                  + '        console.log("  protocol: " + protocol);'
//                  + '        console.log("  priority: " + priority);'
//                  + '        console.log("        ip: " + ip);'
//                  + '        console.log("      port: " + port);'
//                  + '        console.log("      type: " + type);'

//                  + '        if ( attr.length ) {'
//                  + '            console.log("attributes: ");'
//                  + '            for(let i = 0; i < attr.length; i += 2)'
//                  + '                console.log("> " + attr[i] + ": " + attr[i+1]);'
//                  + '        };'

//                  + '        console.log();'
                  + '             let p = document.getElementById(''' + UniLabel10.JSID + ''');'
                  + '             p.innerHTML = ip;'
                  + '             alert (ip);'
                  + '    };'
                  + '} );'
 //read local IP on Client Side
//                  + 'let p = document.getElementById(''ip'');'
//                  + 'findLocalIp().then('
//                  + '        ips => {'
//                  + '            let s = '';'
//                  + '            ips.forEach( ip => s += ip + ''<br>'' );'
//                  + '            p.innerHTML = s;'
//                  + '        };'
//                  + '        err => p.innerHTML = err'
//                  + ' );'
                  + 'findLocalIp();'
                   );
 

Here is a demo Project

ClientInfo.zip

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...