From be360d410b50397578fc0474d820586cb3d66a47 Mon Sep 17 00:00:00 2001 From: randomshell Date: Sun, 23 Aug 2020 09:59:27 +0000 Subject: [PATCH] Allow headless user revokation by CLIENT_NAME --- README.md | 13 +++++++++++++ wireguard-install.sh | 33 ++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 81f59f1..6d6449d 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,19 @@ export CLIENT_DOT="3" ./wireguard-install.sh ``` +## Headless User Revokation + +It's also possible to automate the revokation of an existing user. Here, the key is to provide the (string) value of the `MENU_OPTION` variable along with the remaining mandatory variables before invoking the script. + +The following Bash script revokes an user `foo` from an existing WireGuard configuration + +```bash +#!/bin/bash +export MENU_OPTION="2" +export CLIENT_NAME="foo" +./wireguard-install.sh +``` + ## Providers I recommend these cheap cloud providers for your VPN server: diff --git a/wireguard-install.sh b/wireguard-install.sh index b0c0217..b65e2ee 100644 --- a/wireguard-install.sh +++ b/wireguard-install.sh @@ -336,18 +336,29 @@ function revokeClient() { echo "" echo "Select the existing client you want to revoke" - grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | nl -s ') ' - until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]]; do - if [[ ${CLIENT_NUMBER} == '1' ]]; then - read -rp "Select one client [1]: " CLIENT_NUMBER - else - read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER + + if [[ -z ${CLIENT_NAME} ]]; then + grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | nl -s ') ' + until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]] || [[ -n ${CLIENT_NAME} ]]; do + if [[ ${CLIENT_NUMBER} == '1' ]]; then + read -rp "Select one client [1]: " CLIENT_NUMBER + else + read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER + fi + done + + # match the selected number to a client name + CLIENT_NAME=$(grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p) + else + CLIENT_EXISTS=$(grep -c -E "^### Client ${CLIENT_NAME}\$" "/etc/wireguard/${SERVER_WG_NIC}.conf") + + if [[ ${CLIENT_EXISTS} == '1' ]]; then + echo "" + echo "The client with the specified name doesn't exists." + echo "" + exit 1 fi - done - - # match the selected number to a client name - CLIENT_NAME=$(grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p) - + fi # remove [Peer] block matching $CLIENT_NAME sed -i "/^### Client ${CLIENT_NAME}\$/,/^$/d" "/etc/wireguard/${SERVER_WG_NIC}.conf"