Skip to content

Commit

Permalink
Added support for MS-Edge ip-detection.
Browse files Browse the repository at this point in the history
Fixed issues if media-enumeration API are disabled by chrome/firefox
plugins.

DetectRTC.DetectLocalIPAddress now access "stream" as second parameter.
By default it creates data-channels to detect ip-addresses.
  • Loading branch information
muaz-khan committed Nov 15, 2017
1 parent 0f9c230 commit a3cae23
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 173 deletions.
118 changes: 53 additions & 65 deletions DetectRTC.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

// Last Updated On: 2017-10-06 7:53:56 PM UTC
// Last Updated On: 2017-11-15 5:25:53 PM UTC

// ________________
// DetectRTC v1.3.5
// DetectRTC v1.3.6

// Open-Sourced: https://github.com/muaz-khan/DetectRTC

Expand Down Expand Up @@ -506,135 +506,118 @@
});

// via: https://github.com/diafygi/webrtc-ips
function DetectLocalIPAddress(callback) {
function DetectLocalIPAddress(callback, stream) {
if (!DetectRTC.isWebRTCSupported) {
return;
}

if (DetectRTC.isORTCSupported) {
return;
}

getIPs(function(ip) {
//local IPs
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
callback('Local: ' + ip);
}

//assume the rest are public IPs
else {
} else {
callback('Public: ' + ip);
}
});
}, stream);
}

//get the IP addresses associated with an account
function getIPs(callback) {
function getIPs(callback, stream) {
if (typeof document === 'undefined' || typeof document.getElementById !== 'function') {
return;
}

var ipDuplicates = {};

//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;

// bypass naive webrtc blocking using an iframe
if (!RTCPeerConnection) {
var iframe = document.getElementById('iframe');
if (!iframe) {
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
throw 'NOTE: you need to have an iframe in the page right above the script tag.';
return;
}
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}

// if still no RTCPeerConnection then it is not supported by the browser so just return
if (!RTCPeerConnection) {
return;
}

//minimal requirements for data connection
var mediaConstraints = {
optional: [{
RtpDataChannels: true
}]
};

//firefox already has a default stun server in about:config
// media.peerconnection.default_iceservers =
// [{"url": "stun:stun.services.mozilla.com"}]
var servers;
var peerConfig = null;

//add same stun server for chrome
if (useWebKit) {
servers = {
iceServers: [{
urls: 'stun:stun.services.mozilla.com'
if (DetectRTC.browser === 'Chrome' && DetectRTC.browser.version < 58) {
// todo: add support for older Opera
peerConfig = {
optional: [{
RtpDataChannels: true
}]
};
}

if (typeof DetectRTC !== 'undefined' && DetectRTC.browser.isFirefox && DetectRTC.browser.version <= 38) {
servers[0] = {
url: servers[0].urls
};
var servers = {
iceServers: [{
urls: 'stun:stun.l.google.com:19302'
}]
};

var pc = new RTCPeerConnection(servers, peerConfig);

if (stream) {
if (pc.addStream) {
pc.addStream(stream);
} else if (pc.addTrack && stream.getTracks()[0]) {
pc.addTrack(stream.getTracks()[0], stream);
}
}

//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);

function handleCandidate(candidate) {
//match just the IP address
var ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/;
var match = ipRegex.exec(candidate);
if (!match) {
console.warn('Could not match IP address in', candidate);
return;
}
var ipAddress = match[1];

//remove duplicates
if (ipDuplicates[ipAddress] === undefined) {
callback(ipAddress);
}

ipDuplicates[ipAddress] = true;
}

//listen for candidate events
// listen for candidate events
pc.onicecandidate = function(ice) {
//skip non-candidate events
if (ice.candidate) {
handleCandidate(ice.candidate.candidate);
}
};

//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() {});
// create data channel
if (!stream) {
try {
pc.createDataChannel('sctp', {});
} catch (e) {}
}

}, function() {});
// create an offer sdp
if (DetectRTC.isPromisesSupported) {
pc.createOffer().then(function(result) {
pc.setLocalDescription(result).then(afterCreateOffer);
});
} else {
pc.createOffer(function(result) {
pc.setLocalDescription(result, afterCreateOffer, function() {});
}, function() {});
}

//wait for a while to let everything done
setTimeout(function() {
//read candidate info from local description
function afterCreateOffer() {
var lines = pc.localDescription.sdp.split('\n');

lines.forEach(function(line) {
if (line.indexOf('a=candidate:') === 0) {
handleCandidate(line);
}
});
}, 1000);
}
}

var MediaDevices = [];
Expand All @@ -647,9 +630,14 @@
// Firefox 38+ seems having support of enumerateDevices
// Thanks @xdumaine/enumerateDevices
navigator.enumerateDevices = function(callback) {
navigator.mediaDevices.enumerateDevices().then(callback).catch(function() {
var enumerateDevices = navigator.mediaDevices.enumerateDevices();
if (enumerateDevices && enumerateDevices.then) {
navigator.mediaDevices.enumerateDevices().then(callback).catch(function() {
callback([]);
});
} else {
callback([]);
});
}
};
}

Expand Down
6 changes: 3 additions & 3 deletions DetectRTC.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "detectrtc",
"version": "1.3.4",
"version": "1.3.6",
"authors": [
{
"name": "Muaz Khan",
Expand Down
9 changes: 7 additions & 2 deletions dev/CheckDeviceSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
// Firefox 38+ seems having support of enumerateDevices
// Thanks @xdumaine/enumerateDevices
navigator.enumerateDevices = function(callback) {
navigator.mediaDevices.enumerateDevices().then(callback).catch(function() {
var enumerateDevices = navigator.mediaDevices.enumerateDevices();
if (enumerateDevices && enumerateDevices.then) {
navigator.mediaDevices.enumerateDevices().then(callback).catch(function() {
callback([]);
});
} else {
callback([]);
});
}
};
}

Expand Down
Loading

0 comments on commit a3cae23

Please sign in to comment.