#!/bin/sh

# chkconfig: 2345 13 89
# description: NAT
. /etc/init.d/functions

case "$1" in
	start)
		$0 stop
		echo -n "Starting NAT: "
			iptables -I PREROUTING -t nat -s 192.168.0.13 -p tcp --dport 80 -j DNAT --to 192.168.3.2:3128 \
			&& iptables -I PREROUTING -t nat -s 192.168.0.122 -p tcp --dport 80 -j DNAT --to 192.168.3.2:3128 \
			&& iptables -I PREROUTING -t nat -s 192.168.0.202 -p tcp --dport 80 -j DNAT --to 192.168.3.2:3128 \
			&& iptables -A PREROUTING -t nat -d 192.168.3.2 -i eth0 -p udp -m udp --dport 1194 -j DNAT --to-destination 192.168.0.202:1194 \
			&& iptables -I PREROUTING -t nat -s 192.168.0.73 -p tcp --dport 80 -j DNAT --to 192.168.3.2:3128 \
			&& iptables -I PREROUTING -t nat -s 192.168.0.79 -p tcp --dport 80 -j DNAT --to 192.168.3.2:3128 \
			&& echo 1 > /proc/sys/net/ipv4/ip_forward \
			&& iptables -t nat -A POSTROUTING -s 192.168.0.13 -o eth0 -j MASQUERADE \
			&& iptables -t nat -A POSTROUTING -s 192.168.0.122 -o eth0 -j MASQUERADE \
			&& iptables -t nat -A POSTROUTING -s 192.168.0.202 -o eth0 -j MASQUERADE \
			&& iptables -t nat -A POSTROUTING -s 192.168.0.73 -o eth0 -j MASQUERADE \
			&& iptables -t nat -A POSTROUTING -s 192.168.0.79 -o eth0 -j MASQUERADE \
			&& iptables -t nat -A POSTROUTING -s 192.168.3.2 -o eth1 -j MASQUERADE \
			&& success
		echo
		;;
	stop)
		echo -n "Stopping NAT: "
			echo 0 > /proc/sys/net/ipv4/ip_forward \
                        && iptables -t filter -F \
                        && iptables -t filter -X \
                        && iptables -t nat -F \
                        && iptables -t nat -X \
                        && success
		echo
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: nat {start|stop|restart}"
		exit 1
		;;
esac

exit $RETVAL
