Update fetchServerIP
This commit is contained in:
1 parent
cc6e256ca4
commit
d5921f19e7
3 files changed
+41
-42
No files matched your search
@@ -0,0 +1,40 @@
|
||||
const { networkInterfaces } = require('node:os');
|
||||
const axios = require('./axios.js');
|
||||
const isLocalIP = require('../utils/isLocalIP.js');
|
||||
const { REFRESHING_IP_ADDRESS } = require('../config.js').MAIN;
|
||||
|
||||
const ipAddrList = new Set();
|
||||
|
||||
const fetchIPv4Address = async () => {
|
||||
try {
|
||||
const { data } = await axios.get('https://api.sefinek.net/api/v2/ip');
|
||||
if (data?.success && data?.message) ipAddrList.add(data.message);
|
||||
} catch (err) {
|
||||
console.warn('Error fetching IPv4 address:', err.message);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchIPv6Address = () => {
|
||||
try {
|
||||
Object.values(networkInterfaces()).flat().forEach(({ address, internal }) => {
|
||||
if (!internal && address && !isLocalIP(address)) ipAddrList.add(address);
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn('Error fetching IPv6 address:', err.message);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchServerIPs = async () => {
|
||||
ipAddrList.clear();
|
||||
await fetchIPv4Address();
|
||||
fetchIPv6Address();
|
||||
};
|
||||
|
||||
(async () => {
|
||||
await fetchServerIPs();
|
||||
setInterval(fetchServerIPs, REFRESHING_IP_ADDRESS);
|
||||
|
||||
// console.debug(ipAddrList);
|
||||
})();
|
||||
|
||||
module.exports = () => Array.from(ipAddrList);
|
||||
Reference in new issue
Block a user