Browse Source

implement runCommand function instead

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

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

40
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;
};

18
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;
};

42
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;
};

15
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;
};

15
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;
};

15
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;
};

15
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;
};

15
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;
};

14
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<string | ParsedStatus[] | null> {
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 == "----");

Loading…
Cancel
Save