Skip to content

Commit

Permalink
v1.3.0: Refactoring of structure, updated src/index.js to remove getI…
Browse files Browse the repository at this point in the history
…pArray duplicate getFirstIp, which now calls getIpArray instead.
  • Loading branch information
joeymalvinni committed Jan 22, 2021
1 parent 7a68f78 commit fee20f1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 156 deletions.
114 changes: 36 additions & 78 deletions browser/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@
useWebKit = !!win.webkitRTCPeerConnection;
}

var mediaConstraints = getConstraints()

var servers = getServers()

var pc = new RTCPeerConnection(servers, mediaConstraints);
var pc = new RTCPeerConnection(getServers(), getConstraints());

function handleCandidate(candidate){
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
if(regex.exec(candidate) !== null){
var ip_addr = regex.exec(candidate)[0];
if(validateIp(ip_addr)) finalArray.push(ip_addr)
if(validateIp(ip_addr)) finalArray.push(ip_addr)
}
}

pc.onicecandidate = function(ice){
if(ice.candidate) handleCandidate(ice.candidate.candidate);
};

pc.createDataChannel("ip");
pc.createDataChannel("fake_data_channel");

pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
Expand All @@ -47,79 +43,41 @@
let truncatedArray = removeDuplicates(finalArray)
callback(truncatedArray)
}, 100);
}

function getFirstIp(callback){
let ips = []
var RTCPeerConnection = getPeerConn()
var useWebKit = !!window.webkitRTCPeerConnection;

if(!RTCPeerConnection){
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
function getServers(){
return {
iceServers: [{
urls: ["stun:stun.l.google.com:19302?transport=udp"]
}]
};
}

var mediaConstraints = getConstraints()

var servers = getServers()

var pc = new RTCPeerConnection(servers, mediaConstraints);

function handleCandidate(candidate){
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
if(regex.exec(candidate) !== null){
var ip_addr = regex.exec(candidate)[0];
if(validateIp(ip_addr)) ips.push(ip_addr);
}

function getConstraints(){
return {
optional: [{RtpDataChannels: true}]
};
}

function getPeerConn(){
return window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
}

function validateIp(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return (true)
}
return (false)
}

function removeDuplicates(array){
return [...new Set(array)];
}

pc.onicecandidate = function(ice){
if(ice.candidate) handleCandidate(ice.candidate.candidate);
};

pc.createDataChannel("ip");

pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});

setTimeout(function(){
var lines = pc.localDescription.sdp.split('\n');

lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
callback(ips[0])
}, 100);
}

function getServers(){
return { iceServers: [{ urls: ["stun:stun.l.google.com:19302?transport=udp"] }] };
}

function getConstraints(){
return {
optional: [{RtpDataChannels: true}]
};
}

function getPeerConn(){
return window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
}

function validateIp(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return (true)
}
return (false)
}

function removeDuplicates(array){
return [...new Set(array)];
function getFirstIp(callback){
getIpArray(function(ips){
callback(ips[0] || null)
})
}

function getIpTypes(callback){
Expand All @@ -128,13 +86,13 @@
ips.forEach(ip=>{
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
finalIpArray.push({ ip: ip, type: 'local'})
} else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)){
} else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)) {
finalIpArray.push({ ip: ip, type: 'IPv6'})
} else {
finalIpArray.push({ ip: ip, type: 'public'})
}
})
callback(finalIpArray)
})
callback(finalIpArray)
})
}
},{}]},{},[1]);
114 changes: 36 additions & 78 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ function getIpArray(callback){
useWebKit = !!win.webkitRTCPeerConnection;
}

var mediaConstraints = getConstraints()

var servers = getServers()

var pc = new RTCPeerConnection(servers, mediaConstraints);
var pc = new RTCPeerConnection(getServers(), getConstraints());

function handleCandidate(candidate){
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
if(regex.exec(candidate) !== null){
var ip_addr = regex.exec(candidate)[0];
if(validateIp(ip_addr)) finalArray.push(ip_addr)
if(validateIp(ip_addr)) finalArray.push(ip_addr)
}
}

pc.onicecandidate = function(ice){
if(ice.candidate) handleCandidate(ice.candidate.candidate);
};

pc.createDataChannel("ip");
pc.createDataChannel("fake_data_channel");

pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
Expand All @@ -46,79 +42,41 @@ function getIpArray(callback){
let truncatedArray = removeDuplicates(finalArray)
callback(truncatedArray)
}, 100);
}

function getFirstIp(callback){
let ips = []
var RTCPeerConnection = getPeerConn()
var useWebKit = !!window.webkitRTCPeerConnection;

if(!RTCPeerConnection){
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
function getServers(){
return {
iceServers: [{
urls: ["stun:stun.l.google.com:19302?transport=udp"]
}]
};
}

var mediaConstraints = getConstraints()

var servers = getServers()

var pc = new RTCPeerConnection(servers, mediaConstraints);

function handleCandidate(candidate){
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
if(regex.exec(candidate) !== null){
var ip_addr = regex.exec(candidate)[0];
if(validateIp(ip_addr)) ips.push(ip_addr);
}

function getConstraints(){
return {
optional: [{RtpDataChannels: true}]
};
}

function getPeerConn(){
return window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
}

function validateIp(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return (true)
}
return (false)
}

function removeDuplicates(array){
return [...new Set(array)];
}

pc.onicecandidate = function(ice){
if(ice.candidate) handleCandidate(ice.candidate.candidate);
};

pc.createDataChannel("ip");

pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});

setTimeout(function(){
var lines = pc.localDescription.sdp.split('\n');

lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
callback(ips[0])
}, 100);
}

function getServers(){
return { iceServers: [{ urls: ["stun:stun.l.google.com:19302?transport=udp"] }] };
}

function getConstraints(){
return {
optional: [{RtpDataChannels: true}]
};
}

function getPeerConn(){
return window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
}

function validateIp(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return (true)
}
return (false)
}

function removeDuplicates(array){
return [...new Set(array)];
function getFirstIp(callback){
getIpArray(function(ips){
callback(ips[0] || null)
})
}

function getIpTypes(callback){
Expand All @@ -127,12 +85,12 @@ function getIpTypes(callback){
ips.forEach(ip=>{
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
finalIpArray.push({ ip: ip, type: 'local'})
} else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)){
} else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/)) {
finalIpArray.push({ ip: ip, type: 'IPv6'})
} else {
finalIpArray.push({ ip: ip, type: 'public'})
}
})
callback(finalIpArray)
})
callback(finalIpArray)
})
}

0 comments on commit fee20f1

Please sign in to comment.