diff --git a/default.config.js b/default.config.js index d515d7a..068e774 100644 --- a/default.config.js +++ b/default.config.js @@ -2,25 +2,25 @@ exports.MAIN = { // Server UFW_FILE: '/var/log/ufw.log', CACHE_FILE: '/tmp/ufw-abuseipdb-reporter.cache', - SERVER_ID: null, + SERVER_ID: null, // The server name that will be visible in the reports. If you don't want to define it, leave the value as null. // Reporting ABUSEIPDB_API_KEY: '', REPORT_INTERVAL: 12 * 60 * 60 * 1000, // 12h // Project - GITHUB_REPO: 'https://github.com/sefinek/UFW-AbuseIPDB-Reporter', + GITHUB_REPO: 'https://github.com/sefinek/UFW-AbuseIPDB-Reporter', // If you are using a fork, provide the link to the forked repository here. }; exports.REPORT_COMMENT = (timestamp, srcIp, dstIp, proto, spt, dpt, ttl, len, tos, serverName) => { return `Blocked by UFW ${serverName ? `on ${serverName} ` : ''}[${dpt}/${proto?.toLowerCase()}] -Source port: ${spt} +Source port: ${spt || 'N/A'} TTL: ${ttl || 'N/A'} Packet length: ${len || 'N/A'} TOS: ${tos || 'N/A'} This report (for ${srcIp}) was generated by: -https://github.com/sefinek/UFW-AbuseIPDB-Reporter`; // Please do not remove the URL to the repository of this script. I would be really grateful. 💙 +https://github.com/sefinek/UFW-AbuseIPDB-Reporter`; // Please do not remove this URL; I would be very grateful! Thank you. 💙 }; // See: https://www.abuseipdb.com/categories diff --git a/index.js b/index.js index 1be4652..9a5fe63 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ 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 isLocalIP = require('./services/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 getServerIP = require('./services/serverIp.js'); const config = require('./config.js'); const { version } = require('./package.json'); const { UFW_FILE, ABUSEIPDB_API_KEY, SERVER_ID, GITHUB_REPO } = config.MAIN; @@ -45,6 +46,11 @@ const processLogLine = async line => { return; } + if (srcIp === getServerIP()) { + log(0, 'Ignoring own IP'); + return; + } + if (isLocalIP(srcIp)) { log(0, `Ignoring local/private IP: ${srcIp}`); return; diff --git a/utils/cache.js b/services/cache.js similarity index 96% rename from utils/cache.js rename to services/cache.js index 2538577..77871e8 100644 --- a/utils/cache.js +++ b/services/cache.js @@ -1,6 +1,6 @@ const fs = require('node:fs'); const { CACHE_FILE, REPORT_INTERVAL } = require('../config.js').MAIN; -const log = require('./log.js'); +const log = require('../utils/log.js'); const reportedIPs = new Map(); diff --git a/utils/isLocalIP.js b/services/isLocalIP.js similarity index 100% rename from utils/isLocalIP.js rename to services/isLocalIP.js diff --git a/services/serverIp.js b/services/serverIp.js new file mode 100644 index 0000000..b57a745 --- /dev/null +++ b/services/serverIp.js @@ -0,0 +1,26 @@ +const axios = require('./axios.js'); + +let address = null; + +const fetchIPAddress = async () => { + if (address) return; + + try { + const { data } = await axios.get('https://api.sefinek.net/api/v2/ip'); + if (data?.success && data?.message) { + address = data.message; + } else { + setTimeout(fetchIPAddress, 20 * 1000); + } + } catch { + setTimeout(fetchIPAddress, 25 * 1000); + } +}; + +if (process.env.NODE_ENV === 'production') { + (async () => fetchIPAddress())(); +} else { + address = '::ffff:127.0.0.1'; +} + +module.exports = () => address; \ No newline at end of file