new test with ipset to prevent banning the same ip multiple times

This commit is contained in:
2023-08-28 16:01:47 +02:00
parent 1628ad6ee6
commit 8277e65f13
+12 -11
View File
@@ -2,6 +2,7 @@ const net = require("net")
const {spawn,exec} = require("child_process"); const {spawn,exec} = require("child_process");
const portlist = [21,1433,7777] const portlist = [21,1433,7777]
const host = "0.0.0.0"; const host = "0.0.0.0";
var ipSet = new Set()
servermap = new Map() servermap = new Map()
portlist.forEach(i => { portlist.forEach(i => {
@@ -16,15 +17,16 @@ portlist.forEach(i => {
console.log(`incomming connection from : ${a.remoteAddress} on port ${i}`) console.log(`incomming connection from : ${a.remoteAddress} on port ${i}`)
// handle error even on socket instance (prevents the econnreset error) // handle error even on socket instance (prevents the econnreset error)
a.on("error", () => {}) a.on("error", () => {})
// Handle banning ip // Add ip to ipSet
ipSet.add(a.remoteAddress)
})
servermap.set(i, server)
})
// This interval looks through the ipSet every 30 seconds and if there are an inside, it will ban them and remove them from the Set
// const output = spawn("echo", [`-A INPUT -s ${a.remoteAddress} -j DROP`]); setInterval(()=>{
// output.stdout.on("data", () => {console.log(data);}) ipSet.forEach(i => {
exec(`echo -A INPUT -s ${i} -j DROP`, (error, stdout, stderr) => {
exec(`echo -A INPUT -s ${a.remoteAddress} -j DROP`, (error, stdout, stderr) => {
if (error) { if (error) {
console.log(`error: ${error.message}`); console.log(`error: ${error.message}`);
return; return;
@@ -35,10 +37,9 @@ portlist.forEach(i => {
} }
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
}); });
ipSet.delete(i)
}) })
servermap.set(i, server) },30000)
})
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {