isIPBlockedOrReportedRecently

This commit is contained in:
Sefinek 2024-08-17 22:56:21 +02:00
parent 4c4288aee8
commit f8623056e9

View file

@ -10,8 +10,8 @@ const formatDelay = require('./scripts/formatDelay.js');
const log = require('./scripts/log.js'); const log = require('./scripts/log.js');
const COOLDOWN_MS = 2000; const COOLDOWN_MS = 2000;
const BLOCK_TIME_MS = 5 * 60 * 60 * 1000; // 5h const BLOCK_TIME_MS = 5 * 60 * 60 * 1000;
const REPORTED_IP_COOLDOWN_MS = 7 * 60 * 60 * 1000; // 7h const REPORTED_IP_COOLDOWN_MS = 7 * 60 * 60 * 1000;
const fetchBlockedIPs = async () => { const fetchBlockedIPs = async () => {
try { try {
@ -29,14 +29,14 @@ const fetchBlockedIPs = async () => {
} }
}; };
const isIPBlockedRecently = (ip, reportedIPs) => { const isIPBlockedOrReportedRecently = (ip, reportedIPs) => {
const lastBlock = reportedIPs.find(entry => entry.ip === ip && entry.action.includes('429')); const lastReportOrBlock = reportedIPs.find(entry => entry.ip === ip);
return lastBlock && (Date.now() - new Date(lastBlock.timestamp).getTime()) < BLOCK_TIME_MS; if (!lastReportOrBlock) return false;
};
const isIPReportedRecently = (ip, reportedIPs) => { const lastTimestamp = new Date(lastReportOrBlock.timestamp).getTime();
const lastReport = reportedIPs.find(entry => entry.ip === ip && entry.action === 'Reported'); const currentTime = Date.now();
return lastReport && (Date.now() - new Date(lastReport.timestamp).getTime()) < REPORTED_IP_COOLDOWN_MS;
return (currentTime - lastTimestamp) < REPORTED_IP_COOLDOWN_MS;
}; };
const reportIP = async (event, url, country, cycleErrorCounts) => { const reportIP = async (event, url, country, cycleErrorCounts) => {
@ -92,7 +92,7 @@ const reportIP = async (event, url, country, cycleErrorCounts) => {
const url = `${event.clientRequestHTTPHost}${event.clientRequestPath}`; const url = `${event.clientRequestHTTPHost}${event.clientRequestPath}`;
const country = event.clientCountryName; const country = event.clientCountryName;
if (isIPReportedRecently(ip, reportedIPs) || isIPBlockedRecently(ip, reportedIPs)) { if (isIPBlockedOrReportedRecently(ip, reportedIPs)) {
cycleSkippedCount++; cycleSkippedCount++;
continue; continue;
} }