Browse Source

Change repo to work with mullvad vpn

Signed-off-by: Gavin Howard <gavin@yzena.com>
master
Gavin Howard 1 month ago
parent
commit
c9645c27f3
Signed by: gavin GPG Key ID: C08038BDF280D33E
  1. 33
      install.sh
  2. 25
      vpn.sh
  3. 8
      vpn_completions.sh
  4. 37
      vpnctl.sh

33
install.sh

@ -45,18 +45,11 @@ if [ "$install_dir" = "" ]; then
install_dir="$HOME/.local/bin"
fi
printf 'Credentials file [%s/.config/vpn/creds.txt]: ' "$HOME"
read creds_file
printf 'Country file [%s/.mullvad/country.txt]: ' "$HOME"
read country_file
if [ "$creds_file" = "" ]; then
creds_file="$HOME/.config/vpn/creds.txt"
fi
printf 'Connection file [%s/.config/vpn/conn.txt]: ' "$HOME"
read conn_file
if [ "$conn_file" = "" ]; then
conn_file="$HOME/.config/vpn/conn.txt"
if [ "$country_file" = "" ]; then
country_file="$HOME/.mullvad/country.txt"
fi
printf 'Superuser command [sudo]: '
@ -66,31 +59,21 @@ if [ "$super_cmd" = "" ]; then
super_cmd="sudo"
fi
printf 'Directory with ExpressVPN config files for OpenVPN [%s/.config/expressvpn]: ' "$HOME"
printf 'Directory with Mullvad config files for Wireguard [/etc/wireguard]: '
read config_dir
if [ "$config_dir" = "" ]; then
config_dir="$HOME/.config/expressvpn"
config_dir="/etc/wireguard"
fi
printf 'Username: '
read username
printf 'Password: '
read pass
mkdir -p $(dirname "$creds_file")
printf '%s\n%s\n' "$username" "$pass" | "$super_cmd" tee "$creds_file"
contents=$(cat "$scriptdir/vpn.sh")
contents=$(replace "$contents" "CONN_FILE" "$conn_file")
contents=$(replace "$contents" "COUNTRY_FILE" "$country_file")
contents=$(replace "$contents" "CONFIG_DIR" "$config_dir")
contents=$(replace "$contents" "CREDS_FILE" "$creds_file")
printf '%s\n' "$contents" | tee "$install_dir/vpn"
chmod +x "$install_dir/vpn"
contents=$(cat "$scriptdir/vpnctl.sh")
contents=$(replace "$contents" "CONN_FILE" "$conn_file")
contents=$(replace "$contents" "COUNTRY_FILE" "$country_file")
contents=$(replace "$contents" "SUPERUSER_CMD" "$super_cmd")
contents=$(replace "$contents" "INSTALL_DIR" "$install_dir")
printf '%s\n' "$contents" | tee "$install_dir/vpnctl"

25
vpn.sh

@ -5,6 +5,27 @@
# Copyright (c) 2021 Gavin D. Howard.
#
conn=$(cat "%%CONN_FILE%%")
set -e
exec openvpn --config "%%CONFIG_DIR%%/$conn.ovpn" --auth-user-pass "%%CREDS_FILE%%" > /dev/null
country=$(cat "%%COUNTRY_FILE%%")
opts=$(find "%%CONFIG_DIR%%" -name "mullvad-$country*" | sort)
num=$(find "%%CONFIG_DIR%%" -name "mullvad-$country*" | wc -l)
idx=$(printf 'irand(%s) + 1\n' "$num" | bc)
i=0
for f in $opts; do
if [ "$i" = "$idx" ]; then
conn_file="$f"
break
fi
i=$(printf '%s + 1\n' "$i" | bc)
done
exec wg-quick up "$conn_file" > /dev/null

8
vpn_completions.sh

@ -10,12 +10,6 @@ _vpn_completions()
if [ "${COMP_CWORD}" -eq 1 ]; then
COMPREPLY=($(compgen -W "connect disconnect switch reconnect" "${COMP_WORDS[1]}"))
elif [ "${COMP_CWORD}" -eq 2 ]; then
local files=$(ls $HOME/.config/expressvpn/${COMP_WORDS[2]}*.ovpn 2> /dev/null)
for f in $files; do
COMPREPLY+=($(basename "$f" .ovpn))
done
COMPREPLY=($(compgen -W "at au be bg br ca ch cz de dk ee es fi fr gb hk hu ie it jp lu lv md mk nl no nz pl pt ro rs se sg us" "${COMP_WORDS[1]}"))
fi
}

37
vpnctl.sh

@ -6,7 +6,7 @@
#
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
printf 'usage: %s <command> [<connection_name>]\n' "$0"
printf 'usage: %s <command> [<country_code>]\n' "$0"
printf ' valid commands are: connect, disconnect, reconnect, switch\n'
exit 1
fi
@ -14,12 +14,14 @@ fi
cmd="$1"
shift
CONN_FILE="%%CONN_FILE%%"
COUNTRY_FILE="%%COUNTRY_FILE%%"
if [ "$#" -eq 1 ]; then
conn="$1"
country="$1"
shift
printf '%s\n' "$conn" > "$CONN_FILE"
printf '%s\n' "$country" > "$COUNTRY_FILE"
elif [ ! -f "$COUNTRY_FILE" ]; then
printf 'us\n' > "$COUNTRY_FILE"
fi
connect=0
@ -33,29 +35,22 @@ case "$cmd" in
*) printf 'Invalid command: %s\n' "$cmd"; exit 2 ;;
esac
pgrep openvpn
err="$?"
conn=$(doas wg show interfaces)
if [ "$disconnect" -ne 0 ]; then
"%%SUPERUSER_CMD%%" pkill --signal SIGINT openvpn
sleep 1
elif [ "$err" -eq 0 ]; then
conn=$(cat "$CONN_FILE")
if [ "$conn" != "" ]; then
running=1
else
running=0
fi
pgrep -f "$conn.ovpn"
err="$?"
if [ "$running" -ne 0 ]; then
if [ "$err" -eq 0 ]; then
# VPN already started.
exit 0
elif [ "$connect" -ne 0 ]; then
# Need to disconnect and reconnect.
"%%SUPERUSER_CMD%%" pkill --signal SIGINT openvpn
if [ "$connect" -ne 0 ] || [ "$disconnect" -ne 0 ]; then
"%%SUPERUSER_CMD%%" wg-quick down "$conn"
sleep 1
fi
fi
if [ "$connect" -ne 0 ]; then
backgrounder "%%SUPERUSER_CMD%%" "%%INSTALL_DIR%%/vpn"
backgrounder "%%SUPERUSER_CMD%%" "%%INSTALL_DIR%%/vpn" > /dev/null 2> /dev/null
fi

Loading…
Cancel
Save