Browse Source

added logging method

develop
ray-1337 3 years ago
parent
commit
a158f77ab0
  1. 4
      docs/DOCS.md
  2. 4
      index.d.ts
  3. 3
      src/Main.js
  4. 41
      src/methods/logging.js

4
docs/DOCS.md

@ -55,3 +55,7 @@ Disables and resets firewall to installation defaults. **No prompt. Use this wis
## .status(raw?: boolean) ## .status(raw?: boolean)
List of currently activated UFW. List of currently activated UFW.
> returns `Promise<string | {to: string, action: string, from: string }[]>` > returns `Promise<string | {to: string, action: string, from: string }[]>`
## .logging(type: "off" | "on" | "low" | "medium" | "high" | "full")
Set/toggle UFW logging.
> returns `Promise<Boolean>`

4
index.d.ts

@ -13,6 +13,8 @@ declare const nodeUfw: {
status: (raw?: boolean) => Promise<string | Array<ParsedStatus>>; status: (raw?: boolean) => Promise<string | Array<ParsedStatus>>;
logging: (type: LoggingType) => Promise<Boolean>;
allow: { allow: {
port: (port: number, protocol?: "udp" | "tcp") => Promise<Boolean>; port: (port: number, protocol?: "udp" | "tcp") => Promise<Boolean>;
address: (address: string, port?: number, protocol?: "udp" | "tcp") => Promise<Boolean>; address: (address: string, port?: number, protocol?: "udp" | "tcp") => Promise<Boolean>;
@ -24,6 +26,8 @@ declare const nodeUfw: {
}; };
}; };
type LoggingType = 'off' | 'on' | 'low' | 'medium' | 'high' | 'full';
interface ParsedStatus { interface ParsedStatus {
to: string; to: string;
action: string; action: string;

3
src/Main.js

@ -8,5 +8,6 @@ module.exports = {
status: require("./methods/status"), status: require("./methods/status"),
delete: require("./methods/delete"), delete: require("./methods/delete"),
reset: require("./methods/reset"), reset: require("./methods/reset"),
reload: require("./methods/reload") reload: require("./methods/reload"),
logging: require("./methods/logging")
}; };

41
src/methods/logging.js

@ -0,0 +1,41 @@
const {exec} = require("child_process");
const {promisify} = require("util");
const promisifiedExec = promisify(exec);
const util = require("../Util");
/**
* Set/toggle UFW logging. (root/sudo access is mandatory)
* @param {'off' | 'on' | 'low' | 'medium' | 'high' | 'full'} type A type of UFWlogging.
* @returns {Promise<Boolean>} Returns a boolean.
*/
module.exports = async function(type) {
util.checkSudo();
util.checkNodeVersion();
util.checkPlatform();
await util.checkPlatformExact();
try {
if (!type) throw new Error("Missing type input.");
if (typeof type !== "string") throw new Error("The type must be type of string.");
type = type.toLowerCase();
// https://manpages.ubuntu.com/manpages/focal/en/man8/ufw.8.html#logging
let appropriateType = ['off', 'on', 'low', 'medium', 'high', 'full'];
if (!appropriateType.includes(type)) {
throw new Error(`Invalid type. Currently supported: ${appropriateType.join(", ")}`);
};
let res = await promisifiedExec(`sudo ufw logging ${type}`);
if (res.stderr) throw new Error(res.stderr);
if (res.stdout) {
return true;
} else {
return false;
};
} catch (err) {
throw new Error(err);
};
};
Loading…
Cancel
Save