| 1 | #!/bin/sh |
| 2 | # |
| 3 | # manage iptables rules for the given port |
| 4 | # |
| 5 | |
| 6 | [ $# -lt 1 ] && exit |
| 7 | action=$1 |
| 8 | port=${2:-9929} |
| 9 | testip() { |
| 10 | local chain=$1 |
| 11 | iptables -L $chain | grep -wq ^DROP.*$port |
| 12 | } |
| 13 | logcmd() { |
| 14 | logger -p local7.info "$*" |
| 15 | eval $* |
| 16 | } |
| 17 | |
| 18 | case "$action" in |
| 19 | start) |
| 20 | logcmd iptables -D INPUT -p udp --dport $port -j DROP |
| 21 | logcmd iptables -D OUTPUT -p udp --dport $port -j DROP |
| 22 | logcmd iptables -D INPUT -p udp --sport $port -j DROP |
| 23 | logcmd iptables -D OUTPUT -p udp --sport $port -j DROP |
| 24 | ;; |
| 25 | stop) |
| 26 | testip INPUT && { |
| 27 | echo "packets from/to $port already being dropped!" |
| 28 | exit |
| 29 | } |
| 30 | logcmd iptables -A INPUT -p udp --dport $port -j DROP |
| 31 | logcmd iptables -A OUTPUT -p udp --dport $port -j DROP |
| 32 | logcmd iptables -A INPUT -p udp --sport $port -j DROP |
| 33 | logcmd iptables -A OUTPUT -p udp --sport $port -j DROP |
| 34 | ;; |
| 35 | esac |