#!/bin/bash

_BASEDIR="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}" | xargs dirname)"
LOGFILE="$(basename $0).log"

function printme
{
	( [ $1 == 0 ] && printf "[OK]\t $2\n"  || printf "[FAILED]\t $2\n" ) | tee -a $LOGFILE
}

DISTRO_VERSION=`cat /etc/*release | grep -o "VERSION_ID.*" | awk -F"=" '{print $2}' | sed -e 's/\"//g'`
if [ -z "$DISTRO_VERSION" ]; then
	DISTRO_VERSION=`cat /etc/*release | head -n 1 | grep -oE "6"`
	elif [ -z "$DISTRO_VERSION" ]; then
		DISTRO_VERSION=`lsb_release -r | awk '{print $2}'`
fi

DISTRO_NAME=`cat /etc/*release | grep -h "^PRETTY_NAME" | awk -F"=" '{print $2}'`
if [ -z "$DISTRO_NAME" ]; then
        DISTRO_NAME=`cat /etc/*release* | head -n 1`
fi

CONF_DOWNLOAD_URL="https://nvpn.net"

printf "nVpn.net setup script has detected following Version: $DISTRO_VERSION with Distro $DISTRO_NAME. During Setup procedure you need to enter two details which is your nVpn Username and your forwarded Port.\n\n"

echo -n "1) Enter your nVpn Username and press [ENTER]: "
read VPN_USERNAME

printf "\n"

echo -n "2) Enter your desired Port now and press [ENTER]. Make sure the Port is already added in nVpn.net Members Area. Important: Only with this port (\"ssh VPN_IP -p Port\") you will be able to reach your server again, after the VPN connection is established: "
read VPN_PORTFORWARD

if [ -z "$VPN_USERNAME" ]; then
	echo "Username can not be empty. Restart the script again and enter your nVpn.net Username."
	printme 1 "Username\t -> Username empty!"
	exit
fi

if [ -z "$VPN_PORTFORWARD" ]; then
	echo "Port can not be empty. Without a port you can not reach your server again, after the VPN connection is active. Restart the script again and enter your added port from nVpn.net Members Area."
	printme 1 "Port\t -> Port is empty!"
	exit
fi

re='^[0-9]+$'
if ! [[ $VPN_PORTFORWARD =~ $re ]] ; then
	echo "error: Entered Port is not a number." >&2; exit 1
	printme 1 "Port\t -> Entered Port is not a number!"	
	exit
fi

# outcomment current DNS and set google DNS as new nameservers
sed -i -e 's/nameserver/#&/g' /etc/resolv.conf && echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" >> /etc/resolv.conf

# install openvpn and some other useful stuff
if [[ $(lsb_release -i | grep -iEoc "Ubuntu") == 1 ]]; then
	apt-get install sudo wget openvpn unzip curl openssl -y
	else
		yum install epel-release -y
		yum install sudo wget openvpn unzip curl openssl -y
fi

#service openvpn stop
cd /etc/openvpn/

response=`curl -X POST -F "username=$VPN_USERNAME" $CONF_DOWNLOAD_URL/memberss/conf_create.php`
if [ ! -z "$response" ]; then
	echo $response && exit
fi

wget $CONF_DOWNLOAD_URL/tools/configs/$VPN_USERNAME/config.zip
unzip config.zip
rm -f config.zip
ls -ls

# flush iptables ifirewall rules to be sure
iptables_path=`which iptables`
$iptables_path -P INPUT ACCEPT
$iptables_path -P OUTPUT ACCEPT
$iptables_path -F

# backup current sshd_config, outcomment previous ListenAddress and add new one with "ListenAddress 0.0.0.0" and replace old ssh port with your new "VPN_PORTFORWARD" port.
/bin/cp -Rp /etc/ssh/sshd_config /etc/ssh/sshd_config_backup
sed -i -e "s/Port .*/Port $VPN_PORTFORWARD/g" /etc/ssh/sshd_config
sed -i -e 's/^#Port/Port/g' /etc/ssh/sshd_config
sed -i -e 's/ListenAddress/#ListenAddress/g' /etc/ssh/sshd_config
sed -i -e '/Port/a ListenAddress 0.0.0.0' /etc/ssh/sshd_config

# be careful here, double check all previous settings until here, ssh will be restarted now and afterwards ssh connect will look differently "ssh YourServerIP -p9741"
service sshd restart
netstat -tulpn | grep ssh

# connect to nVPN now - be careful here, after OpenVPN connection is established, you will be dropped from this terminal session and the new ssh connect has to be made differently "ssh YourVPNIP -p9741"
# enter your correct Username and Passsword when it prompts
touch /var/log/openvpn.log
sudo openvpn --cd /etc/openvpn --config $VPN_USERNAME.ovpn > /var/log/openvpn.log

# used for verifying and debugging purposes (this is your OpenVPN connection log)
tail -f /var/log/openvpn.log

# find out your current external IP - this command must show your VPN IP if OpenVPN connection has been established
dig +short myip.opendns.com @resolver1.opendns.com
