From 3e818103761625b65494ffbd93842bbc949d3088 Mon Sep 17 00:00:00 2001 From: Sefinek Date: Tue, 10 Sep 2024 17:58:41 +0200 Subject: [PATCH] Bug fixes --- index.js | 19 +++++++++---------- scripts/csv.js | 6 +++--- scripts/sefinekAPI.js | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 4a25469..aa1054e 100644 --- a/index.js +++ b/index.js @@ -29,8 +29,12 @@ const fetchBlockedIPs = async () => { } }; -const isIPReportedRecently = (ip, reportedIPs) => { - const lastReport = reportedIPs.find(entry => entry.ip === ip && (entry.action === 'REPORTED' || entry.action === 'TOO_MANY_REQUESTS')); +const isIPReportedRecently = (rayId, ip, reportedIPs) => { + const lastReport = reportedIPs.find(entry => + (entry.rayId === rayId || entry.ip === ip) && + (entry.action === 'REPORTED' || entry.action === 'TOO_MANY_REQUESTS') + ); + if (lastReport) { const lastTimestamp = new Date(lastReport.timestamp).getTime(); const currentTime = Date.now(); @@ -75,7 +79,7 @@ const reportIP = async (event, country, hostname, endpoint, userAgent, cycleErro } catch (err) { if (err.response?.status === 429) { logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, 'TOO_MANY_REQUESTS'); - log('log', `Rate limited (429) while reporting ${event.clientIP}; URI: ${uri}`); + log('log', `Rate limited while reporting ${event.clientIP} (${event.rayName}); Endpoint: ${endpoint}`); cycleErrorCounts.blocked++; } else { log('error', `Error ${err.response?.status} while reporting ${event.clientIP}; URI: ${uri}; (${err.response?.data})`); @@ -125,11 +129,8 @@ const reportIP = async (event, country, hostname, endpoint, userAgent, cycleErro for (const event of blockedIPEvents) { cycleProcessedCount++; const ip = event.clientIP; - const hostname = event.clientRequestHTTPHost; - const endpoint = event.clientRequestPath; - const country = event.clientCountryName; - const { recentlyReported, timeDifference } = isIPReportedRecently(ip, reportedIPs); + const { recentlyReported, timeDifference } = isIPReportedRecently(event.rayName, ip, reportedIPs); if (recentlyReported) { const hoursAgo = Math.floor(timeDifference / (1000 * 60 * 60)); const minutesAgo = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)); @@ -142,8 +143,6 @@ const reportIP = async (event, country, hostname, endpoint, userAgent, cycleErro if (isImageRequest(event.clientRequestPath)) { cycleImageSkippedCount++; if (!wasImageRequestLogged(ip, reportedIPs)) { - logToCSV(event.rayName, ip, country, hostname, endpoint, null, 'SKIPPED_IMAGE_REQUEST'); - if (imageRequestLogged) continue; log('log', 'Skipping image requests in this cycle...'); imageRequestLogged = true; @@ -152,7 +151,7 @@ const reportIP = async (event, country, hostname, endpoint, userAgent, cycleErro continue; } - const wasReported = await reportIP(event, country, hostname, endpoint, event.userAgent, cycleErrorCounts); + const wasReported = await reportIP(event, event.clientCountryName, event.clientRequestHTTPHost, event.clientRequestPath, event.userAgent, cycleErrorCounts); if (wasReported) { cycleReportedCount++; await new Promise(resolve => setTimeout(resolve, SUCCESS_COOLDOWN_MS)); diff --git a/scripts/csv.js b/scripts/csv.js index 6c1787a..74e141c 100644 --- a/scripts/csv.js +++ b/scripts/csv.js @@ -36,11 +36,11 @@ const readReportedIPs = () => { .slice(1) .filter(line => line.trim() !== '') .map(line => { - const parts = line.match(/(".*?"|[^",]+)(?=\s*,|\s*$)/g); + const parts = line.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/g); if (!parts || parts.length < 9) return null; return { - timestamp: new Date(parts[0]), + timestamp: Date.parse(parts[0]), rayId: parts[1], ip: parts[2], country: parts[3], @@ -48,7 +48,7 @@ const readReportedIPs = () => { endpoint: parts[5], useragent: parts[6].replace(/(^"|"$)/g, ''), action: parts[7], - sefinekAPI: parts[8] + sefinekAPI: parts[8] === 'true' }; }) .filter(item => item !== null); diff --git a/scripts/sefinekAPI.js b/scripts/sefinekAPI.js index 865ffe7..4d8c9f2 100644 --- a/scripts/sefinekAPI.js +++ b/scripts/sefinekAPI.js @@ -5,7 +5,7 @@ const log = require('./log.js'); const SEFINEK_API_URL = process.env.SEFINEK_API_URL || `${process.env.NODE_ENV === 'production' ? 'https://api.sefinek.net' : 'http://127.0.0.1:4010'}/api/v2/cloudflare-waf-abuseipdb/post`; module.exports = async () => { - const reportedIPs = readReportedIPs().filter(ip => ip.action === 'REPORTED' && ip.sefinekAPI === 'false'); + const reportedIPs = readReportedIPs().filter(ip => ip.action === 'REPORTED' && !ip.sefinekAPI); if (reportedIPs.length === 0) return log('log', 'No IPs with `action Reported` and `SefinekAPI false` to send to Sefinek API'); const uniqueLogs = reportedIPs.reduce((acc, ip) => {