Jump to content

Need to get output into variable


Recommended Posts

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites
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.

Link to post
Share on other sites
9 minutes ago, requinix said:

So don't get it outside the function :confused:

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.

Link to post
Share on other sites
This thread is more than a year old.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.