Fixes
This commit is contained in:
1 parent
39ce553892
commit
699354b85a
6 files changed
+25
-21
No files matched your search
+5
-4
@@ -1,12 +1,13 @@
|
||||
exports.MAIN = {
|
||||
// Server
|
||||
UFW_FILE: '/var/log/ufw.log',
|
||||
// This server
|
||||
UFW_LOG_FILE: '/var/log/ufw.log',
|
||||
CACHE_FILE: '/tmp/ufw-abuseipdb-reporter.cache',
|
||||
SERVER_ID: null, // The server name that will be visible in the reports (e.g., 'homeserver01'). If you don't want to define it, leave the value as null.
|
||||
REFRESHING_IP_ADDRESS: 5 * 60 * 1000, // How often should (every 5 minutes) the script check the server's IP address to avoid accidental self-reports?
|
||||
|
||||
// Reporting
|
||||
ABUSEIPDB_API_KEY: '',
|
||||
REPORT_INTERVAL: 12 * 60 * 60 * 1000, // 12h
|
||||
ABUSEIPDB_API_KEY: '', // Secret API key for AbuseIPDB.
|
||||
IP_REPORT_COOLDOWN: 12 * 60 * 60 * 1000, // The minimum time (12 hours) that must pass before reporting the same IP address again.
|
||||
|
||||
// Project
|
||||
GITHUB_REPO: 'https://github.com/sefinek/UFW-AbuseIPDB-Reporter', // If you are using a fork, provide the link to the forked repository here.
|
||||
|
||||
@@ -1,27 +1,30 @@
|
||||
// Copyright 2024 © by Sefinek. All rights reserved.
|
||||
//
|
||||
// Copyright 2024-2025 (c) by Sefinek All rights reserved.
|
||||
// https://sefinek.net
|
||||
//
|
||||
|
||||
const fs = require('node:fs');
|
||||
const chokidar = require('chokidar');
|
||||
const isLocalIP = require('./services/isLocalIP.js');
|
||||
const isLocalIP = require('./utils/isLocalIP.js');
|
||||
const { reportedIPs, loadReportedIPs, saveReportedIPs, isIPReportedRecently, markIPAsReported } = require('./services/cache.js');
|
||||
const log = require('./utils/log.js');
|
||||
const axios = require('./services/axios.js');
|
||||
const serverAddress = require('./services/serverAddress.js');
|
||||
const config = require('./config.js');
|
||||
const { version } = require('./package.json');
|
||||
const { UFW_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN;
|
||||
const { UFW_LOG_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN;
|
||||
|
||||
let fileOffset = 0;
|
||||
|
||||
const reportToAbuseIPDb = async (logData, categories, comment) => {
|
||||
try {
|
||||
const { data } = await axios.post('https://api.abuseipdb.com/api/v2/report', new URLSearchParams({
|
||||
const { data: res } = await axios.post('https://api.abuseipdb.com/api/v2/report', new URLSearchParams({
|
||||
ip: logData.srcIp,
|
||||
categories,
|
||||
comment,
|
||||
}), { headers: { 'Key': ABUSEIPDB_API_KEY } });
|
||||
|
||||
log(0, `Reported ${logData.srcIp} [${logData.dpt}/${logData.proto}]; ID: ${logData.id}; Categories ${categories}; Abuse: ${data.data.abuseConfidenceScore}%`);
|
||||
log(0, `Reported ${logData.srcIp} [${logData.dpt}/${logData.proto}]; ID: ${logData.id}; Categories ${categories}; Abuse: ${res.data.abuseConfidenceScore}%`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
log(2, `Failed to report ${logData.srcIp} [${logData.dpt}/${logData.proto}]; ID: ${logData.id}; ${err.message}\n${JSON.stringify(err.response.data?.errors || err.response.data)}`);
|
||||
@@ -111,19 +114,18 @@ const processLogLine = async line => {
|
||||
};
|
||||
|
||||
(async () => {
|
||||
log(0, `* Version: ${version}`);
|
||||
log(0, `* Repository: ${GITHUB_REPO}`);
|
||||
log(0, `v${version} (${GITHUB_REPO})`);
|
||||
|
||||
loadReportedIPs();
|
||||
|
||||
if (!fs.existsSync(UFW_FILE)) {
|
||||
log(2, `Log file ${UFW_FILE} does not exist.`);
|
||||
if (!fs.existsSync(UFW_LOG_FILE)) {
|
||||
log(2, `Log file ${UFW_LOG_FILE} does not exist.`);
|
||||
return;
|
||||
}
|
||||
|
||||
fileOffset = fs.statSync(UFW_FILE).size;
|
||||
fileOffset = fs.statSync(UFW_LOG_FILE).size;
|
||||
|
||||
chokidar.watch(UFW_FILE, { persistent: true, ignoreInitial: true })
|
||||
chokidar.watch(UFW_LOG_FILE, { persistent: true, ignoreInitial: true })
|
||||
.on('change', path => {
|
||||
const stats = fs.statSync(path);
|
||||
if (stats.size < fileOffset) {
|
||||
@@ -140,6 +142,6 @@ const processLogLine = async line => {
|
||||
|
||||
if (process?.send) process.send('ready');
|
||||
|
||||
log(0, `Ready! Now monitoring: ${UFW_FILE}`);
|
||||
log(0, `Ready! Now monitoring: ${UFW_LOG_FILE}`);
|
||||
log(0, '=====================================================================');
|
||||
})();
|
||||
+1
-1
@@ -185,7 +185,7 @@ fi
|
||||
config_file="config.js"
|
||||
if [[ -f $config_file ]]; then
|
||||
echo "🔧 Updating $PWD/$config_file..."
|
||||
sed -i "s|UFW_FILE: .*|UFW_FILE: '$ufw_log_path',|" $config_file
|
||||
sed -i "s|UFW_LOG_FILE: .*|UFW_LOG_FILE: '$ufw_log_path',|" $config_file
|
||||
sed -i "s|SERVER_ID: .*|SERVER_ID: '$server_id',|" $config_file
|
||||
sed -i "s|ABUSEIPDB_API_KEY: .*|ABUSEIPDB_API_KEY: '$api_token',|" $config_file
|
||||
else
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
const fs = require('node:fs');
|
||||
const { CACHE_FILE, REPORT_INTERVAL } = require('../config.js').MAIN;
|
||||
const { CACHE_FILE, IP_REPORT_COOLDOWN } = require('../config.js').MAIN;
|
||||
const log = require('../utils/log.js');
|
||||
|
||||
const reportedIPs = new Map();
|
||||
@@ -22,7 +22,7 @@ const saveReportedIPs = () => fs.writeFileSync(CACHE_FILE, Array.from(reportedIP
|
||||
|
||||
const isIPReportedRecently = ip => {
|
||||
const reportedTime = reportedIPs.get(ip);
|
||||
return reportedTime && (Date.now() / 1000 - reportedTime < REPORT_INTERVAL / 1000);
|
||||
return reportedTime && (Date.now() / 1000 - reportedTime < IP_REPORT_COOLDOWN / 1000);
|
||||
};
|
||||
|
||||
const markIPAsReported = ip => reportedIPs.set(ip, Math.floor(Date.now() / 1000));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const os = require('node:os');
|
||||
const axios = require('./axios.js');
|
||||
const isLocalIP = require('./isLocalIP.js');
|
||||
const isLocalIP = require('../utils/isLocalIP.js');
|
||||
const { MAIN } = require('../config.js');
|
||||
|
||||
const ipAddressList = new Set();
|
||||
|
||||
@@ -34,7 +35,7 @@ const fetchIPAddress = async () => {
|
||||
|
||||
(async () => {
|
||||
await fetchIPAddress();
|
||||
setInterval(fetchIPAddress, 25 * 1000);
|
||||
setInterval(fetchIPAddress, MAIN.REFRESHING_IP_ADDRESS);
|
||||
|
||||
// console.debug(ipAddressList);
|
||||
})();
|
||||
|
||||
File renamed without changes.
Reference in new issue
Block a user