#!/bin/bash

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

RED='\033[0;31m'
GREEN='\033[0;32m'
WHITE='\033[0;37m'
NEUTRAL='\033[0m'
YELLOW='\033[0;33m'

if hash lsb_release 2>/dev/null; then
        lsb_exists=true
    else
        lsb_exists=false
fi

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 - IKEv2 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 Password.\n\n"

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

printf "\n"

echo -n "2) Enter your nVpn Password and press [ENTER]: "
read VPN_PASSWORD

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

if [ -z "$VPN_PASSWORD" ]; then
	echo "Password can not be empty. Restart the script again and enter your nVpn.net Password."
	exit
fi

function set_google_dns
{
	# 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
}

function install_strongswan 
{
	# install strongswan
	if [[ "$lsb_exists" = true && $(lsb_release -i | grep -iEoc "Ubuntu") == 1 ]]; then
		STRONGSWAN_INSTALL_PATH="/etc/"
		VPN_RELOAD_CONFIG_COMMAND="ipsec restart"
		VPN_CONNECT_COMMAND="ipsec up nvpn"
                VPN_DISCONNECT_COMMAND="ipsec down nvpn"
		apt-get update -y
		apt-get install curl strongswan strongswan-plugin-eap-md5 strongswan-plugin-eap-mschapv2 -y
	elif [[ $(echo "$DISTRO_NAME" | grep -iEoc 'CentOS.*6\..*') == 1 ]]; then
		STRONGSWAN_INSTALL_PATH="/etc/strongswan/"
		VPN_RELOAD_CONFIG_COMMAND="strongswan restart"
		VPN_CONNECT_COMMAND="strongswan up nvpn"
		VPN_DISCONNECT_COMMAND="strongswan down nvpn"
		yum install epel-release -y
		yum install sudo wget curl strongswan -y
	fi
}

function paste_config
{

cat << EOF > $STRONGSWAN_INSTALL_PATH/ipsec.conf
config setup

conn nvpn
        ike=aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!
        esp=aes256-sha256,aes256-sha1,3des-sha1!
        ikelifetime=60m
        keylife=20m
        rekeymargin=3m
        keyingtries=1
        keyexchange=ikev2
        leftfirewall=yes
        leftauth=eap-md5
        eap_identity=$VPN_USERNAME
        right=$hostname
        rightauth=pubkey
        rightid="C=SO, O=nVpn, CN=nvpn.so"
        rightsubnet=0.0.0.0/0
        auto=add
        leftsourceip=%config
EOF

}

function add_secrets
{
	echo "$VPN_USERNAME : EAP \"$VPN_PASSWORD\"" > $STRONGSWAN_INSTALL_PATH/ipsec.secrets
}

function add_ca
{

cat << EOF > $STRONGSWAN_INSTALL_PATH/ipsec.d/cacerts/nvpn.pem
-----BEGIN CERTIFICATE-----
[payload removed: 1768 characters in 28 lines]
-----END CERTIFICATE-----
EOF

}

function connect
{
	$VPN_RELOAD_CONFIG_COMMAND
	sleep 6
	status=`$VPN_CONNECT_COMMAND | grep -cE "established successfully"`
	if [[ $status -eq 1 ]]; then
		echo -e "${GREEN}nVpn IKEv2 Connection successfully established, your IP is: ${YELLOW}`curl -s ifconfig.co`${NEUTRAL}"
		echo -e "${WHITE}To disconnect from VPN, use the following command: ${RED}$VPN_DISCONNECT_COMMAND${NEUTRAL}"
		echo -e "${WHITE}To connect to the VPN, use the following command: ${GREEN}$VPN_CONNECT_COMMAND${NEUTRAL}"
	else
		echo -e "${RED}Error: Something went wrong here, the connect attempt has failed!${NEUTRAL}"
	fi 
}

function get_user_data
{
	response=`curl -s -X POST -F "username=$VPN_USERNAME" -F "proto=ikev2" $CONF_DOWNLOAD_URL/memberss/conf_create.php`
	if [[ $(echo $response | grep -cE "hostname=.*") -ne 0 ]]; then
		hostname=`echo $response | awk -F "=" '{ print $2 }'`
	else
		echo -e ${RED} $response ${NEUTRAL} && exit
	fi
}

set_google_dns
install_strongswan
get_user_data
paste_config
add_secrets
add_ca
connect
