How to obtain an IP address via JS

Source: Internet
Author: User

Through the JS to get your real external network IP and intranet IP, even if the agent is not used, think it is too scary, but also can be happy to install force!

Code:

Get the IP addresses associated with a account

function Getips (callback) {

var ip_dups = {};

Compatibility for Firefox and Chrome

var rtcpeerconnection = window. Rtcpeerconnection

|| Window.mozrtcpeerconnection

|| Window.webkitrtcpeerconnection;

Bypass naive WEBRTC blocking

if (! Rtcpeerconnection) {

var iframe = document.createelement (' iframe ');

Invalidate content Script

Iframe.sandbox = ' Allow-same-origin ';

Iframe.style.display = ' None ';

Document.body.appendChild (IFRAME);

var win = Iframe.contentwindow;

Window. Rtcpeerconnection = win. Rtcpeerconnection;

Window.mozrtcpeerconnection = win.mozrtcpeerconnection;

Window.webkitrtcpeerconnection = win.webkitrtcpeerconnection;

rtcpeerconnection = window. Rtcpeerconnection

|| Window.mozrtcpeerconnection

|| Window.webkitrtcpeerconnection;

}

Minimal requirements for data connection

var mediaconstraints = {

Optional: [{rtpdatachannels:true}]

};

Firefox already have a default stun server in About:config

Media.peerconnection.default_iceservers =

[{"url": "Stun:stun.services.mozilla.com"}]

var servers = undefined;

Add same stun server for Chrome

if (window.webkitrtcpeerconnection)

Servers = {iceservers: [{urls: ' stun:stun.services.mozilla.com '}]};

Construct a new rtcpeerconnection

var pc = new Rtcpeerconnection (servers, mediaconstraints);

Listen for candidate events

Pc.onicecandidate = function (ICE) {

Skip Non-candidate Events

if (ice.candidate) {

Match just the IP address

var Ip_regex =/([0-9]{1,3} (\.[ 0-9]{1,3}) {3})/

var ip_addr = ip_regex.exec (Ice.candidate.candidate) [1];

Remove Duplicates

if (ip_dups[ip_addr] = = = undefined)

Callback (IP_ADDR);

IP_DUPS[IP_ADDR] = true;

}

};

Create a bogus data channel

Pc.createdatachannel ("");

Create an offer SDP

Pc.createoffer (function (result) {

Trigger the STUN server request

Pc.setlocaldescription (result, function () {}, function () {});

}, function () {});

}

Test:print the IP addresses into the console

Getips (function (IP) {console.log (IP);});

Because Firefox and Chrome support WEBRTC, can request to the stun server, return the internal and external network IP, different from XMLHttpRequest request, stun Request Developer tool not see the network request.

Solutions

So do we have a way to keep pretending? The answer is yes. We can close the WEBRTC.

Chrome

Installing the plugin https://chrome.google.com/webstore/detail/webrtc-block/nphkkbaidamjmhfanlpblblcadhfbkdm?hl=en

Can't download must be your online posture is unscientific.

Firefox

Disabled with media.peerconnection.enabled.

How to obtain an IP address via JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.