#!/bin/sh
#author:Wos
# get udp vpn configuration file ,save in current directory 'ovpn'
# auto connect vpn server list
# recommend to modify dns server when connected!
# update need proxy server,do it by yourself
WORK_DIR=$(dirname $0)
OVPN_DIR=${WORK_DIR}/ovpn
OVPN_TEMP=${WORK_DIR}/wosvpn.ovpn
PXY="http://127.0.0.1:7891"
PRELINK="https://www.vpngate.net"

if [ ! -d "$OVPN_DIR" ]; then
    mkdir -p "$OVPN_DIR"
fi

function usage(){
    echo  "update need proxy server,do it by yourself!"
    echo "usage:`basename $0` -u(update configurations)"
    echo "     :`basename $0` (connect server list)"
    exit -1
}

function logo(){
    echo -e "\033[96;40m"
    base64 -d <<< "H4sIAAAAAAAAA42NsQ3AMAgEe0/xbSq2SYX0i3j4/OFIaYMxNscDtl4z9j7+CIi/VquPsnKsLgQHQkA72dZeAlcPT65mTg9UBfgKRB5p4K34cK6pAunNDgpZgjSDKq4ZWEhPkkDvie2R/rT1AJRzjUICAQAA"|gunzip
    echo -e "\033[0m"
}

function update_config(){
while :;
do
curl --connect-timeout 5 -s -x ${PXY} --referer ${PRELINK}/en/ ${PRELINK}/en/ > /tmp/vpngate.html 
if [ $? == 0 ];then
    break
fi
done
echo -e "\033[96;40mDelete Old Configurations!\033[0m"
rm "${OVPN_DIR}"/* -rf

echo -e "\033[96;40mPing Test...\033[0m"
sleep 1
>/tmp/proxy.txt
>/tmp/proxy2.txt
cat /tmp/vpngate.html|grep -oE "fqdn=\S+&udp=([1-9]{4,5})\S+'"|sed 's/.$//'|awk -F\& '{print $2,$4,$5,$6}'|sed 's/=/ /g' > /tmp/hostlist
cat /tmp/hostlist|awk '{print $2}'|xargs -P8 -I % sh -c 'fping -c1 -t300 %'|grep bytes|awk '{print $1,$6}'|sort -k2 -g|uniq|awk '{print $1}'|xargs -P8 -I % sh -c 'cat /tmp/hostlist|grep %'|sort|uniq > /tmp/fastproxylist
cat /tmp/fastproxylist |awk '{print $2}'|xargs -P8 -I % sh -c 'curl --connect-timeout 3 -s http://ipinfo.io/%/country?token=091e5334b0f0bb|grep -E "KR|JP|TH|US|VN" >/dev/null && (echo %|tee -a /tmp/proxy.txt)'
cat /tmp/proxy.txt|xargs -I % sh -c 'cat /tmp/fastproxylist|grep % >> /tmp/proxy2.txt'
cat /tmp/proxy2.txt|awk '{print "https://www.vpngate.net/common/openvpn_download.aspx?udp=1&host="$2"&port="$4"&sid="$6"&hid="$8"&/vpngate_"$2"_udp_"$4".ovpn"}' > /tmp/vpngate
echo -e "\033[96;40mConfigurations Downloading...\033[0m"
aria2c -i /tmp/vpngate -c --allow-overwrite=true --check-certificate=false --file-allocation=none -m3 -s8 -x8 -k 1M --all-proxy=${PXY} -d ${OVPN_DIR} 
echo -e "\033[96;40mDownload Complete!\033[0m"
echo -e "\033[96;40mRun it Again without Arguments!\033[0m"
exit -1
}

logo
if [ $# == 1 ]; then
    if [ ${1} == '-u' ]; then
        update_config
    else
	usage
    fi
fi
echo -e "\033[96;40m"
echo "Start to Connect vpn server..."
echo "If the connection is stuck, press Ctrl+C to skip it."
echo -e "\033[0m"
sudo openvpn --disable-dco --compat-mode 2.3.0  --data-ciphers AES-128-CBC --data-ciphers-fallback AES-128-CBC --connect-timeout 4 --connect-retry-max 1 --config ${OVPN_TEMP} 
for file in `ls -ht "$OVPN_DIR"/*`; do
  if [ -f "${file}" ]; then
    echo -e "\033[96;40m"
    echo ${file}
    echo "If the connection is stuck, press Ctrl+C to skip it."
    echo -e "\033[0m"
    sed -i 's/^cipher/data-ciphers/g' ${file}
    cp ${file} ${OVPN_TEMP}
    sudo openvpn --disable-dco --compat-mode 2.3.0 --data-ciphers AES-128-CBC --data-ciphers-fallback AES-128-CBC --connect-timeout 4 --connect-retry-max 1 --config ${OVPN_TEMP} 
    sleep 1
  else
    usage
  fi
done