SoFunction
Updated on 2025-03-03

Get an instance of the native IP address (JavaScript / )

--web ClientJavaScript

<!-- Call method --> 

<body onload="checkCookie()"></body> 
function getYourIP()

{
const RTCPeerConnection =  ||  || ;
if (RTCPeerConnection) (function()
{
const rtc = new RTCPeerConnection({iceServers: []});
if (1 || )
{
('', {reliable: false});
}
 = function(evt)
{
if () grepSDP(`a=${}`);
};
(function(offerDesc)
{
grepSDP();
(offerDesc);
}, function(e) {('offer failed', e);});
const addrs = (null);
addrs['0.0.0.0'] = false;
function updateDisplay(newAddr)
{
if (newAddr in addrs) return;
addrs[newAddr] = true;
const displayAddrs = (addrs).filter(function(k) {return addrs[k];});
for (let i = 0; i < ; i++)
{
if (displayAddrs[i].length > 16)
{
(i, 1);
i--;
}
}
('Your IP: ', displayAddrs[0]);
}
function grepSDP(sdp)
{
('\r\n').forEach(function(line, index, arr)
{
if (~('a=candidate'))
{
const parts = (' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
}
else if (~('c='))
{
const parts = (' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})();
else
{
('Please use mainstream browser: chrome, firefox, opera, safari');
}
}

--webServer side

const os = require('os');
 

 =
{
getLocalIP : function()
{
const eth0 = ().eth0;
let localhost = null;
for (let i = 0; i < ; i++)
{
if (eth0[i].family == 'IPv4')
{
localhost = eth0[i].address;
}
}
return localhost;
},
};

The above example of obtaining the native IP address (JavaScript / ) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.