Current File : //etc/rc2.d/S01ndn-iptables
#! /bin/sh
# pushed by ansible ndn_iptables_role
### BEGIN INIT INFO
# Provides: ndn-iptables
# Required-Start: $remote_fs $syslog $networking $procps
# Required-Stop:     $remote_fs $syslog
# Default-Start: 2 3 5
# Default-Stop: 0 1 4 6
# Short-Description: iptables starting
# Description: manage iptables services.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Debian usrmerge fix for Ubuntu 19+
IPPATH=/usr/sbin
if [ -e /sbin/iptables ]
then
IPPATH=/sbin
fi

# find ipset
IPSET=/sbin/ipset
if [ -e /usr/sbin/ipset ]
then
IPSET=/usr/sbin/ipset
fi

SAVE=$IPPATH/iptables-save
RESTORE=$IPPATH/iptables-restore
IPTABLES=$IPPATH/iptables
SAVE6=$IPPATH/ip6tables-save
RESTORE6=$IPPATH/ip6tables-restore
IPTABLES6=$IPPATH/ip6tables
IPSETDIR=/var/lib/ipset
NAME=ndn-iptables
DESC=ndn-iptables
BASERULES=/var/lib/iptables/base.rules
MYRULES=/var/lib/iptables/my.rules
AUTORULES=/var/lib/iptables/auto.rules
BASERULES6=/var/lib/ip6tables/base.rules
MYRULES6=/var/lib/ip6tables/my.rules
AUTORULES6=/var/lib/ip6tables/auto.rules

LISTS=$(/bin/ls /var/lib/ipset | grep -E "\.ipset$"|sort)

test -x $SAVE || exit 1
test -x $RESTORE || exit 1
test -f $BASERULES || exit 1
test -f $MYRULES || exit 1
test -x $SAVE6 || exit 1
test -x $RESTORE6 || exit 1
test -f $BASERULES6 || exit 1
test -f $MYRULES6 || exit 1
test -x $IPSET || exit 1

# Include ndn-iptables defaults if available
if [ -f /etc/default/ndn-iptables ] ; then
	# shellcheck source=files/etc/init.d/ndn-iptables
	. /etc/default/ndn-iptables
fi

set -e

case "$1" in
	start)
		echo "Starting $DESC ... "
		for L in $LISTS
		do
			echo "Restoring ipset $L ..."
			$IPSET restore -f $IPSETDIR/"$L" -exist || true
			echo "done"
		done
		cat $BASERULES $MYRULES | awk -F# '{print $1}' | $RESTORE
		cat $BASERULES6 $MYRULES6 | awk -F# '{print $1}' | $RESTORE6
		echo "$NAME."
	;;
	stop)
		echo "Stopping $DESC ... "
		$IPTABLES -F || true
		$IPTABLES6 -F || true

		echo "Stopping ipset ..."
		$IPSET flush || true
		echo "done"
	;;
	restart|force-reload)
		echo "Restarting $DESC ... "
		$0 stop
		sleep 1
		$0 start
	;;
	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart}" >&2
		exit 1
	;;
esac

exit 0