Last active
February 7, 2020 03:35
-
-
Save hitswa/f6d64f4b89dedf651a99f2515a30b912 to your computer and use it in GitHub Desktop.
functions to get enviornment details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getIP() { | |
detectIP().then((ip)=>{ return ip }).catch((error)=>{ console.log({error}); return null; }); | |
} | |
function getBrowser() { | |
var ua = navigator.userAgent, tem, | |
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; | |
if (/trident/i.test(M[1])) { | |
tem = /\brv[ :]+(\d+)/g.exec(ua) || []; | |
return 'IE ' + (tem[1] || ''); | |
} | |
if (M[1] === 'Chrome') { | |
tem = ua.match(/\b(OPR|Edge?)\/(\d+)/); | |
if (tem != null) return tem.slice(1).join(' ').replace('OPR', 'Opera').replace('Edg ', 'Edge '); | |
} | |
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; | |
if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]); | |
return M.join(' '); | |
} | |
function getOS() { | |
var userAgent = window.navigator.userAgent, | |
platform = window.navigator.platform, | |
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'], | |
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'], | |
iosPlatforms = ['iPhone', 'iPad', 'iPod'], | |
os = null; | |
if (macosPlatforms.indexOf(platform) !== -1) { | |
os = 'Mac OS'; | |
} else if (iosPlatforms.indexOf(platform) !== -1) { | |
os = 'iOS'; | |
} else if (windowsPlatforms.indexOf(platform) !== -1) { | |
os = 'Windows'; | |
} else if (/Android/.test(userAgent)) { | |
os = 'Android'; | |
} else if (!os && /Linux/.test(platform)) { | |
os = 'Linux'; | |
} | |
return os; | |
} | |
function detectIP() { | |
return new Promise((resolve,reject) => { | |
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome | |
var pc = new RTCPeerConnection({ iceServers: [] }), noop = function () { }; | |
pc.createDataChannel(""); //create a bogus data channel | |
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description | |
pc.onicecandidate = function (ice) { //listen for candidate events | |
if (!ice || !ice.candidate || !ice.candidate.candidate) return; | |
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.target.localDescription.sdp)[0]; | |
pc.onicecandidate = noop; | |
ipAddress = myIP; // here comes your IP Address | |
if( ipAddress !== undefined ) { | |
resolve(ipAddress); | |
} else { | |
reject('Unable to fetch IP Address'); | |
} | |
}; | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment