Get the ip of the server

Since GetConvar("web_baseUrl", "") does not return the IP address, I created a small script that utilizes the api.ipify.org API to obtain the server’s IP. This approach is often used for authentication purposes or similar tasks. I believe it won’t encounter API blocking issues, as the limit seems to be 20 requests per minute.
but her is the server side js code

const https = require(‘https’);

// Function to get external IP address
function getExternalIP() {
https.get(‘https://api.ipify.org?format=json’, (resp) => {
let data = ‘’;

    // A chunk of data has been received.
    resp.on('data', (chunk) => {
        data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on('end', () => {
        try {
            const ipInfo = JSON.parse(data);
            console.log('Server External IP Address:', ipInfo.ip);
        } catch (e) {
            console.error('Error parsing IP information:', e);
        }
    });

}).on("error", (err) => {
    console.error("Error fetching IP address:", err.message);
});

}

the lua code

// Call the function
getExternalIP();
– Function to get external IP address
function getExternalIP()
PerformHttpRequest(‘https://api.ipify.org?format=json’, function(err, text, headers)
if err == 0 then
local ipInfo = json.decode(text)
if ipInfo then
print(‘Server External IP Address:’, ipInfo.ip)
else
print(‘Error: Unable to parse IP information.’)
end
else
print(‘Error fetching IP address:’, err)
end
end, ‘GET’, ‘’)
end

– Call the function
getExternalIP()

2 Likes