From 8d8cbe9303128b481e950c47393089b82db46824 Mon Sep 17 00:00:00 2001 From: Sefinek Date: Mon, 10 Mar 2025 23:38:30 +0100 Subject: [PATCH] 1.5.0: CronJob for Sefinek API --- config.default.js | 4 ++-- index.js | 5 ++--- package-lock.json | 4 ++-- package.json | 2 +- services/SefinekAPI.js | 8 ++++---- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/config.default.js b/config.default.js index 11ecaae..6293137 100644 --- a/config.default.js +++ b/config.default.js @@ -34,8 +34,8 @@ exports.CONFIG = { // Secret key for api.sefinek.net SECRET_TOKEN: '', - // How often should the log (reported_ips.csv) be analyzed and sent to the Sefinek API? In hours. - INTERVAL: 60 * 60 * 1000, // Frequency for analyzing and submitting logs to the Sefinek API + // How often should the log (reported_ips.csv) be analyzed and sent to the Sefinek API? + REPORT_SCHEDULE: '0 */1 * * *', }, }; diff --git a/index.js b/index.js index b1777da..b6cf8a9 100644 --- a/index.js +++ b/index.js @@ -160,9 +160,8 @@ const cron = async () => { log(0, 'Loading data, please wait...'); // Sefinek API - // await SefinekAPI(); - if (CONFIG.SEFINEK_API.REPORT_TO_SEFIN_API && CONFIG.SEFINEK_API.INTERVAL && CONFIG.SEFINEK_API.SECRET_TOKEN) { - setInterval(SefinekAPI, CONFIG.SEFINEK_API.INTERVAL); + if (CONFIG.SEFINEK_API.REPORT_TO_SEFIN_API && CONFIG.SEFINEK_API.SECRET_TOKEN && CONFIG.SEFINEK_API.REPORT_SCHEDULE) { + new CronJob(CONFIG.SEFINEK_API.REPORT_SCHEDULE, SefinekAPI, null, true, 'UTC'); } // Ready diff --git a/package-lock.json b/package-lock.json index a30a599..db5888a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cf-waf-to-abuseipdb", - "version": "1.4.1", + "version": "1.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cf-waf-to-abuseipdb", - "version": "1.4.1", + "version": "1.5.0", "license": "MIT", "dependencies": { "axios": "^1.8.2", diff --git a/package.json b/package.json index 4c3c0a9..1fd5071 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cf-waf-to-abuseipdb", - "version": "1.4.1", + "version": "1.5.0", "description": "Node.js script for automatically reporting incidents to AbuseIPDB using data obtained from Cloudflare WAF.", "homepage": "https://github.com/sefinek/Cloudflare-WAF-To-AbuseIPDB", "bugs": { diff --git a/services/SefinekAPI.js b/services/SefinekAPI.js index a857ae5..ab5274c 100644 --- a/services/SefinekAPI.js +++ b/services/SefinekAPI.js @@ -6,7 +6,7 @@ const { SEFINEK_API } = require('../config.js').CONFIG; module.exports = async () => { const reportedIPs = (readReportedIPs() || []).filter(x => x.status === 'REPORTED' && x.ip !== fetchServerIP() && !x.sefinekAPI); - if (!reportedIPs.length) return; + if (!reportedIPs.length) return log(0, 'Sefinek API: No data to report'); const seenIPs = new Set(); const uniqueLogs = reportedIPs.filter(ip => { @@ -15,7 +15,7 @@ module.exports = async () => { return true; }); - if (!uniqueLogs.length) return log(0, 'No unique IPs to send to Sefinek API'); + if (!uniqueLogs.length) return log(0, 'Sefinek API: No unique IPs to send'); try { // http://127.0.0.1:4010/api/v2/cloudflare-waf-abuseipdb @@ -31,13 +31,13 @@ module.exports = async () => { })), }, { headers: { 'Authorization': SEFINEK_API.SECRET_TOKEN } }); - log(0, `Successfully sent ${uniqueLogs.length} logs to Sefinek API! Status: ${res.status}`); + log(0, `Sefinek API: Successfully sent ${uniqueLogs.length} logs! Status: ${res.status}`); uniqueLogs.forEach(ip => updateSefinekAPIInCSV(ip.rayId, true)); } catch (err) { if (!err.response?.data?.message?.includes('No valid or unique')) { const msg = err.response?.data?.message[0] || err.response?.data?.message || err.message; - log(2, `Failed to send logs to Sefinek API! Status: ${err.response?.status ?? 'Unknown'}; Message: ${typeof msg === 'object' ? JSON.stringify(msg) : msg}`); + log(2, `Sefinek API: Failed to send logs! Status: ${err.response?.status ?? 'Unknown'}; Message: ${typeof msg === 'object' ? JSON.stringify(msg) : msg}`); } } }; \ No newline at end of file