From e13b17361d60e235d56ad8b1345abe17f8ef7509 Mon Sep 17 00:00:00 2001 From: 2M4U Date: Thu, 3 Mar 2022 09:59:27 +0000 Subject: [PATCH] Update root.js --- lib/src/root.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/src/root.js b/lib/src/root.js index a07b14d..641b782 100644 --- a/lib/src/root.js +++ b/lib/src/root.js @@ -12,38 +12,31 @@ const { exec } = require("child_process"); const regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g; class Root { - async CheckIP() { + async ipBlocker() { for (const ipAddress of ips) { switch (isPrivateIP(ipAddress)) { case true: console.timeLog(ipAddress); break; case false: - await new Root().Execute(ipAddress); - //console.log(ipAddress); + if (ipAddress.match(regex)) { + let { error, stdout, stderr } = await exec( + `sudo iptables -A INPUT -s ${ipAddress} -j DROP` + ); + if (stderr) { + return console.log(`${stderr}`); + } + if (error !== null) { + console.log(`exec error: ${error}`); + } + console.log(`${ipAddress} Dropped.`); + + let { stdout } = await exec("sudo iptables-save"); + } break; - default: } } } - async Execute(ip) { - if (ip.match(regex)) { - exec(`sudo iptables -A INPUT -s ${ip} -j DROP`, (error, stdout, stderr) => { - if (stderr) { - return console.log(`${stderr}`); - } - if (error !== null) { - console.log(`exec error: ${error}`); - } - - return console.log(`${ip} Dropped.`); - } - ); - exec(`sudo iptables-save`, (error, stdout, stderr) => { - //console.log(stdout) - }); - } - } -} +}; module.exports = new Root();