This commit is contained in:
Sefinek committed 2025-02-01 20:29:35 +01:00
1 parent d5921f19e7
commit 5320952ccb
8 files changed
+47 -49

No files matched your search

-3
View File
@@ -12,9 +12,6 @@ exports.MAIN = {
// Automatic updates // Automatic updates
AUTO_UPDATE_SCHEDULE: '0 18 * * *', // Schedule for automatic script updates (CRON format). Default: every day at 18:00 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) 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.
}; };
+10 -9
View File
@@ -12,10 +12,7 @@ const axios = require('./services/axios.js');
const serverAddress = require('./services/fetchServerIP.js'); const serverAddress = require('./services/fetchServerIP.js');
const config = require('./config.js'); const config = require('./config.js');
const { version } = require('./package.json'); const { version } = require('./package.json');
const { UFW_LOG_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN; const { UFW_LOG_FILE, ABUSEIPDB_API_KEY, SERVER_ID, AUTO_UPDATE_ENABLED } = config.MAIN;
// Updates
require('./update.js');
let fileOffset = 0; let fileOffset = 0;
@@ -38,12 +35,10 @@ const reportToAbuseIPDb = async (logData, categories, comment) => {
const processLogLine = async line => { const processLogLine = async line => {
if (!line.includes('[UFW BLOCK]')) return log(1, `Ignoring line: ${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 timestampMatch = line.match(/\[(\d+\.\d+)\]/);
const newTime = timestampMatch ? parseFloat(timestampMatch[1]) : null;
const logData = { const logData = {
timestampOld: oldTime || 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: newTime || null, timestampNew: (timestampMatch ? parseFloat(timestampMatch[1]) : null) || null,
In: line.match(/IN=([\d.]+)/)?.[1] || null, In: line.match(/IN=([\d.]+)/)?.[1] || null,
Out: line.match(/OUT=([\d.]+)/)?.[1] || null, Out: line.match(/OUT=([\d.]+)/)?.[1] || null,
srcIp: line.match(/SRC=([\d.]+)/)?.[1] || null, srcIp: line.match(/SRC=([\d.]+)/)?.[1] || null,
@@ -117,7 +112,7 @@ const processLogLine = async line => {
}; };
(async () => { (async () => {
log(0, `v${version} (${GITHUB_REPO})`); log(0, `v${version} (https://github.com/sefinek/UFW-AbuseIPDB-Reporter)`);
loadReportedIPs(); loadReportedIPs();
@@ -147,4 +142,10 @@ const processLogLine = async line => {
log(0, `Ready! Now monitoring: ${UFW_LOG_FILE}`); log(0, `Ready! Now monitoring: ${UFW_LOG_FILE}`);
log(0, '====================================================================='); log(0, '=====================================================================');
// Auto updates
if (AUTO_UPDATE_ENABLED) {
const autoUpdates = require('./services/updates.js');
await autoUpdates.pull();
}
})(); })();
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "ufw-abuseipdb-reporter", "name": "ufw-abuseipdb-reporter",
"version": "0.1.1", "version": "0.1.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ufw-abuseipdb-reporter", "name": "ufw-abuseipdb-reporter",
"version": "0.1.1", "version": "0.1.2",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"axios": "^1.7.9", "axios": "^1.7.9",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "ufw-abuseipdb-reporter", "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.", "description": "A tool (with a simple installer) that monitors UFW firewall logs in real time and reports IP addresses to the AbuseIPDB database.",
"keywords": [ "keywords": [
"ufw", "ufw",
+1 -2
View File
@@ -1,9 +1,8 @@
const axios = require('axios'); const axios = require('axios');
const { version } = require('../package.json'); const { version } = require('../package.json');
const { GITHUB_REPO } = require('../config.js').MAIN;
axios.defaults.headers.common = { 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', 'Accept': 'application/json',
'Cache-Control': 'no-cache', 'Cache-Control': 'no-cache',
'Connection': 'keep-alive', 'Connection': 'keep-alive',
+4 -4
View File
@@ -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 { CACHE_FILE, IP_REPORT_COOLDOWN } = require('../config.js').MAIN;
const log = require('../utils/log.js'); const log = require('../utils/log.js');
const reportedIPs = new Map(); const reportedIPs = new Map();
const loadReportedIPs = () => { const loadReportedIPs = () => {
if (fs.existsSync(CACHE_FILE)) { if (existsSync(CACHE_FILE)) {
fs.readFileSync(CACHE_FILE, 'utf8') readFileSync(CACHE_FILE, 'utf8')
.split('\n') .split('\n')
.forEach(line => { .forEach(line => {
const [ip, time] = line.split(' '); 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 isIPReportedRecently = ip => {
const reportedTime = reportedIPs.get(ip); const reportedTime = reportedIPs.get(ip);
+29
View File
@@ -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 };
-28
View File
@@ -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())();
}