saynotojava 0 Posted November 20, 2018 Share Posted November 20, 2018 I have this code: <script> function findIP(onNewIP) { var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var pc = new myPeerConnection({iceServers: [{urls: "stun:stun.l.google.com:19302"}]}), noop = function() {}, localIPs = {}, ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g, key; function ipIterate(ip) { if (!localIPs[ip]) onNewIP(ip); localIPs[ip] = true; } pc.createDataChannel(""); pc.createOffer(function(sdp) { sdp.sdp.split('/n').forEach(function(line) { if (line.indexOf('candidate') < 0) return; line.match(ipRegex).forEach(ipIterate); }); pc.setLocalDescription(sdp, noop, noop); }, noop); pc.onicecandidate = function(ice) { if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return; ice.candidate.candidate.match(ipRegex).forEach(ipIterate); }; } function addIP(ip) { var li = document.createElement('li'); li.textContent = ip; document.getElementById("IPLeak").appendChild(li); } findIP(addIP); </script> Which read local ip, ipv6 and ipv4 and output it into HTML element by using WebRTC protocol. But i trying to get that output store into js variable instead HTML element. Tried by adding variable into function addIP , but that variable cannot be read outside that function for some reason. And another thing which i cant figure out, how to output only ipv4 ip and not all 3 ip types. Quote Link to post Share on other sites
requinix 984 Posted November 20, 2018 Share Posted November 20, 2018 Getting it into a variable is irrelevant. What do you want to do with the value? 26 minutes ago, saynotojava said: how to output only ipv4 ip and not all 3 ip types There are only two types of IP address: v4 and v6. You can tell the difference by checking if the string contains a colon - IPv4 never will, IPv6 always does. Quote Link to post Share on other sites
saynotojava 0 Posted November 21, 2018 Author Share Posted November 21, 2018 12 hours ago, requinix said: Getting it into a variable is irrelevant. What do you want to do with the value? There are only two types of IP address: v4 and v6. You can tell the difference by checking if the string contains a colon - IPv4 never will, IPv6 always does. Same what i doing with almost any javascript- send it to ajax call so i can process it further with php. So that means i could get right ip adress with php explode, but i still need to get that output properly, as for example, when i put console log or alert into function addIP function, it loops , and if i tried to get it outside that function, then there is no output. Quote Link to post Share on other sites
requinix 984 Posted November 21, 2018 Share Posted November 21, 2018 So don't get it outside the function If you don't want to put the value into the HTML then don't do that. If you want to send the value through AJAX then do that. Quote Link to post Share on other sites
saynotojava 0 Posted November 21, 2018 Author Share Posted November 21, 2018 9 minutes ago, requinix said: So don't get it outside the function If you don't want to put the value into the HTML then don't do that. If you want to send the value through AJAX then do that. Currently that does not work as function before output 3 ip's instead one, for example when i was testing it with alert, each ip was displayed in 3 alert instead all 3 in one alert. Tho, could be solved if i would remove those 2 ip's and have one only, but dont know which line exactly to remove. Quote Link to post Share on other sites
requinix 984 Posted November 21, 2018 Share Posted November 21, 2018 findIP won't call your addIP multiple times for the same address. That means findIP is being called multiple times. You need to find out why. Quote Link to post Share on other sites
saynotojava 0 Posted November 21, 2018 Author Share Posted November 21, 2018 3 hours ago, requinix said: findIP won't call your addIP multiple times for the same address. That means findIP is being called multiple times. You need to find out why. Ok solution found. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.