From 3402f99b2d346c333d6fee7cea42e2e501746bfc Mon Sep 17 00:00:00 2001 From: Sefinek Date: Sun, 10 Nov 2024 03:12:56 +0100 Subject: [PATCH] Quality fixes --- index.js | 10 +++++----- scripts/whitelist.js | 6 +++--- services/clientIp.js | 2 +- services/sefinekAPI.js | 31 +++++++++++++++---------------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 07a1ef4..be15dca 100644 --- a/index.js +++ b/index.js @@ -20,9 +20,9 @@ const fetchBlockedIPs = async () => { if (events) { const filtered = events.filter(x => x.ip !== clientIp.getAddress() && - !whitelist.subdomains.some(subdomain => x.clientRequestHTTPHost.includes(subdomain)) && // Subdomains - !whitelist.useragents.some(ua => x.userAgent.includes(ua)) && // User-agents - !whitelist.endpoints.some(endpoint => x.clientRequestPath.includes(endpoint))// Endpoints + !whitelist.subdomains.some(subdomain => x.clientRequestHTTPHost?.includes(subdomain)) && // Subdomains + !whitelist.userAgents.some(ua => x.userAgent?.includes(ua)) && // User-agents + !whitelist.endpoints.some(endpoint => x.clientRequestPath?.includes(endpoint)) // Endpoints ); log('log', `Fetched ${events.length} (filtered ${filtered.length}) events from Cloudflare`); @@ -173,8 +173,8 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount log('log', `- Reported IPs: ${cycleReportedCount}`); log('log', `- Total IPs processed: ${cycleProcessedCount}`); log('log', `- Skipped IPs: ${cycleSkippedCount}`); - log('log', `- Skipped due to Image Requests: ${cycleImageSkippedCount}`); - log('log', `- 429 Too Many Requests: ${cycleErrorCounts.blocked}`); + log('log', `- Skipped due to image requests: ${cycleImageSkippedCount}`); + log('log', `- Rate-limits: ${cycleErrorCounts.blocked}`); log('log', `- Other errors: ${cycleErrorCounts.otherErrors}`); log('log', '===================== End of Reporting Cycle ====================='); diff --git a/scripts/whitelist.js b/scripts/whitelist.js index 181bcbf..711c34c 100644 --- a/scripts/whitelist.js +++ b/scripts/whitelist.js @@ -1,5 +1,5 @@ const subdomains = ['api.', 'cdn.']; -const useragents = ['Chrome/129', 'Chrome/130', 'Chrome/131', 'Chrome/132', 'Chrome/133', 'StellaLauncher']; -const endpoints = ['/api/', '//video', '//js', '//images', '//imgs', 'favicon.ico']; +const userAgents = ['Chrome/129', 'Chrome/130', 'Chrome/131', 'Chrome/132', 'StellaLauncher', 'PrepareStella']; +const endpoints = ['/api/', '//video', '//js', '//images', '//imgs', 'favicon.ico', 'sitemap.xml', 'robots.txt']; -module.exports = { subdomains, useragents, endpoints }; \ No newline at end of file +module.exports = { subdomains, userAgents, endpoints }; \ No newline at end of file diff --git a/services/clientIp.js b/services/clientIp.js index cb38d95..2ed1b0c 100644 --- a/services/clientIp.js +++ b/services/clientIp.js @@ -13,7 +13,7 @@ const fetchIPAddress = async () => { log('error', 'Failed to retrieve your IP'); } } catch (err) { - log('error', `Error fetching your IP: ${err.message}`); + log('error', `Error fetching your IP: ${err.stack}`); } }; diff --git a/services/sefinekAPI.js b/services/sefinekAPI.js index 7bf8bf8..0e8b7bd 100644 --- a/services/sefinekAPI.js +++ b/services/sefinekAPI.js @@ -7,26 +7,27 @@ const whitelist = require('../scripts/whitelist.js'); const API_URL = `${process.env.SEFINEK_API_URL}/cloudflare-waf-abuseipdb/post`; module.exports = async () => { - const reportedIPs = readReportedIPs().filter(x => + const reportedIPs = (readReportedIPs() || []).filter(x => x.status === 'REPORTED' && x.ip !== clientIp.getAddress() && x.hostname !== 'blocklist.sefinek.net' && // Domain - !whitelist.subdomains.some(subdomain => x.clientRequestHTTPHost.includes(subdomain)) && // Subdomains - !whitelist.useragents.some(ua => x.userAgent.includes(ua)) && // User-agents - !whitelist.endpoints.some(endpoint => x.clientRequestPath.includes(endpoint)) && // Endpoints - !(/crawler|spider|bot/gi).test(x.useragent) && // Bots + !whitelist.subdomains.some(subdomain => x.clientRequestHTTPHost?.includes(subdomain)) && // Subdomains + !whitelist.userAgents.some(ua => x.userAgent?.includes(ua)) && // User-agents + !whitelist.endpoints.some(endpoint => x.clientRequestPath?.includes(endpoint)) && // Endpoints + !(/crawler|spider|bot/gi).test(x.userAgent) && !x.sefinekAPI ); + if (!reportedIPs.length) return; - const uniqueLogs = reportedIPs.reduce((acc, ip) => { - if (acc.seen.has(ip.ip)) return acc; - acc.seen.add(ip.ip); - acc.logs.push(ip); - return acc; - }, { seen: new Set(), logs: [] }).logs; + const seenIPs = new Set(); + const uniqueLogs = reportedIPs.filter(ip => { + if (seenIPs.has(ip.ip)) return false; + seenIPs.add(ip.ip); + return true; + }); - if (!uniqueLogs?.length) return log('log', 'No unique IPs to send to Sefinek API'); + if (!uniqueLogs.length) return log('log', 'No unique IPs to send to Sefinek API'); try { const res = await axios.post(API_URL, { @@ -34,7 +35,7 @@ module.exports = async () => { rayId: ip.rayId, ip: ip.ip, endpoint: ip.endpoint, - useragent: ip.useragent.replace(/"/g, ''), + userAgent: ip.userAgent?.replace(/"/g, ''), action: ip.action, country: ip.country, timestamp: ip.timestamp, @@ -45,8 +46,6 @@ module.exports = async () => { uniqueLogs.forEach(ip => updateSefinekAPIInCSV(ip.rayId, true)); } catch (err) { - if (err.response?.data?.code !== 'NO_VALID_OR_UNIQUE_IPS') { - log('error', `Failed to send logs to Sefinek API. Status: ${err.status}. Message: ${err.response?.data?.message || err.stack}`); - } + log('error', `Failed to send logs to Sefinek API. Status: ${err.response?.status}. Message: ${err.response?.data?.message || err.stack}`); } }; \ No newline at end of file