dotfiles

Dash Eclipse's dotfiles
git clone git://ezup.dev/dotfiles.git
Log | Files | Refs | README | LICENSE

vpngate-fzf.sh (3225B)


      1#!/bin/sh
      2
      3CSV="/tmp/vpngate.csv"
      4
      5# check dependencies
      6which curl fzf tput sed column cut base64 >/dev/null || exit 1
      7
      8# download vpngate.csv
      9
     10if [ ! -f $CSV ]; then
     11	echo "$(tput bold; tput setaf 4)$CSV$(tput setaf 1) not found$(tput sgr0), do you want to download it?"
     12	read -p "Press $(tput bold)Enter$(tput sgr0) to continue... " -r ANSWER && [ "$ANSWER" = "" ] || { echo "$(tput bold; tput setaf 1)Canceled$(tput sgr0)"; exit 0; }
     13	curl -o "$CSV" --progress-bar 'https://www.vpngate.net/api/iphone/'
     14fi
     15
     16
     17# echo "$(seq -s, 1 15)\n$(sed -n 2p vpngate.csv | sed 's/^#HostName,/HostName,/')" | column -s',' -t
     18# 1         2   3      4     5      6            7             8               9       10          11            12       13        14       15
     19# HostName  IP  Score  Ping  Speed  CountryLong  CountryShort  NumVpnSessions  Uptime  TotalUsers  TotalTraffic  LogType  Operator  Message  OpenVPN_ConfigData_Base64
     20
     21get_csv() {
     22	echo '0,Hostname,Score,Ping,Speed,Country,Code,Sessions,Uptime,Users,Traffic,Log,Operator,Message'
     23	local i=0; sed -e '
     24	1,2d;$d
     25	s/weeks,/ weeks,/
     26	s/^#HostName,/HostName,/
     27	s/,Korea Republic of,/,South Korea,/
     28	s/,Russian Federation,/,Russia,/
     29	s/,Viet Nam,/,Vietnam,/
     30	' $CSV \
     31	| while IFS=, read HostName IP Score Ping Speed CountryLong CountryShort NumVpnSessions Uptime TotalUsers TotalTraffic LogType Operator Message OpenVPN_ConfigData_Base64; do
     32		local i=$(($i + 1))
     33		Score=$(numfmt --to=si $Score)
     34		Speed=$(numfmt --to=iec-i --suffix=B/s --format="%.1f" $Speed)
     35		UptimeInMilliseconds=$Uptime
     36		Uptime=$(echo "$(($UptimeInMilliseconds/15552000000)) years")                      # 1000*60*60*12*30*12
     37		[ "${Uptime%% *}" -eq 0 ] && Uptime="$(($UptimeInMilliseconds/1296000000)) months" # 1000*60*60*12*30
     38		[ "${Uptime%% *}" -eq 0 ] && Uptime="$(($UptimeInMilliseconds/43200000)) days"     # 1000*60*60*12
     39		[ "${Uptime%% *}" -eq 0 ] && Uptime="$(($UptimeInMilliseconds/3600000)) hours"     # 1000*60*60
     40		[ "${Uptime%% *}" -eq 0 ] && Uptime="$(($UptimeInMilliseconds/60000)) minutes"     # 1000*60
     41		[ "${Uptime%% *}" -eq 0 ] && Uptime="$(($UptimeInMilliseconds/1000)) seconds"      # 1000
     42		[ "${Uptime%% *}" -eq 0 ] && Uptime="${UptimeInMilliseconds} milliseconds"
     43		TotalUsers=$(numfmt --to=si $TotalUsers)
     44		TotalTraffic=$(numfmt --to=iec-i --suffix=B --format="%.1f" $TotalTraffic)
     45		#     1  2         3      4     5      6            7             8               9       10          11            12       13        14
     46		echo "$i,$HostName,$Score,$Ping,$Speed,$CountryLong,$CountryShort,$NumVpnSessions,$Uptime,$TotalUsers,$TotalTraffic,$LogType,$Operator,$Message"
     47	done
     48}
     49
     50get_srv() {
     51	local NUM=$(get_csv | column -s, -t -R3,4,5,8,10,11 | fzf +s | cut -d' ' -f1)
     52	[ -z "$NUM" ] && exit 0
     53	if [ "$NUM" -ne 0 ]; then
     54		local SRV=$(sed -n "$(($NUM + 2))p" $CSV)
     55		local CountryCode=$(echo $SRV | awk -F, '{print tolower($7)}')
     56		local Hostname=$(echo $SRV | awk -F, '{print $1}')
     57		local OVPN="vpngate_${CountryCode}_${Hostname}.ovpn"
     58		read -p "Press $(tput bold)Enter$(tput sgr0) to save $(tput bold; tput setaf 4)${OVPN}$(tput sgr0)... " -r ANSWER
     59		[ "$ANSWER" = "" ] && echo $SRV | awk -F, '{print $15}' | base64 -di >$OVPN
     60	fi
     61}
     62
     63while true; do get_srv; done