From b747e646a0ad9061ea1254a816f0e93dfd435aa1 Mon Sep 17 00:00:00 2001 From: ray-1337 <33544674+ray-1337@users.noreply.github.com> Date: Mon, 11 Apr 2022 21:27:49 +0200 Subject: [PATCH] added allow method --- src/methods/allow.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/methods/allow.js diff --git a/src/methods/allow.js b/src/methods/allow.js new file mode 100644 index 0000000..d714395 --- /dev/null +++ b/src/methods/allow.js @@ -0,0 +1,77 @@ +const {exec} = require("child_process"); +const {promisify} = require("util"); +const promisifiedExec = promisify(exec); +const util = require("../Util"); + +module.exports.port = async function (port, protocol) { + try { + if (!port) throw new Error("Missing port input."); + if (typeof port !== "number") throw new Error("The port must be type of number."); + + // check port range + let checkPort = util.checkAppropriatePort(port); + if (!checkPort) return false; + + if (protocol) { + if (typeof protocol !== "string") throw new Error("The protocol must be type of string."); + if (protocol !== "tcp" || protocol !== "udp") throw new Error('The protocol must be either "tcp" or "udp"'); + }; + + let res = await promisifiedExec(`echo "y" | sudo ufw allow ${port} ${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; + }; + } catch (err) { + throw new Error(err); + }; +}; + +module.exports.address = async function (address, port, protocol) { + try { + // address validation + if (!address) throw new Error("Missing address input."); + if (typeof address !== "string") throw new Error("The address must be type of string."); + let checkAddress = util.checkAppropriateIP(address); + if (!checkAddress) return false; + + // port validation + if (typeof port !== "number") throw new Error("The port must be type of number."); + let checkPort = util.checkAppropriatePort(port); + if (!checkPort) return false; + + // protocol (tcp/udp) validation + if (protocol) { + if (typeof protocol !== "string") throw new Error("The protocol must be type of string."); + if (protocol !== "tcp" || protocol !== "udp") throw new Error('The protocol must be either "tcp" or "udp"'); + }; + + 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; + }; + } catch (err) { + throw new Error(err); + }; +}; \ No newline at end of file