From c2fb0a9238a66d91aeca73cfbd9b46db20c5da9b Mon Sep 17 00:00:00 2001 From: ray-1337 <33544674+ray-1337@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:36:31 +0200 Subject: [PATCH] implement isAddedOrUpdated function --- src/Util.ts | 4 ++++ src/methods/Allow.ts | 6 +++--- src/methods/Deny.ts | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Util.ts b/src/Util.ts index c0225e8..bc5f5f5 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -2,6 +2,10 @@ import { execSync, exec } from "node:child_process"; import { getuid, versions, platform } from "node:process"; import { promisify } from "node:util"; +export function isAddedOrUpdated(response: string) { + return /((adde|update)d)/gi.test(response.toLowerCase()); +}; + export const shouldDryRunDuringTesting = process.env.npm_lifecycle_event === "test" ? "--dry-run" : ""; export async function runCommand(command: string) { diff --git a/src/methods/Allow.ts b/src/methods/Allow.ts index b9da7df..261c0b1 100644 --- a/src/methods/Allow.ts +++ b/src/methods/Allow.ts @@ -1,4 +1,4 @@ -import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; +import { checkAppropriatePort, isAddedOrUpdated, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import type { PortProtocol } from "../Typings"; /** @@ -11,7 +11,7 @@ async function port(port: number, protocol?: PortProtocol) { if (!checkPort) return false; let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} allow ${port}${protocol ? `/${protocol}` : ""}`); - return command ? command.toLowerCase().match(/(added)/gi) !== null : false; + return command ? isAddedOrUpdated(command) : false; } catch (err) { throw err; }; @@ -33,7 +33,7 @@ async function address(address: string, port?: number, protocol?: PortProtocol) }; 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; + return command ? isAddedOrUpdated(command) : false; } catch (err) { throw err; }; diff --git a/src/methods/Deny.ts b/src/methods/Deny.ts index eae7d69..fd1cd12 100644 --- a/src/methods/Deny.ts +++ b/src/methods/Deny.ts @@ -1,4 +1,4 @@ -import { checkAppropriatePort, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; +import { checkAppropriatePort, isAddedOrUpdated, checkAppropriateIP, runCommand, shouldDryRunDuringTesting } from "../Util"; import type { PortProtocol } from "../Typings"; /** @@ -11,7 +11,7 @@ async function port(port: number, protocol?: PortProtocol) { if (!checkPort) return false; let command = await runCommand(`echo "y" | sudo ufw ${shouldDryRunDuringTesting} deny ${port}${protocol ? `/${protocol}` : ""}`); - return command ? command.toLowerCase().match(/(added)/gi) !== null : false; + return command ? isAddedOrUpdated(command) : false; } catch (err) { throw err; }; @@ -33,7 +33,7 @@ async function address(address: string, port?: number, protocol?: PortProtocol) }; 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; + return command ? isAddedOrUpdated(command) : false; } catch (err) { throw err; };