#!/bin/bash

OPTION=$1

if [ -z $OPTION ]
then
	echo "Please use:"
	echo "./whonix_vpn.sh start"
	echo "./whonix_vpn.sh stop"
	
elif [ $OPTION == "start" ]
then
	echo "Startins..."
	if [ ! `id -u` = 0 ] ; then
			echo "This script needs to be run using 'sudo SCRIPT' or in 'root terminal'"
	  read -n1 -r -p "Press any key to exit..." key
	  exit 1
	fi
	
	if [ ! -f /sbin/resolvconf ]
	then
	  echo
	  echo
	  echo "    **********************  Resolvconf install  **********************"
	  echo
	  apt-get update
	  apt-get install -y resolvconf
	fi
	

	  echo
	  echo
	  echo "    **********************  OpenVPN install  **********************"
	  echo
	  apt-get update
	  apt-get purge openvpn -y
	  
	OPENVPN_F=`echo openvpn*`
	if [ ! -f $OPENVPN_F ]; then
	    echo "File openvpn not found!"
	fi

	LIBLZO_F=`echo liblzo*`
	if [ ! -f $LIBLZO_F ]; then
	    echo "File liblzo not found!"
	fi

	LIBPK_F=`echo libpkcs*`
	if [ ! -f $LIBPK_F ]; then
	    echo "File libpkcs not found!"
	fi
	LIBSSL_F=`echo libssl*`
	if [ ! -f $LIBSSL_F ]; then
	    echo "File libssl not found!"
	fi

	dpkg -i $LIBSSL_F
	dpkg -i $LIBPK_F
	dpkg -i $LIBLZO_F
	dpkg -i $OPENVPN_F

	
	if [[ ! $(cat /etc/whonix_firewall.d/50_user.conf | grep "VPN_FIREWALL=1") ]]
	then
	  echo "VPN_FIREWALL=1" > /etc/whonix_firewall.d/50_user.conf
	fi
	
	if [[ ! $(cat /etc/sudoers.d/tunnel_unpriv | grep "vpnsn") ]]
	then
	  echo "tunnel ALL=(ALL) NOPASSWD: /bin/ip" > /etc/sudoers.d/tunnel_unpriv
	  echo "tunnel ALL=(ALL) NOPASSWD: /usr/sbin/openvpn *" >> /etc/sudoers.d/tunnel_unpriv
	  echo "#vpnsn" >> /etc/sudoers.d/tunnel_unpriv
	  echo 'Defaults:tunnel !requiretty' >> /etc/sudoers.d/tunnel_unpriv
	fi
	
	
	
	cp *.crt *.key vpn.ovpn auth.txt /etc/openvpn
	
	cd /etc/openvpn
	
	cp vpn.ovpn openvpn.conf
	
	rm vpn.ovpn
	
	if [[ ! $(cat /etc/openvpn/openvpn.conf | grep "auth.txt") ]]
	then
	  sed -i -e 's/auth-user-pass/auth-user-pass auth.txt/g' /etc/openvpn/openvpn.conf
	fi
	
	if [[ ! $(cat /etc/openvpn/openvpn.conf | grep "tun0") ]]
	then
	  sed -i -e 's/dev tun/dev tun0/g' /etc/openvpn/openvpn.conf
	fi
	
	if [[ ! $(cat /etc/openvpn/openvpn.conf | grep "user tunnel") ]]
	then
	  echo "user tunnel" >> /etc/openvpn/openvpn.conf
	fi
	
	if [[ ! $(cat /etc/openvpn/openvpn.conf | grep "ip_unpriv") ]]
	then
	  echo "iproute /usr/bin/ip_unpriv" >> /etc/openvpn/openvpn.conf
	fi
	
	if [[ ! $(cat /etc/openvpn/openvpn.conf | grep "update-resolv-conf") ]]
	then
	  echo "up \"/etc/openvpn/update-resolv-conf script_type=up dev=tun0\"" >> /etc/openvpn/openvpn.conf

	  echo "down \"/etc/openvpn/update-resolv-conf script_type=down dev=tun0\"" >> /etc/openvpn/openvpn.conf
	fi
	
	if [[ ! $(cat /etc/rc.local	| grep "tunnel:tunnel") ]]
	then
	  echo "#!/bin/sh -e" > /etc/rc.local
	  echo "sudo chown -R tunnel:tunnel /etc/openvpn" >> /etc/rc.local
	  echo "sudo chown -R tunnel:tunnel /run/openvpn" >> /etc/rc.local
	  echo "sudo chown --recursive root:tunnel /run/resolvconf" >> /etc/rc.local
	  echo "sudo chmod --recursive 775 /run/resolvconf" >> /etc/rc.local
	  echo "exit 0" >> /etc/rc.local
	fi

	sudo chmod +x /etc/rc.local

	sudo systemctl enable rc-local.service
	sudo systemctl start rc-local.service
	
	sudo chown -R tunnel:tunnel /etc/openvpn
	sudo chown -R tunnel:tunnel /run/openvpn
	
	sudo chown --recursive root:tunnel /run/resolvconf
	
	sudo chmod --recursive 775 /run/resolvconf
	
	sudo cp /lib/systemd/system/openvpn@.service /lib/systemd/system/openvpn@openvpn.service
	
	sudo systemctl enable openvpn@openvpn
	
	sudo systemctl start openvpn@openvpn
	
	
	

	
	
	  echo
	  echo
	  echo "    **********************  VPN Success CONNECTED  **********************"
	  echo
	  
	  service tor restart

	
elif [ $OPTION == "stop" ]
then
	echo "Stopping..."
	

	rm /etc/whonix_firewall.d/50_user.conf
	
	rm /etc/sudoers.d/tunnel_unpriv
	
	rm /usr/lib/tmpfiles.d/50_openvpn_unpriv.conf
	
	
	cd /etc/openvpn
	rm  *.crt *.key openvpn.conf auth.txt
	
	 echo "#!/bin/sh -e" > /etc/rc.local

	  echo "exit 0" >> /etc/rc.local
	
	systemctl disable openvpn@openvpn
	
	sudo systemctl stop openvpn@openvpn

	echo
	  echo
	  echo "    **********************  VPN Success DELETED  **********************"
	  echo
fi
