From 825424a1de958e7eb4f50320e865c4475bc965ff Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 31 Mar 2024 22:57:32 +0300 Subject: [PATCH] scripts: tools for iptables rules Signed-off-by: HeshamTB --- scripts/set_rules.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 scripts/set_rules.sh diff --git a/scripts/set_rules.sh b/scripts/set_rules.sh new file mode 100755 index 0000000..123130a --- /dev/null +++ b/scripts/set_rules.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +iptables=iptables + +cmd="${1}" +wgIface="${2}" +uplinkIface="${3}" + +cmd() { + echo "[#] $*" >&2 + "$@" +} + +add_rules() { + trap 'rm_rules; exit' INT TERM EXIT + cmd ${iptables} -A FORWARD -i ${wgIface} -j ACCEPT + cmd ${iptables} -A FORWARD -o ${wgIface} -j ACCEPT + cmd ${iptables} -t nat -A POSTROUTING -o ${uplinkIface} -j MASQUERADE || exit 1 + trap - INT TERM EXIT +} + +rm_rules() { + cmd ${iptables} -D FORWARD -i ${wgIface} -j ACCEPT + cmd ${iptables} -D FORWARD -o ${wgIface} -j ACCEPT + cmd ${iptables} -t nat -D POSTROUTING -o ${uplinkIface} -j MASQUERADE +} + +if [ "${cmd}" == "set" ] +then + add_rules + +elif [ "${cmd}" == "unset" ]; +then + rm_rules +else + # cat << "Invalid command. Use set or unset" >&2 + echo "Invalid command. Use set or unset" +fi +