From e1bfd289fdefeadadb02c9f0bb6bf8d616bcdeee Mon Sep 17 00:00:00 2001 From: Sefinek Date: Tue, 24 Dec 2024 23:47:58 +0100 Subject: [PATCH] Quality fixes --- default.config.js | 4 ++-- index.js | 41 +++++++++++++++++++++-------------------- package.json | 8 ++++---- services/axios.js | 4 ++-- utils/cache.js | 18 +++++++++--------- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/default.config.js b/default.config.js index 2765a57..d515d7a 100644 --- a/default.config.js +++ b/default.config.js @@ -1,8 +1,8 @@ exports.MAIN = { // Server - LOG_FILE: '/var/log/ufw.log', + UFW_FILE: '/var/log/ufw.log', CACHE_FILE: '/tmp/ufw-abuseipdb-reporter.cache', - SERVER_IDENTIFIER: null, + SERVER_ID: null, // Reporting ABUSEIPDB_API_KEY: '', diff --git a/index.js b/index.js index 046b40f..1be4652 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,16 @@ const fs = require('node:fs'); const chokidar = require('chokidar'); const isLocalIP = require('./utils/isLocalIP.js'); -const { reportedIps, loadReportedIps, saveReportedIps, isIpReportedRecently, markIpAsReported } = require('./utils/cache.js'); +const { reportedIPs, loadReportedIPs, saveReportedIPs, isIPReportedRecently, markIPAsReported } = require('./utils/cache.js'); const log = require('./utils/log.js'); const axios = require('./services/axios.js'); const config = require('./config.js'); const { version } = require('./package.json'); -const { LOG_FILE, ABUSEIPDB_API_KEY, SERVER_IDENTIFIER } = config.MAIN; +const { UFW_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN; let fileOffset = 0; -const reportToAbuseIpDb = async (ip, categories, comment) => { +const reportToAbuseIPDb = async (ip, categories, comment) => { try { const { data } = await axios.post('https://api.abuseipdb.com/api/v2/report', new URLSearchParams({ ip, categories, comment }), { headers: { 'Key': ABUSEIPDB_API_KEY }, @@ -58,8 +58,8 @@ const processLogLine = async line => { return; } - if (isIpReportedRecently(srcIp)) { - const lastReportedTime = reportedIps.get(srcIp); + if (isIPReportedRecently(srcIp)) { + const lastReportedTime = reportedIPs.get(srcIp); const elapsedTime = Math.floor(Date.now() / 1000 - lastReportedTime); const days = Math.floor(elapsedTime / 86400); @@ -79,27 +79,30 @@ const processLogLine = async line => { } const categories = config.DETERMINE_CATEGORIES(proto, dpt); - const comment = config.REPORT_COMMENT(match.timestamp, srcIp, match.dstIp, proto, match.spt, dpt, match.ttl, match.len, match.tos, SERVER_IDENTIFIER); + const comment = config.REPORT_COMMENT(match.timestamp, srcIp, match.dstIp, proto, match.spt, dpt, match.ttl, match.len, match.tos, SERVER_ID); log(0, `Reporting IP ${srcIp} (${proto} ${dpt}) with categories: ${categories}`); - if (await reportToAbuseIpDb(srcIp, categories, comment)) { - markIpAsReported(srcIp); - saveReportedIps(); + if (await reportToAbuseIPDb(srcIp, categories, comment)) { + markIPAsReported(srcIp); + saveReportedIPs(); } }; -const startMonitoring = () => { - loadReportedIps(); +(async () => { + log(0, `* Version: ${version}`); + log(0, `* Repository: ${GITHUB_REPO}`); - if (!fs.existsSync(LOG_FILE)) { - log(2, `Log file ${LOG_FILE} does not exist.`); + loadReportedIPs(); + + if (!fs.existsSync(UFW_FILE)) { + log(2, `Log file ${UFW_FILE} does not exist.`); return; } - fileOffset = fs.statSync(LOG_FILE).size; + fileOffset = fs.statSync(UFW_FILE).size; - chokidar.watch(LOG_FILE, { persistent: true, ignoreInitial: true }) + chokidar.watch(UFW_FILE, { persistent: true, ignoreInitial: true }) .on('change', path => { const stats = fs.statSync(path); if (stats.size < fileOffset) { @@ -114,8 +117,6 @@ const startMonitoring = () => { }); }); - log(0, `==================== Version ${version} ====================`); - log(0, `Ready! Now monitoring: ${LOG_FILE}`); -}; - -startMonitoring(); \ No newline at end of file + log(0, '====================================================================='); + log(0, `Ready! Now monitoring: ${UFW_FILE}`); +})(); \ No newline at end of file diff --git a/package.json b/package.json index 8211d18..e34f6e5 100644 --- a/package.json +++ b/package.json @@ -22,13 +22,13 @@ "test": "echo \"Error: no test specified\" && exit 1", "up": "ncu -u && npm install && npm update && npm audit fix" }, - "devDependencies": { - "@eslint/js": "^9.17.0", - "globals": "^15.14.0" - }, "dependencies": { "axios": "^1.7.9", "chokidar": "^4.0.3", "ipaddr.js": "^2.2.0" + }, + "devDependencies": { + "@eslint/js": "^9.17.0", + "globals": "^15.14.0" } } diff --git a/services/axios.js b/services/axios.js index f33a5ab..3534d7a 100644 --- a/services/axios.js +++ b/services/axios.js @@ -1,8 +1,8 @@ const axios = require('axios'); -const { version, homepage } = require('../config.js'); +const { version } = require('../config.js'); axios.defaults.headers.common = { - 'User-Agent': `Mozilla/5.0 (compatible; UFW-AbuseIPDB-Reporter/${version}; +${homepage})`, + 'User-Agent': `Mozilla/5.0 (compatible; UFW-AbuseIPDB-Reporter/${version}; +https://github.com/sefinek/UFW-AbuseIPDB-Reporter)`, 'Accept': 'application/json', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', diff --git a/utils/cache.js b/utils/cache.js index 42a5ac7..2538577 100644 --- a/utils/cache.js +++ b/utils/cache.js @@ -2,29 +2,29 @@ const fs = require('node:fs'); const { CACHE_FILE, REPORT_INTERVAL } = require('../config.js').MAIN; const log = require('./log.js'); -const reportedIps = new Map(); +const reportedIPs = new Map(); -const loadReportedIps = () => { +const loadReportedIPs = () => { if (fs.existsSync(CACHE_FILE)) { fs.readFileSync(CACHE_FILE, 'utf8') .split('\n') .forEach(line => { const [ip, time] = line.split(' '); - if (ip && time) reportedIps.set(ip, Number(time)); + if (ip && time) reportedIPs.set(ip, Number(time)); }); - log(0, `Loaded ${reportedIps.size} IPs from ${CACHE_FILE}`); + log(0, `Loaded ${reportedIPs.size} IPs from ${CACHE_FILE}`); } else { log(0, `${CACHE_FILE} does not exist. No data to load.`); } }; -const saveReportedIps = () => fs.writeFileSync(CACHE_FILE, Array.from(reportedIps).map(([ip, time]) => `${ip} ${time}`).join('\n'), 'utf8'); +const saveReportedIPs = () => fs.writeFileSync(CACHE_FILE, Array.from(reportedIPs).map(([ip, time]) => `${ip} ${time}`).join('\n'), 'utf8'); -const isIpReportedRecently = ip => { - const reportedTime = reportedIps.get(ip); +const isIPReportedRecently = ip => { + const reportedTime = reportedIPs.get(ip); return reportedTime && (Date.now() / 1000 - reportedTime < REPORT_INTERVAL / 1000); }; -const markIpAsReported = ip => reportedIps.set(ip, Math.floor(Date.now() / 1000)); +const markIPAsReported = ip => reportedIPs.set(ip, Math.floor(Date.now() / 1000)); -module.exports = { reportedIps, loadReportedIps, saveReportedIps, isIpReportedRecently, markIpAsReported }; \ No newline at end of file +module.exports = { reportedIPs, loadReportedIPs, saveReportedIPs, isIPReportedRecently, markIPAsReported }; \ No newline at end of file