From 87e439ae58751729e1cce3c37079950214277a0e Mon Sep 17 00:00:00 2001 From: ray-1337 <33544674+ray-1337@users.noreply.github.com> Date: Sat, 3 Jun 2023 00:49:19 +0200 Subject: [PATCH] implement runCommand function instead --- src/Util.ts | 14 +++++++++++++- src/methods/Allow.ts | 40 +++++----------------------------------- src/methods/Delete.ts | 18 +++--------------- src/methods/Deny.ts | 42 +++++------------------------------------- src/methods/Disable.ts | 15 +++------------ src/methods/Enable.ts | 15 +++------------ src/methods/Logging.ts | 15 +++------------ src/methods/Reload.ts | 15 +++------------ src/methods/Reset.ts | 15 +++------------ src/methods/Status.ts | 14 +++++--------- 10 files changed, 46 insertions(+), 157 deletions(-) diff --git a/src/Util.ts b/src/Util.ts index 4485400..fbf530d 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -1,5 +1,17 @@ -import { execSync } from "node:child_process"; +import { execSync, exec } from "node:child_process"; import { getuid, versions, platform } from "node:process"; +import { promisify } from "node:util"; + +export async function runCommand(command: string) { + const promisifiedExec = promisify(exec); + const { stderr, stdout } = await promisifiedExec(command); + + if (stderr) { + throw stderr; + }; + + return stdout?.length ? stdout : null; +}; export function checkSudo() { return getuid && getuid() == 0 ? true : false; diff --git a/src/methods/Allow.ts b/src/methods/Allow.ts index 22514f0..3903f2d 100644 --- a/src/methods/Allow.ts +++ b/src/methods/Allow.ts @@ -1,10 +1,6 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -import { checkAppropriatePort, checkAppropriateIP } from "../Util"; +import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util"; import type { PortProtocol } from "../Typings"; -const promisifiedExec = promisify(exec); - /** * Allow incoming requests through specific port. (root/sudo access is mandatory) */ @@ -14,21 +10,8 @@ async function port(port: number, protocol?: PortProtocol) { let checkPort = checkAppropriatePort(port); if (!checkPort) return false; - let res = await promisifiedExec(`echo "y" | sudo ufw allow ${port}${protocol ? `/${protocol}` : ""}`); - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - // will find a better way to parse this - if (res.stdout.toLowerCase().match(/(added)/gi)) { - return true; - } else { - console.log(res.stdout); - return false; - }; - } else { - console.log(res.stdout); - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw allow ${port}${protocol ? `/${protocol}` : ""}`); + return command ? command.toLowerCase().match(/(added)/gi) !== null : false; } catch (err) { throw err; }; @@ -49,21 +32,8 @@ async function address(address: string, port: number, protocol?: PortProtocol) { if (!checkPort) return false; }; - let res = await promisifiedExec(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - // will find a better way to parse this - if (res.stdout.toLowerCase().match(/(added)/gi)) { - return true; - } else { - console.log(res.stdout); - return false; - }; - } else { - console.log(res.stdout); - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); + return command ? command.toLowerCase().match(/(added)/gi) !== null : false; } catch (err) { throw err; }; diff --git a/src/methods/Delete.ts b/src/methods/Delete.ts index 51ab4d2..788ac9f 100644 --- a/src/methods/Delete.ts +++ b/src/methods/Delete.ts @@ -1,6 +1,4 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; /** * Delete ufw rule(s). (root/sudo access is mandatory) @@ -11,18 +9,8 @@ export default async function(num: number) { num = 1; }; - let res = await promisifiedExec(`echo "y" | sudo ufw delete ${num}`); - - if (res.stderr) { - throw new Error(res.stderr); - }; - - if (res.stdout) { - return true; - } else { - console.log(res.stdout); - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw delete ${num}`); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Deny.ts b/src/methods/Deny.ts index 0bc812a..04de425 100644 --- a/src/methods/Deny.ts +++ b/src/methods/Deny.ts @@ -1,10 +1,6 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -import { checkAppropriatePort, checkAppropriateIP } from "../Util"; +import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util"; import type { PortProtocol } from "../Typings"; -const promisifiedExec = promisify(exec); - /** * Deny incoming requests through specific port. (root/sudo access is mandatory) */ @@ -14,23 +10,8 @@ async function port(port: number, protocol?: PortProtocol) { let checkPort = checkAppropriatePort(port); if (!checkPort) return false; - let res = await promisifiedExec(`echo "y" | sudo ufw deny ${port}${protocol ? `/${protocol}` : ""}`); - if (res.stderr) { - throw new Error(res.stderr); - }; - - if (res.stdout) { - // will find a better way to parse this - if (res.stdout.toLowerCase().match(/(added)/gi)) { - return true; - } else { - console.log(res.stdout); - return false; - }; - } else { - console.log(res.stdout); - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw deny ${port}${protocol ? `/${protocol}` : ""}`); + return command ? command.toLowerCase().match(/(added)/gi) !== null : false; } catch (err) { throw err; }; @@ -51,21 +32,8 @@ async function address(address: string, port: number, protocol?: PortProtocol) { if (!checkPort) return false; }; - let res = await promisifiedExec(`echo "y" | sudo ufw deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - // will find a better way to parse this - if (res.stdout.toLowerCase().match(/(added)/gi)) { - return true; - } else { - console.log(res.stdout); - return false; - }; - } else { - console.log(res.stdout); - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); + return command ? command.toLowerCase().match(/(added)/gi) !== null : false; } catch (err) { throw err; }; diff --git a/src/methods/Disable.ts b/src/methods/Disable.ts index b192dcd..ae48929 100644 --- a/src/methods/Disable.ts +++ b/src/methods/Disable.ts @@ -1,21 +1,12 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; /** * Disable ufw. (root/sudo access is mandatory) */ export default async function() { try { - let res = await promisifiedExec(`echo "y" | sudo ufw disable`); - - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - return true; - } else { - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw disable`); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Enable.ts b/src/methods/Enable.ts index 208e16a..9fb0fe9 100644 --- a/src/methods/Enable.ts +++ b/src/methods/Enable.ts @@ -1,19 +1,10 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; export default async function() { try { // https://serverfault.com/a/790150 - let res = await promisifiedExec(`echo "y" | sudo ufw enable`); - - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - return true; - } else { - return false; - }; + let command = await runCommand(`echo "y" | sudo ufw enable`); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Logging.ts b/src/methods/Logging.ts index 3a9e178..26da7cf 100644 --- a/src/methods/Logging.ts +++ b/src/methods/Logging.ts @@ -1,22 +1,13 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; +import { runCommand } from "../Util"; import type { LoggingType } from "../Typings"; -const promisifiedExec = promisify(exec); /** * Set/toggle UFW logging. (root/sudo access is mandatory) */ export default async function(type: LoggingType) { try { - let res = await promisifiedExec(`sudo ufw logging ${type}`); - - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - return true; - } else { - return false; - }; + let command = await runCommand(`sudo ufw logging ${type}`); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Reload.ts b/src/methods/Reload.ts index 4f7a7db..40e0d46 100644 --- a/src/methods/Reload.ts +++ b/src/methods/Reload.ts @@ -1,21 +1,12 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; /** * Reloads firewall. (root/sudo access is mandatory) */ export default async function() { try { - let res = await promisifiedExec("sudo ufw reload"); - - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - return true; - } else { - return false; - }; + let command = await runCommand("sudo ufw reload"); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Reset.ts b/src/methods/Reset.ts index fa7cf20..603aac0 100644 --- a/src/methods/Reset.ts +++ b/src/methods/Reset.ts @@ -1,21 +1,12 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; /** * Disables and resets firewall to installation defaults. No prompt. Use this wisely. (root/sudo access is mandatory)= */ export default async function() { try { - let res = await promisifiedExec("sudo ufw --force reset"); - - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - return true; - } else { - return false; - }; + let command = await runCommand("sudo ufw --force reset"); + return command !== null; } catch (err) { throw err; }; diff --git a/src/methods/Status.ts b/src/methods/Status.ts index a8eb1ea..d636538 100644 --- a/src/methods/Status.ts +++ b/src/methods/Status.ts @@ -1,24 +1,20 @@ -import { exec } from "node:child_process"; -import { promisify } from "node:util"; import type { ParsedStatus } from "../Typings"; -const promisifiedExec = promisify(exec); +import { runCommand } from "../Util"; /** * List of currently activated ufw. (root/sudo access is mandatory) */ export default async function(raw?: boolean): Promise { try { - let res = await promisifiedExec("sudo ufw status"); + let command = await runCommand("sudo ufw status"); - if (res.stderr) throw new Error(res.stderr); - - if (res.stdout) { - if (raw) return res.stdout; + if (command) { + if (raw) return command; let list = []; // parsing - let parsedStatus = res.stdout.replace(/\r|\n/gi, " ").split(/\s{2,}/gi).filter(x => x.length); + let parsedStatus = command.replace(/\r|\n/gi, " ").split(/\s{2,}/gi).filter(x => x.length); if (!parsedStatus.length) return []; let findAfterFrom = parsedStatus.findIndex(x => x == "----");