dotfiles

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

vpngate-prompt.sh (2785B)


      1#!/bin/sh
      2
      3ServerList="https://www.vpngate.net/api/iphone/"
      4CSV="/tmp/vpngate.csv"
      5
      6: "${progname:="${0##*/}"}"
      7
      8
      9
     10help() {
     11	cat <<_EOF | GREP_COLORS='ms=01;32' egrep --color "^f |^l |^g |^h |^c |^q|$"
     12f | fetch    fetch $FileName
     13l | list     list servers
     14g | get      get OpenVPN ovpn file(s)
     15h | help     show this help
     16c | clear    clear the terminal screen
     17q | quit     quit
     18
     19_EOF
     20	if [ -f $CSV ]; then
     21		srv_info
     22		echo
     23	else
     24		echo "$(tput bold)$(tput setaf 1)Can't find $FileName"
     25		echo "Please fetch it at first$(tput sgr0)"
     26	fi
     27}
     28
     29fetch_srv() {
     30	local HTTP_CODE=$(curl -s --write-out %{http_code} -o "$CSV" "$ServerList")
     31	case "$HTTP_CODE" in
     32		200) echo "$(tput bold)$(tput setaf 2)successfully fetched $FileName$(tput sgr0)";;
     33		000) echo "failed to fetch $FileName";;
     34	*) echo "HTTP code: ($HTTP_CODE)";;
     35	esac
     36	echo
     37}
     38
     39srv_info() {
     40	local total=$(sed '1,2d;$d' $CSV | wc -l)
     41	echo "total servers: $(tput setaf 4)${total}$(tput sgr0)"
     42}
     43
     44list_srv() {
     45	srv_info
     46	SRV_ALL=$(sed '1,2d;$d' $CSV)
     47	local countries=$(echo "$SRV_ALL" | awk -F',' -vOFS=',' '{print $7}' | sort | uniq | tr '[:upper:]' '[:lower:]' | paste -s -d' ')
     48	local country_list=$(echo $countries | tr ' ' '|')
     49	echo "available countries: $(tput bold)$(tput setaf 2)$countries$(tput sgr0)"
     50	echo "$(tput setaf 3)Input one of the country name above"
     51	echo "or leave it blank to list all servers$(tput sgr0)"
     52	read -p "country: " country
     53	local SRV_LIST=$(echo "$SRV_ALL" | grep -i ",$country," | awk -F',' -vOFS=',' '{print $3, $4, $5, $1, $6}' | awk -F',' -vOFS=',' '{tmp_score="echo "$1" | numfmt --to=si"; tmp_speed="echo "$3" | numfmt --to=iec-i --suffix=B/s"; tmp_score | getline score; tmp_speed | getline speed; $1=score; $3=speed; print}' | sort -h)
     54	if [ ! -z "${SRV_LIST}" ]; then
     55		local SRV_LIST_APPEND=$(sed -n 2p $CSV | sed 's/^#HostName/HostName/' | awk -F',' -vOFS=',' '{print $3, $4, $5, $1, $6}')
     56		echo "${SRV_LIST}
     57$(tput bold)$(tput setaf 4)${SRV_LIST_APPEND}$(tput sgr0)" | column -s',' -t
     58	fi
     59}
     60
     61get_ovpn() {
     62	echo "$(tput setaf 3) Input the hostname you get from the output of g | get"
     63	echo " You can input multiple hostnames by using space as separator$(tput sgr0)"
     64	read -p "hostname(s): " hostnames
     65	for srv in $hostnames; do
     66		if grep "^${srv}," $CSV >/dev/null; then
     67			echo "$SRV_ALL" | grep "^${srv}," | awk -F',' -vOFS=',' '{print $15}' | base64 -di >${srv}.ovpn
     68			echo "get: ${srv}.ovpn"
     69		else
     70			echo "$(tput bold)$(tput setaf 1)$srv: no such hostname$(tput sgr0)"
     71		fi
     72	done
     73}
     74
     75main_prompt() {
     76	while true; do
     77		read -p "$(basename $progname .sh)> $(tput bold)" cmd
     78		tput sgr0
     79		case "$cmd" in
     80			f|fetch) fetch_srv;;
     81			l|list) list_srv;;
     82			g|get) get_ovpn;;
     83			h|help) help;;
     84			c|clear) clear >$(tty);;
     85			q|quit) exit 0;;
     86			*) help;;
     87		esac
     88	done
     89}
     90
     91main_prompt