diff --git a/docs/DOCS.md b/docs/DOCS.md index 74cc4e9..ec658d0 100644 --- a/docs/DOCS.md +++ b/docs/DOCS.md @@ -54,4 +54,8 @@ Disables and resets firewall to installation defaults. **No prompt. Use this wis ## .status(raw?: boolean) List of currently activated UFW. -> returns `Promise` \ No newline at end of file +> returns `Promise` + +## .logging(type: "off" | "on" | "low" | "medium" | "high" | "full") +Set/toggle UFW logging. +> returns `Promise` \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 25bce8e..9f9cc5a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,6 +13,8 @@ declare const nodeUfw: { status: (raw?: boolean) => Promise>; + logging: (type: LoggingType) => Promise; + allow: { port: (port: number, protocol?: "udp" | "tcp") => Promise; address: (address: string, port?: number, protocol?: "udp" | "tcp") => Promise; @@ -24,6 +26,8 @@ declare const nodeUfw: { }; }; +type LoggingType = 'off' | 'on' | 'low' | 'medium' | 'high' | 'full'; + interface ParsedStatus { to: string; action: string; diff --git a/src/Main.js b/src/Main.js index c834fb6..b091dcd 100644 --- a/src/Main.js +++ b/src/Main.js @@ -8,5 +8,6 @@ module.exports = { status: require("./methods/status"), delete: require("./methods/delete"), reset: require("./methods/reset"), - reload: require("./methods/reload") + reload: require("./methods/reload"), + logging: require("./methods/logging") }; \ No newline at end of file diff --git a/src/methods/logging.js b/src/methods/logging.js new file mode 100644 index 0000000..7b45950 --- /dev/null +++ b/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} 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); + }; +}; \ No newline at end of file