diff --git a/src/Util.ts b/src/Util.ts index e99bd07..c0225e8 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -2,6 +2,8 @@ import { execSync, exec } from "node:child_process"; import { getuid, versions, platform } from "node:process"; import { promisify } from "node:util"; +export const shouldDryRunDuringTesting = process.env.npm_lifecycle_event === "test" ? "--dry-run" : ""; + export async function runCommand(command: string) { const promisifiedExec = promisify(exec); const { stderr, stdout } = await promisifiedExec(command); diff --git a/src/methods/Allow.ts b/src/methods/Allow.ts index 380d474..b9da7df 100644 --- a/src/methods/Allow.ts +++ b/src/methods/Allow.ts @@ -1,4 +1,4 @@ -import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util"; +import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import type { PortProtocol } from "../Typings"; /** @@ -10,7 +10,7 @@ async function port(port: number, protocol?: PortProtocol) { let checkPort = checkAppropriatePort(port); if (!checkPort) return false; - let command = await runCommand(`echo "y" | sudo ufw allow ${port}${protocol ? `/${protocol}` : ""}`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} allow ${port}${protocol ? `/${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 788ac9f..d6f18df 100644 --- a/src/methods/Delete.ts +++ b/src/methods/Delete.ts @@ -1,4 +1,4 @@ -import { runCommand } from "../Util"; +import { runCommand, shouldDryRunDuringTesting } from "../Util"; /** * Delete ufw rule(s). (root/sudo access is mandatory) @@ -9,7 +9,7 @@ export default async function(num: number) { num = 1; }; - let command = await runCommand(`echo "y" | sudo ufw delete ${num}`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} delete ${num}`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Deny.ts b/src/methods/Deny.ts index ea9ebb1..eae7d69 100644 --- a/src/methods/Deny.ts +++ b/src/methods/Deny.ts @@ -1,4 +1,4 @@ -import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util"; +import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import type { PortProtocol } from "../Typings"; /** @@ -10,7 +10,7 @@ async function port(port: number, protocol?: PortProtocol) { let checkPort = checkAppropriatePort(port); if (!checkPort) return false; - let command = await runCommand(`echo "y" | sudo ufw deny ${port}${protocol ? `/${protocol}` : ""}`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny ${port}${protocol ? `/${protocol}` : ""}`); return command ? command.toLowerCase().match(/(added)/gi) !== null : false; } catch (err) { throw err; @@ -32,7 +32,7 @@ async function address(address: string, port?: number, protocol?: PortProtocol) if (!checkPort) return false; }; - let command = await runCommand(`echo "y" | sudo ufw deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} 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 ae48929..b1a8449 100644 --- a/src/methods/Disable.ts +++ b/src/methods/Disable.ts @@ -1,11 +1,11 @@ -import { runCommand } from "../Util"; +import { runCommand,shouldDryRunDuringTesting } from "../Util"; /** * Disable ufw. (root/sudo access is mandatory) */ export default async function() { try { - let command = await runCommand(`echo "y" | sudo ufw disable`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} disable`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Enable.ts b/src/methods/Enable.ts index 9fb0fe9..6712ce4 100644 --- a/src/methods/Enable.ts +++ b/src/methods/Enable.ts @@ -1,9 +1,9 @@ -import { runCommand } from "../Util"; +import { runCommand, shouldDryRunDuringTesting } from "../Util"; export default async function() { try { // https://serverfault.com/a/790150 - let command = await runCommand(`echo "y" | sudo ufw enable`); + let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} enable`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Logging.ts b/src/methods/Logging.ts index 26da7cf..066faab 100644 --- a/src/methods/Logging.ts +++ b/src/methods/Logging.ts @@ -1,4 +1,4 @@ -import { runCommand } from "../Util"; +import { runCommand, shouldDryRunDuringTesting } from "../Util"; import type { LoggingType } from "../Typings"; /** @@ -6,7 +6,7 @@ import type { LoggingType } from "../Typings"; */ export default async function(type: LoggingType) { try { - let command = await runCommand(`sudo ufw logging ${type}`); + let command = await runCommand(`sudo ufw ${shouldDryRunDuringTesting} logging ${type}`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Reload.ts b/src/methods/Reload.ts index 40e0d46..a378d02 100644 --- a/src/methods/Reload.ts +++ b/src/methods/Reload.ts @@ -1,11 +1,11 @@ -import { runCommand } from "../Util"; +import { runCommand,shouldDryRunDuringTesting } from "../Util"; /** * Reloads firewall. (root/sudo access is mandatory) */ export default async function() { try { - let command = await runCommand("sudo ufw reload"); + let command = await runCommand(`sudo ufw ${shouldDryRunDuringTesting} reload`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Reset.ts b/src/methods/Reset.ts index 603aac0..df656fd 100644 --- a/src/methods/Reset.ts +++ b/src/methods/Reset.ts @@ -1,11 +1,11 @@ -import { runCommand } from "../Util"; +import { runCommand, shouldDryRunDuringTesting } 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 command = await runCommand("sudo ufw --force reset"); + let command = await runCommand(`sudo ufw ${shouldDryRunDuringTesting} --force reset`); return command !== null; } catch (err) { throw err; diff --git a/src/methods/Status.ts b/src/methods/Status.ts index d636538..2c3c2b2 100644 --- a/src/methods/Status.ts +++ b/src/methods/Status.ts @@ -1,12 +1,12 @@ import type { ParsedStatus } from "../Typings"; -import { runCommand } from "../Util"; +import { runCommand, shouldDryRunDuringTesting } from "../Util"; /** * List of currently activated ufw. (root/sudo access is mandatory) */ export default async function(raw?: boolean): Promise { try { - let command = await runCommand("sudo ufw status"); + let command = await runCommand(`sudo ufw ${shouldDryRunDuringTesting} status`); if (command) { if (raw) return command;