且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编辑本地IP地址

更新时间:2023-02-22 14:45:09

有很多方法可以实现这一目标。如果你在初始函数期间只获得前3个octate,那就更好了。

There are many ways to achieve this. its better if you only get first 3 octate during your initial function itself.

但是下面也会做这个工作:

But following will also do the job:

var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})

/*Usage example*/
findIP.then(ip => console.log('your ip: ', ip.split('.')[0]+'.'+ip.split('.')[1]+'.'+ip.split('.')[2]+'.200')).catch(e => console.error(e))

编辑:
您可以使用webrtc更改实际代码,以便按以下方式提供所需的IP:

You can change actual code using webrtc to give the ip u needed as follows:

var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/\b(\d{1,3}\.){2}\d{1,3}\b/).forEach(r)}catch(e){}}})

findIP.then(ip => document.write('your ip: ', ip+'.200')).catch(e => console.error(e))