hvpn-node3/scripts/set_rules.sh

40 lines
799 B
Bash
Raw Normal View History

#!/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