Browse Source

use --dry-run while testing

develop
ray-1337 2 years ago
parent
commit
5c4ba65f9f
No known key found for this signature in database GPG Key ID: DED41DCC150FCD32
  1. 2
      src/Util.ts
  2. 4
      src/methods/Allow.ts
  3. 4
      src/methods/Delete.ts
  4. 6
      src/methods/Deny.ts
  5. 4
      src/methods/Disable.ts
  6. 4
      src/methods/Enable.ts
  7. 4
      src/methods/Logging.ts
  8. 4
      src/methods/Reload.ts
  9. 4
      src/methods/Reset.ts
  10. 4
      src/methods/Status.ts

2
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);

4
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;

4
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;

6
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;

4
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;

4
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;

4
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;

4
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;

4
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;

4
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<string | ParsedStatus[] | null> {
try {
let command = await runCommand("sudo ufw status");
let command = await runCommand(`sudo ufw ${shouldDryRunDuringTesting} status`);
if (command) {
if (raw) return command;

Loading…
Cancel
Save