Node.js version (not finished yet)

This commit is contained in:
Sefinek committed 2024-12-19 14:58:18 +01:00
1 parent 4469aa0816
commit c19d2dd7c1
18 files changed
+487 -562

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
const fs = require('node:fs');
const { CACHE_FILE, REPORT_INTERVAL } = require('../config.js').MAIN;
const log = require('./log.js');
const reportedIps = new Map();
const loadReportedIps = () => {
if (fs.existsSync(CACHE_FILE)) {
fs.readFileSync(CACHE_FILE, 'utf8')
.split('\n')
.forEach(line => {
const [ip, time] = line.split(' ');
if (ip && time) reportedIps.set(ip, Number(time));
});
log(0, `Loaded ${reportedIps.size} IPs from ${CACHE_FILE}`);
} else {
log(0, `${CACHE_FILE} does not exist. No data to load.`);
}
};
const saveReportedIps = () => fs.writeFileSync(CACHE_FILE, Array.from(reportedIps).map(([ip, time]) => `${ip} ${time}`).join('\n'), 'utf8');
const isIpReportedRecently = ip => {
const now = Math.floor(Date.now() / 1000);
return reportedIps.has(ip) && (now - reportedIps.get(ip) < REPORT_INTERVAL);
};
const markIpAsReported = ip => reportedIps.set(ip, Math.floor(Date.now() / 1000));
module.exports = { loadReportedIps, saveReportedIps, isIpReportedRecently, markIpAsReported };
+10
View File
@@ -0,0 +1,10 @@
const ipaddr = require('ipaddr.js');
module.exports = ip => {
const range = ipaddr.parse(ip).range();
return [
'unspecified', 'multicast', 'linkLocal', 'loopback', 'reserved', 'benchmarking',
'amt', 'broadcast', 'carrierGradeNat', 'private', 'as112', 'uniqueLocal',
'ipv4Mapped', 'rfc6145', '6to4', 'teredo', 'as112v6', 'orchid2', 'droneRemoteIdProtocolEntityTags',
].includes(range);
};
+10
View File
@@ -0,0 +1,10 @@
const levels = {
0: { method: 'log', label: '[INFO]' },
1: { method: 'warn', label: '[WARN]' },
2: { method: 'error', label: '[FAIL]' },
};
module.exports = (level, msg) => {
const { method, label } = levels[level] || { method: 'log', label: '[N/A]' };
console[method](`${label} ${msg}`);
};