admin管理员组文章数量:1418147
It seems impossible to get your IP address with pure javascript without recurring to external services or server side code.
My question is: why didn't the language implement this feature natively? After all, the browser should know "its" IP address.
It seems impossible to get your IP address with pure javascript without recurring to external services or server side code.
My question is: why didn't the language implement this feature natively? After all, the browser should know "its" IP address.
Share Improve this question edited Sep 3, 2015 at 19:13 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Sep 3, 2015 at 19:05 friolfriol 7,1064 gold badges49 silver badges82 bronze badges 4- The browser knows it's IP address, sure. Why do you need it? You can't get it from the browser for security reasons. – gen_Eric Commented Sep 3, 2015 at 19:14
- 1 Actually, you can get the user's IP address with JavaScript. WebRTC has a security flaw (read: feature) that can get both your external and internal IPs. Have a gander at ipleak, it uses code based off github./diafygi/webrtc-ips – gen_Eric Commented Sep 3, 2015 at 19:15
- 1 My question was simpler: why doesn't native Javascript have a "getMyIP()" function? – friol Commented Sep 4, 2015 at 5:47
-
I'd say it's just for security/privacy. Though, with the code from github./diafygi/webrtc-ips, JavaScript basically does have a
getMyIP()
function. – gen_Eric Commented Sep 4, 2015 at 13:10
1 Answer
Reset to default 7You actually can the user's IP address using vanilla JavaScript. WebRTC has a security flaw (read: feature) that lets get both the user's external and even internal (LAN) IP addresses.
Have a look at this GitHub project: https://github./diafygi/webrtc-ips
//get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {};
//patibility 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){
//NOTE: you need to have an iframe in the page right above the script tag
//
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
//<script>...getIPs called in here...
//
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
var servers = {iceServers: [{urls: "stun:stun.services.mozilla."}]};
//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate){
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_regex.exec(candidate)[1];
//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
//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(){});
}, function(){});
//wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});
Check out the demo: https://diafygi.github.io/webrtc-ips/
本文标签: network programmingNative javascript way to get user39s IP addressStack Overflow
版权声明:本文标题:network programming - Native javascript way to get user's IP address - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745278903a2651327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论