Skip to content

Instantly share code, notes, and snippets.

@InternetUnexplorer
Last active August 16, 2021 04:06
Show Gist options
  • Save InternetUnexplorer/0aff28a24597ab9aaee74995628055b9 to your computer and use it in GitHub Desktop.
Save InternetUnexplorer/0aff28a24597ab9aaee74995628055b9 to your computer and use it in GitHub Desktop.
Script to quickly connect to/disconnect from WireGuard servers using wg-quick.
#! /usr/bin/env bash
set -euo pipefail
CONFIG_DIR="$XDG_DATA_HOME/WireGuard"
STATE_FILE="/tmp/wg-menu"
color() { echo -n "$(tput bold)$(tput setaf ${1})${2}$(tput sgr 0)" ; }
step() { echo "$(color 2 '[*]') $(color 7 "${@}")" ; }
do_down() {
if [ ! -f "$STATE_FILE" ]; then
echo "The connection is not up!"
exit 2
fi
local CONFIG_FILE=$(sudo cat "$STATE_FILE")
sudo -v
step "Disconnecting from '$(basename $CONFIG_FILE)'..."
sudo wg-quick down "$CONFIG_FILE"
sudo rm "$STATE_FILE"
}
do_up() {
local CONFIG_FILE=$(find "$CONFIG_DIR" -type f | sk)
sudo -v
if [ -f "$STATE_FILE" ]; then do_down; fi
step "Connecting to '$(basename $CONFIG_FILE)'..."
sudo wg-quick up "$CONFIG_FILE"
echo "$CONFIG_FILE" | sudo tee "$STATE_FILE" > /dev/null
}
case "${1-}" in
up) do_up ;;
down) do_down ;;
*) echo "Usage: $0 up|down"; exit 1; ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment