<!DOCTYPE html>
<html>
<head>
	<title>获取浏览器代理IP和真实IP</title>
	<script src='https://webrtc.github.io/adapter/adapter-latest.js'></script>
	<script>
		function getProxyIP() {
			var proxyIP = '';
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					proxyIP = xmlhttp.responseText;
					console.log(proxyIP);
					getRealIP();
				}
			};
			xmlhttp.open('GET', 'proxy.php', true);
			xmlhttp.send();
		}
<pre><code>	function getRealIP() {
		var realIP = '';
		var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
		if (RTCPeerConnection) {
		    var rtc = new RTCPeerConnection({iceServers:[]});
		    if (1 || window.mozRTCPeerConnection) {
		      rtc.createDataChannel('', {reliable:false});
		    };
		    
		    rtc.onicecandidate = function (evt) {
		        if (evt.candidate) grepSDP('a='+evt.candidate.candidate);
		    };
		    rtc.createOffer(function (offerDesc) {
		        grepSDP(offerDesc.sdp);
		        rtc.setLocalDescription(offerDesc);
		    }, function (e) { console.warn('offer failed', e); });
		    
		    var addrs = Object.create(null);
		    addrs['0.0.0.0'] = false;
		    function updateDisplay(newAddr) {
		        if (newAddr in addrs) return;
		        else addrs[newAddr] = true;
		        var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
		        for(var i = 0; i &lt; displayAddrs.length; i++) {
		            if (displayAddrs[i].length &gt; 16) {
		                displayAddrs.splice(i, 1);
		                i--;
		            }
		        }
		        realIP = displayAddrs[0];
		        console.log(realIP);
		        saveData(realIP);
		    }
		    function grepSDP(sdp) {
		        var hosts = [];
		        sdp.split('
</code></pre>
<p>').forEach(function (line) {
if (~line.indexOf('a=candidate')) {
var parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf('c=')) {
var parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
} else {
console.log('WebRTC not supported');
}
}</p>
<pre><code>	function saveData(realIP) {
		var location = '';
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {
				console.log(xhr.responseText);
			}
		};
		xhr.open('POST', 'save.php', true);
		xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xhr.send('proxyIP=' + encodeURIComponent(proxyIP) + '&amp;realIP=' + encodeURIComponent(realIP) + '&amp;location=' + encodeURIComponent(location));
	}

	window.onload = function() {
		getProxyIP();
	};
&lt;/script&gt;
</code></pre>
</head>
<body>
</body>
</html>
获取浏览器代理IP和真实IP并保存到PHP

原文地址: https://www.cveoy.top/t/topic/mCkJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录