diff --git a/index.js b/index.js index e007ecf..07a1ef4 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const headers = require('./scripts/headers.js'); const { logToCSV, readReportedIPs, wasImageRequestLogged } = require('./services/csv.js'); const formatDelay = require('./scripts/formatDelay.js'); const clientIp = require('./services/clientIp.js'); -const whitelist = require('./whitelist.js'); +const whitelist = require('./scripts/whitelist.js'); const log = require('./scripts/log.js'); const fetchBlockedIPs = async () => { @@ -18,8 +18,15 @@ const fetchBlockedIPs = async () => { const { data, status } = await axios.post('https://api.cloudflare.com/client/v4/graphql', PAYLOAD(), { headers: headers.CLOUDFLARE }); const events = data?.data?.viewer?.zones[0]?.firewallEventsAdaptive; if (events) { - log('log', `Fetched ${events.length} events from Cloudflare`); - return 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 + ); + + log('log', `Fetched ${events.length} (filtered ${filtered.length}) events from Cloudflare`); + return filtered; } else { throw new Error(`Failed to retrieve data from Cloudflare (status ${status}); ${JSON.stringify(data?.errors)}`); } @@ -131,7 +138,7 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount continue; } - if (whitelist.includes(event.clientRequestPath)) return log('log', `Skipping ${event.clientRequestPath}...`); + if (whitelist.endpoints.includes(event.clientRequestPath)) return log('log', `Skipping ${event.clientRequestPath}...`); const reportedIPs = readReportedIPs(); const { recentlyReported, timeDifference, reason } = isIPReportedRecently(event.rayName, ip, reportedIPs); diff --git a/scripts/whitelist.js b/scripts/whitelist.js new file mode 100644 index 0000000..181bcbf --- /dev/null +++ b/scripts/whitelist.js @@ -0,0 +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']; + +module.exports = { subdomains, useragents, endpoints }; \ No newline at end of file diff --git a/services/sefinekAPI.js b/services/sefinekAPI.js index 8f549c3..7bf8bf8 100644 --- a/services/sefinekAPI.js +++ b/services/sefinekAPI.js @@ -2,6 +2,7 @@ const { axios } = require('./axios.js'); const { readReportedIPs, updateSefinekAPIInCSV } = require('./csv.js'); const log = require('../scripts/log.js'); const clientIp = require('./clientIp.js'); +const whitelist = require('../scripts/whitelist.js'); const API_URL = `${process.env.SEFINEK_API_URL}/cloudflare-waf-abuseipdb/post`; @@ -9,11 +10,10 @@ module.exports = async () => { const reportedIPs = readReportedIPs().filter(x => x.status === 'REPORTED' && x.ip !== clientIp.getAddress() && - !x.endpoint.includes('/api') && // API requests - !['//video', '//js', '//images', '//imgs', 'favicon.ico'].some(endpoint => x.endpoint.includes(endpoint)) && // Endpoints - ['api.', 'cdn.'].some(prefix => x.hostname.startsWith(prefix)) && // Domains x.hostname !== 'blocklist.sefinek.net' && // Domain - !['Chrome/129', 'Chrome/130', 'Chrome/131', 'Chrome/132', 'Chrome/133', 'StellaLauncher'].some(agent => x.useragent.includes(agent)) && // User-agents + !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 !x.sefinekAPI ); @@ -39,9 +39,7 @@ module.exports = async () => { country: ip.country, timestamp: ip.timestamp, })), - }, { - headers: { 'Authorization': process.env.SEFINEK_API_SECRET }, - }); + }, { headers: { 'Authorization': process.env.SEFINEK_API_SECRET } }); log('log', `Successfully sent ${uniqueLogs.length} logs to Sefinek API. Status: ${res.status}`); diff --git a/whitelist.js b/whitelist.js deleted file mode 100644 index 2252be2..0000000 --- a/whitelist.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = [ - '/weryfikacja', - '/verification', - '/download', -]; \ No newline at end of file