ec3f3d6
#!/bin/bash
ec3f3d6
# transparent_proxying  This shell script takes care of starting and stopping
ec3f3d6
#               transparent proxying
ec3f3d6
#
ec3f3d6
# chkconfig: - 91 24
ec3f3d6
# description: transparent proxying: this will force all web traffic to be \
ec3f3d6
#	redirected to the squid proxy server. It will only work if squid \
ec3f3d6
#	is running
ec3f3d6
# pidfile: /var/run/squid.pid
ec3f3d6
	
ec3f3d6
# Source function library.
ec3f3d6
. /etc/init.d/functions
ec3f3d6
ec3f3d6
start() {
ec3f3d6
	echo -n "Starting transparent proxying: "
ec3f3d6
	if [ -f /var/run/squid.pid ]
ec3f3d6
	then
ec3f3d6
		# Turn on IP forwarding
ec3f3d6
		echo 1 > /proc/sys/net/ipv4/ip_forward
ec3f3d6
ec3f3d6
		# Turn on transparent proxy redirect
ec3f3d6
		/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3128
ec3f3d6
       		echo_success
ec3f3d6
	else
ec3f3d6
		echo -n "Squid is not running! "
ec3f3d6
       		echo_failure
ec3f3d6
	fi
ec3f3d6
	echo
ec3f3d6
	return 0
ec3f3d6
}	
ec3f3d6
stop() {
ec3f3d6
	echo -n "Stopping transparent proxying: "
ec3f3d6
	echo 0 > /proc/sys/net/ipv4/ip_forward
ec3f3d6
	# Turn off transparent proxy redirect
ec3f3d6
	/sbin/iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3128
ec3f3d6
  	echo_success
ec3f3d6
	echo
ec3f3d6
	return 0
ec3f3d6
}	
ec3f3d6
status() {
ec3f3d6
	if [ "`/sbin/iptables -L -n -t nat | grep '80 redir ports 3128'`" ]
ec3f3d6
	then
ec3f3d6
		echo "Transparent proxying is active"
ec3f3d6
		return 0
ec3f3d6
	else
ec3f3d6
		echo "Transparent proxying is inactive"
ec3f3d6
		return 0
ec3f3d6
	fi
ec3f3d6
}	
ec3f3d6
ec3f3d6
ec3f3d6
restart() {
ec3f3d6
	    stop
ec3f3d6
	    start
ec3f3d6
}
ec3f3d6
ec3f3d6
case "$1" in
ec3f3d6
  start)
ec3f3d6
  	start
ec3f3d6
	;;
ec3f3d6
  status)
ec3f3d6
  	status
ec3f3d6
	;;
ec3f3d6
  stop)
ec3f3d6
  	stop
ec3f3d6
	;;
ec3f3d6
  restart|reload)
ec3f3d6
  	restart
ec3f3d6
	;;
ec3f3d6
  *)
ec3f3d6
	echo "*** Usage: {start|stop|restart}"
ec3f3d6
	exit 1
ec3f3d6
esac
ec3f3d6
ec3f3d6
exit $?