From 06a455b029e177cd87f5733d0a7ccc99c6a1c554 Mon Sep 17 00:00:00 2001 From: Esmail EL BoB Date: Fri, 26 Jul 2019 18:56:10 +0200 Subject: [PATCH] WebRTC Problem! there was a small bug inside my code & thx a lot @RedSQL for helping me out :) --- index.html | 4 +++- main.js | 36 +++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 8ca3225..b8b2792 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,8 @@

Your Public IP Is:

@@ -55,7 +57,7 @@

ــــــــــــــــــــــ

-

Your Net's Speed Is:

+

Your Net Speed Is:

diff --git a/main.js b/main.js index e47798c..0fe3154 100644 --- a/main.js +++ b/main.js @@ -1,18 +1,28 @@ // Find my ip code -var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection; -var peerConn = new RTCPeerConnection({ - 'iceServers': [{ - 'urls': ['stun:stun.l.google.com:19302'] - }] +// Since when webrtc is disabled, webkitRTCPeerConnection is nonexistent, therefore we try and catch and define RTCPeerConnection with no value because webrtc is disabled. +try { + var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection; +} catch { + var RTCPeerConnection; +} +if (RTCPeerConnection) { + var peerConn = new RTCPeerConnection({ + 'iceServers': [{ + 'urls': ['stun:stun.l.google.com:19302'] + }] -}); -var dataChannel = peerConn.createDataChannel('test'); // Needs something added for some reason -peerConn.createOffer({}).then((desc) => peerConn.setLocalDescription(desc)); -peerConn.onicecandidate = (e) => { - if (e.candidate == null) { - document.getElementById("ip").innerText = /c=IN IP4 ([^\n]*)\n/.exec(peerConn.localDescription.sdp)[1]; - } -}; + }); + var dataChannel = peerConn.createDataChannel('test'); // Needs something added for some reason + peerConn.createOffer({}).then((desc) => peerConn.setLocalDescription(desc)); + peerConn.onicecandidate = (e) => { + if (e.candidate == null) { + document.getElementById("ip").innerText = /c=IN IP4 ([^\n]*)\n/.exec(peerConn.localDescription.sdp)[1]; + } + }; +} else { + // Inform user that webrtc fetch failed + document.getElementById("ip").innerText = 'Failed to fetch IP via WebRTC, perhaps your WebRTC is disabled?'; +} // ـــــــــــــــــــــــــــــــــــ // Location