Skip to content

Instantly share code, notes, and snippets.

@ewagner12
Created September 17, 2022 19:34
Show Gist options
  • Save ewagner12/8d40412c2bca2367777ef954ae1a638c to your computer and use it in GitHub Desktop.
Save ewagner12/8d40412c2bca2367777ef954ae1a638c to your computer and use it in GitHub Desktop.
Simple setup script for Mozilla VPN on Linux using mozwire and nmcli
#!/bin/bash
# Run this script with mozwire installed in your PATH
# Change this CONF_NAME to whichever VPN server you want to connect to
CONF_NAME=us10-wireguard
CONF_DIR=~/wireguard
CONF_FILE="$CONF_DIR"/"$CONF_NAME".conf
setup() {
# Create Wireguard config files (using MozWire) in CONF_DIR
mozwire-linux relay save -o "$CONF_DIR" -n 0
# Setup Wireguard profile in nmcli
echo "Select VPN Server from one of the files in ""$CONF_DIR"
read -r CONF_NAME
if [ -e "$CONF_DIR"/"$CONF_NAME".conf ]; then
CONF_FILE="$CONF_DIR"/"$CONF_NAME".conf
else
echo "Name not recognized, setup not completed"
exit 1
fi
nmcli connection import type wireguard file $CONF_FILE
echo "Autoconnect to this VPN? (Y/n)"
read -r AUTO_RES
if [ "${AUTO_RES,,}" = "y" ] || [ "${AUTO_RES,,}" = "yes" ]; then
nmcli connection modify "$CONF_NAME" connection.autoconnect yes
else
nmcli connection modify "$CONF_NAME" connection.autoconnect no
fi
echo "Setup successful"
echo "Commands: setup, delete, connect, disconnect, status, quit"
}
delete() {
# Delete Wireguard profile
nmcli connection delete "$CONF_NAME"
echo "Connection ""$CONF_NAME"" deleted"
}
connect() {
# Connect to Wireguard profile
nmcli --show-secrets --ask connection up "$CONF_NAME"
echo "Connected to ""$CONF_NAME"
}
disconnect() {
# Disconnect from Wireguard profile
nmcli --show-secrets --ask connection down "$CONF_NAME"
echo "Disconnected from ""$CONF_NAME"
}
status() {
# Display status of Wireguard connection
if [ "$(nmcli connection show --active "$CONF_NAME" | grep -c connection)" -eq 0 ]; then
echo "Wireguard connection not active"
else
echo "Wireguard connection active. Use the following command for more info: nmcli connection show --active ""$CONF_NAME"
fi
}
menu() {
case "$1" in
setup | -s | s)
setup
;;
delete | -r | r)
delete
;;
connect | -c | c)
connect
;;
disconnect | -d | d)
disconnect
;;
status | -t | t)
status
;;
quit | -q | q)
exit 0
;;
*)
echo "Commands: setup, delete, connect, disconnect, status, quit"
echo "Example: nmwg-moz connect"
;;
esac
}
if [ -n "$1" ]; then
menu "$1"
else
menu
while true; do
read -r RES
menu "$RES"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment