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 { 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() { export function checkSudo() {
return getuid && getuid() == 0 ? true : false; return getuid && getuid() == 0 ? true : false;

40
src/methods/Allow.ts

@ -1,10 +1,6 @@
import { exec } from "node:child_process"; import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util";
import { promisify } from "node:util";
import { checkAppropriatePort, checkAppropriateIP } from "../Util";
import type { PortProtocol } from "../Typings"; import type { PortProtocol } from "../Typings";
const promisifiedExec = promisify(exec);
/** /**
* Allow incoming requests through specific port. (root/sudo access is mandatory) * 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); let checkPort = checkAppropriatePort(port);
if (!checkPort) return false; if (!checkPort) return false;
let res = await promisifiedExec(`echo "y" | sudo ufw allow ${port}${protocol ? `/${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw allow ${port}${protocol ? `/${protocol}` : ""}`);
if (res.stderr) throw new Error(res.stderr); return command ? command.toLowerCase().match(/(added)/gi) !== null : false;
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;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };
@ -49,21 +32,8 @@ async function address(address: string, port: number, protocol?: PortProtocol) {
if (!checkPort) return false; if (!checkPort) return false;
}; };
let res = await promisifiedExec(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw allow from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`);
if (res.stderr) throw new Error(res.stderr); return command ? command.toLowerCase().match(/(added)/gi) !== null : false;
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;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

18
src/methods/Delete.ts

@ -1,6 +1,4 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
const promisifiedExec = promisify(exec);
/** /**
* Delete ufw rule(s). (root/sudo access is mandatory) * Delete ufw rule(s). (root/sudo access is mandatory)
@ -11,18 +9,8 @@ export default async function(num: number) {
num = 1; num = 1;
}; };
let res = await promisifiedExec(`echo "y" | sudo ufw delete ${num}`); let command = await runCommand(`echo "y" | sudo ufw delete ${num}`);
return command !== null;
if (res.stderr) {
throw new Error(res.stderr);
};
if (res.stdout) {
return true;
} else {
console.log(res.stdout);
return false;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

42
src/methods/Deny.ts

@ -1,10 +1,6 @@
import { exec } from "node:child_process"; import { checkAppropriatePort, checkAppropriateIP, runCommand } from "../Util";
import { promisify } from "node:util";
import { checkAppropriatePort, checkAppropriateIP } from "../Util";
import type { PortProtocol } from "../Typings"; import type { PortProtocol } from "../Typings";
const promisifiedExec = promisify(exec);
/** /**
* Deny incoming requests through specific port. (root/sudo access is mandatory) * 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); let checkPort = checkAppropriatePort(port);
if (!checkPort) return false; if (!checkPort) return false;
let res = await promisifiedExec(`echo "y" | sudo ufw deny ${port}${protocol ? `/${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw deny ${port}${protocol ? `/${protocol}` : ""}`);
if (res.stderr) { return command ? command.toLowerCase().match(/(added)/gi) !== null : false;
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;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };
@ -51,21 +32,8 @@ async function address(address: string, port: number, protocol?: PortProtocol) {
if (!checkPort) return false; if (!checkPort) return false;
}; };
let res = await promisifiedExec(`echo "y" | sudo ufw deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`); let command = await runCommand(`echo "y" | sudo ufw deny from ${address} ${port ? `to any port ${port}` : ""} ${protocol ? `proto ${protocol}` : ""}`);
if (res.stderr) throw new Error(res.stderr); return command ? command.toLowerCase().match(/(added)/gi) !== null : false;
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;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

15
src/methods/Disable.ts

@ -1,21 +1,12 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
const promisifiedExec = promisify(exec);
/** /**
* Disable ufw. (root/sudo access is mandatory) * Disable ufw. (root/sudo access is mandatory)
*/ */
export default async function() { export default async function() {
try { try {
let res = await promisifiedExec(`echo "y" | sudo ufw disable`); let command = await runCommand(`echo "y" | sudo ufw disable`);
return command !== null;
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

15
src/methods/Enable.ts

@ -1,19 +1,10 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
const promisifiedExec = promisify(exec);
export default async function() { export default async function() {
try { try {
// https://serverfault.com/a/790150 // https://serverfault.com/a/790150
let res = await promisifiedExec(`echo "y" | sudo ufw enable`); let command = await runCommand(`echo "y" | sudo ufw enable`);
return command !== null;
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

15
src/methods/Logging.ts

@ -1,22 +1,13 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
import type { LoggingType } from "../Typings"; import type { LoggingType } from "../Typings";
const promisifiedExec = promisify(exec);
/** /**
* Set/toggle UFW logging. (root/sudo access is mandatory) * Set/toggle UFW logging. (root/sudo access is mandatory)
*/ */
export default async function(type: LoggingType) { export default async function(type: LoggingType) {
try { try {
let res = await promisifiedExec(`sudo ufw logging ${type}`); let command = await runCommand(`sudo ufw logging ${type}`);
return command !== null;
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

15
src/methods/Reload.ts

@ -1,21 +1,12 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
const promisifiedExec = promisify(exec);
/** /**
* Reloads firewall. (root/sudo access is mandatory) * Reloads firewall. (root/sudo access is mandatory)
*/ */
export default async function() { export default async function() {
try { try {
let res = await promisifiedExec("sudo ufw reload"); let command = await runCommand("sudo ufw reload");
return command !== null;
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) { } catch (err) {
throw err; throw err;
}; };

15
src/methods/Reset.ts

@ -1,21 +1,12 @@
import { exec } from "node:child_process"; import { runCommand } from "../Util";
import { promisify } from "node:util";
const promisifiedExec = promisify(exec);
/** /**
* Disables and resets firewall to installation defaults. No prompt. Use this wisely. (root/sudo access is mandatory)= * Disables and resets firewall to installation defaults. No prompt. Use this wisely. (root/sudo access is mandatory)=
*/ */
export default async function() { export default async function() {
try { try {
let res = await promisifiedExec("sudo ufw --force reset"); let command = await runCommand("sudo ufw --force reset");
return command !== null;
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) { } catch (err) {
throw 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"; import type { ParsedStatus } from "../Typings";
const promisifiedExec = promisify(exec); import { runCommand } from "../Util";
/** /**
* List of currently activated ufw. (root/sudo access is mandatory) * List of currently activated ufw. (root/sudo access is mandatory)
*/ */
export default async function(raw?: boolean): Promise<string | ParsedStatus[] | null> { export default async function(raw?: boolean): Promise<string | ParsedStatus[] | null> {
try { try {
let res = await promisifiedExec("sudo ufw status"); let command = await runCommand("sudo ufw status");
if (res.stderr) throw new Error(res.stderr); if (command) {
if (raw) return command;
if (res.stdout) {
if (raw) return res.stdout;
let list = []; let list = [];
// parsing // 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 []; if (!parsedStatus.length) return [];
let findAfterFrom = parsedStatus.findIndex(x => x == "----"); let findAfterFrom = parsedStatus.findIndex(x => x == "----");

Loading…
Cancel
Save