From f6774898b5d51df10b0583137c0adfa270316451 Mon Sep 17 00:00:00 2001 From: Sefinek Date: Sat, 18 Jan 2025 04:48:11 +0100 Subject: [PATCH] Quality fixes --- services/serverAddress.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/services/serverAddress.js b/services/serverAddress.js index c6c48fc..80b8723 100644 --- a/services/serverAddress.js +++ b/services/serverAddress.js @@ -2,12 +2,12 @@ const os = require('node:os'); const axios = require('./axios.js'); const isLocalIP = require('./isLocalIP.js'); -let ipAddressList = []; +const ipAddressList = new Set(); const fetchIPv4Address = async () => { try { const { data } = await axios.get('https://api.sefinek.net/api/v2/ip'); - if (data?.success && data?.message) ipAddressList.push(data.message); + if (data?.success && data?.message) ipAddressList.add(data.message); } catch (err) { console.warn('Error fetching IPv4 address:', err.message); } @@ -16,16 +16,18 @@ const fetchIPv4Address = async () => { const fetchIPv6Address = () => { const networkInterfaces = os.networkInterfaces(); - for (const interfaces of Object.values(networkInterfaces)) { - for (const details of interfaces) { + Object.values(networkInterfaces).forEach(interfaces => { + interfaces.forEach(details => { const ip = details.address; - if (!details.internal && ip && !isLocalIP(ip)) ipAddressList.push(ip); - } - } + if (!details.internal && ip && !isLocalIP(ip)) { + ipAddressList.add(ip); + } + }); + }); }; const fetchIPAddress = async () => { - ipAddressList = []; + ipAddressList.clear(); await fetchIPv4Address(); fetchIPv6Address(); }; @@ -33,6 +35,8 @@ const fetchIPAddress = async () => { (async () => { await fetchIPAddress(); setInterval(fetchIPAddress, 25 * 1000); + + // console.debug(ipAddressList); })(); -module.exports = () => [...ipAddressList]; \ No newline at end of file +module.exports = () => Array.from(ipAddressList); \ No newline at end of file