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 +