diff --git a/default.config.js b/default.config.js index 7f8e433..cb42edb 100644 --- a/default.config.js +++ b/default.config.js @@ -12,9 +12,6 @@ exports.MAIN = { // Automatic updates AUTO_UPDATE_SCHEDULE: '0 18 * * *', // Schedule for automatic script updates (CRON format). Default: every day at 18:00 AUTO_UPDATE_ENABLED: true, // Do you want the script to automatically update to the latest version using 'git pull'? (true = enabled, false = disabled) - - // Project - GITHUB_REPO: 'https://github.com/sefinek/UFW-AbuseIPDB-Reporter', // If you are using a fork, provide the link to the forked repository here. }; diff --git a/index.js b/index.js index 7600ef4..1be416c 100644 --- a/index.js +++ b/index.js @@ -12,10 +12,7 @@ const axios = require('./services/axios.js'); const serverAddress = require('./services/fetchServerIP.js'); const config = require('./config.js'); const { version } = require('./package.json'); -const { UFW_LOG_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN; - -// Updates -require('./update.js'); +const { UFW_LOG_FILE, ABUSEIPDB_API_KEY, SERVER_ID, AUTO_UPDATE_ENABLED } = config.MAIN; let fileOffset = 0; @@ -38,12 +35,10 @@ const reportToAbuseIPDb = async (logData, categories, comment) => { const processLogLine = async line => { if (!line.includes('[UFW BLOCK]')) return log(1, `Ignoring line: ${line}`); - const oldTime = line.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[+-]\d{2}:\d{2})?/)?.[0]; const timestampMatch = line.match(/\[(\d+\.\d+)\]/); - const newTime = timestampMatch ? parseFloat(timestampMatch[1]) : null; const logData = { - timestampOld: oldTime || null, - timestampNew: newTime || null, + timestampOld: line.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:[+-]\d{2}:\d{2})?/)?.[0] || null, + timestampNew: (timestampMatch ? parseFloat(timestampMatch[1]) : null) || null, In: line.match(/IN=([\d.]+)/)?.[1] || null, Out: line.match(/OUT=([\d.]+)/)?.[1] || null, srcIp: line.match(/SRC=([\d.]+)/)?.[1] || null, @@ -117,7 +112,7 @@ const processLogLine = async line => { }; (async () => { - log(0, `v${version} (${GITHUB_REPO})`); + log(0, `v${version} (https://github.com/sefinek/UFW-AbuseIPDB-Reporter)`); loadReportedIPs(); @@ -147,4 +142,10 @@ const processLogLine = async line => { log(0, `Ready! Now monitoring: ${UFW_LOG_FILE}`); log(0, '====================================================================='); + + // Auto updates + if (AUTO_UPDATE_ENABLED) { + const autoUpdates = require('./services/updates.js'); + await autoUpdates.pull(); + } })(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3d8c270..959f999 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ufw-abuseipdb-reporter", - "version": "0.1.1", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ufw-abuseipdb-reporter", - "version": "0.1.1", + "version": "0.1.2", "license": "GPL-3.0", "dependencies": { "axios": "^1.7.9", diff --git a/package.json b/package.json index 6136a2b..e028517 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ufw-abuseipdb-reporter", - "version": "0.1.1", + "version": "0.1.2", "description": "A tool (with a simple installer) that monitors UFW firewall logs in real time and reports IP addresses to the AbuseIPDB database.", "keywords": [ "ufw", diff --git a/services/axios.js b/services/axios.js index 3feccab..c329bae 100644 --- a/services/axios.js +++ b/services/axios.js @@ -1,9 +1,8 @@ const axios = require('axios'); const { version } = require('../package.json'); -const { GITHUB_REPO } = require('../config.js').MAIN; axios.defaults.headers.common = { - 'User-Agent': `Mozilla/5.0 (compatible; UFW-AbuseIPDB-Reporter/${version}; +${GITHUB_REPO})`, + '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/services/cache.js b/services/cache.js index 0911d27..d2150ee 100644 --- a/services/cache.js +++ b/services/cache.js @@ -1,12 +1,12 @@ -const fs = require('node:fs'); +const { existsSync, readFileSync, writeFileSync } = require('node:fs'); const { CACHE_FILE, IP_REPORT_COOLDOWN } = require('../config.js').MAIN; const log = require('../utils/log.js'); const reportedIPs = new Map(); const loadReportedIPs = () => { - if (fs.existsSync(CACHE_FILE)) { - fs.readFileSync(CACHE_FILE, 'utf8') + if (existsSync(CACHE_FILE)) { + readFileSync(CACHE_FILE, 'utf8') .split('\n') .forEach(line => { const [ip, time] = line.split(' '); @@ -18,7 +18,7 @@ const loadReportedIPs = () => { } }; -const saveReportedIPs = () => fs.writeFileSync(CACHE_FILE, Array.from(reportedIPs).map(([ip, time]) => `${ip} ${time}`).join('\n'), 'utf8'); +const saveReportedIPs = () => writeFileSync(CACHE_FILE, Array.from(reportedIPs).map(([ip, time]) => `${ip} ${time}`).join('\n'), 'utf8'); const isIPReportedRecently = ip => { const reportedTime = reportedIPs.get(ip); diff --git a/services/updates.js b/services/updates.js new file mode 100644 index 0000000..d353d82 --- /dev/null +++ b/services/updates.js @@ -0,0 +1,29 @@ +const { AUTO_UPDATE_SCHEDULE } = require('../config.js').MAIN; + +const simpleGit = require('simple-git'); +const { CronJob } = require('cron'); +const restartApp = require('./reloadApp.js'); +const log = require('../utils/log.js'); + +const git = simpleGit(); + +const pull = async () => { + log(0, '$ git pull'); + + const { summary } = await git.pull(); + log(0, `Changes: ${summary.changes}; Deletions: ${summary.insertions}; Insertions: ${summary.insertions};`); +}; + +const pullAndRestart = async () => { + try { + await pull(); + await restartApp(); + } catch (err) { + log(2, err.message); + } +}; + +// https://crontab.guru +new CronJob(AUTO_UPDATE_SCHEDULE || '0 18 * * *', pullAndRestart, null, true, 'UTC'); // At 18:00 + +module.exports = { pull }; \ No newline at end of file diff --git a/update.js b/update.js deleted file mode 100644 index 997f9c1..0000000 --- a/update.js +++ /dev/null @@ -1,28 +0,0 @@ -const { SERVER_ID, AUTO_UPDATE_SCHEDULE, AUTO_UPDATE_ENABLED } = require('./config.js').MAIN; - -if (AUTO_UPDATE_ENABLED) { - const simpleGit = require('simple-git'); - const { CronJob } = require('cron'); - const restartApp = require('./services/reloadApp.js'); - const log = require('./utils/log.js'); - - const git = simpleGit(); - - const updateScript = async () => { - log(0, 'The script is being updated...'); - - try { - const { summary } = await git.pull(); - log(0, `Changes: ${summary.changes}; Deletions: ${summary.insertions}; Insertions: ${summary.insertions};`); - - if (SERVER_ID !== 'development') await restartApp(); - } catch (err) { - log(2, err.message); - } - }; - - // https://crontab.guru - new CronJob(AUTO_UPDATE_SCHEDULE || '0 18 * * *', updateScript, null, true, 'UTC'); // At 18:00 - - if (SERVER_ID === 'development') (async () => updateScript())(); -} \ No newline at end of file