in tools/CCPConnectivityTestTools/scripts/detectRTC.js [546:652]
function getIPs(callback, stream) {
if (typeof document === 'undefined' || typeof document.getElementById !== 'function') {
return;
}
var ipDuplicates = {};
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
if (!RTCPeerConnection) {
var iframe = document.getElementById('iframe');
if (!iframe) {
return;
}
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
}
if (!RTCPeerConnection) {
return;
}
var peerConfig = null;
if (DetectRTC.browser === 'Chrome' && DetectRTC.browser.version < 58) {
// todo: add support for older Opera
peerConfig = {
optional: [{
RtpDataChannels: true
}]
};
}
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);
}
}
function handleCandidate(candidate) {
if (!candidate) {
callback(); // Pass nothing to tell that ICE-gathering-ended
return;
}
var match = regexIpv4.exec(candidate);
if (!match) {
return;
}
var ipAddress = match[1];
const isPublic = (candidate.match(regexIpv4Local)),
isIpv4 = true;
if (ipDuplicates[ipAddress] === undefined) {
callback(ipAddress, isPublic, isIpv4);
}
ipDuplicates[ipAddress] = true;
}
// listen for candidate events
pc.onicecandidate = function(event) {
if (event.candidate && event.candidate.candidate) {
handleCandidate(event.candidate.candidate);
} else {
handleCandidate(); // Pass nothing to tell that ICE-gathering-ended
}
};
// create data channel
if (!stream) {
try {
pc.createDataChannel('sctp', {});
} catch (e) {}
}
// 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() {});
}
function afterCreateOffer() {
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line) {
if (line && line.indexOf('a=candidate:') === 0) {
handleCandidate(line);
}
});
}
}