ray-1337
2 years ago
No known key found for this signature in database
GPG Key ID: DED41DCC150FCD32
10 changed files with
21 additions and
19 deletions
src/Util.ts
src/methods/Allow.ts
src/methods/Delete.ts
src/methods/Deny.ts
src/methods/Disable.ts
src/methods/Enable.ts
src/methods/Logging.ts
src/methods/Reload.ts
src/methods/Reset.ts
src/methods/Status.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 ) ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;
@ -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 ;