Scripts to make it easier to use MullvadVPN on Gentoo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2 KiB

#! /bin/sh
#
# SPDX-License-Identifier: BlueOak-1.0.0
#
# Copyright (c) 2021 Gavin D. Howard.
#
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
printf 'usage: %s <command> [<country_code>]\n' "$0"
printf ' valid commands are: connect, disconnect, reconnect, switch\n'
exit 1
fi
cmd="$1"
shift
COUNTRY_FILE="%%COUNTRY_FILE%%"
if [ "$#" -eq 1 ]; then
country="$1"
shift
printf '%s\n' "$country" > "$COUNTRY_FILE"
elif [ ! -f "$COUNTRY_FILE" ]; then
printf 'us\n' > "$COUNTRY_FILE"
fi
connect=0
disconnect=0
case "$cmd" in
connect) connect=1 ;;
disconnect) disconnect=1 ;;
reconnect) connect=1; disconnect=1 ;;
switch) connect=1; disconnect=1 ;;
*) printf 'Invalid command: %s\n' "$cmd"; exit 2 ;;
esac
pgrep openvpn
err="$?"
if [ "$err" -eq 0 ]; then
if [ "$disconnect" -ne 0 ]; then
"%%SUPERUSER_CMD%%" pkill --signal SIGINT openvpn
sleep 1
fi
country=$(cat "$COUNTRY_FILE")
pgrep -f "mullvad_$country.conf"
err="$?"
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
sleep 1
fi
fi
if [ "$connect" -ne 0 ]; then
backgrounder "%%SUPERUSER_CMD%%" "%%INSTALL_DIR%%/vpn"
fi