From 899b7723544657e8ca62c7710dd87ee4206251d0 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Fri, 2 Sep 2022 23:42:09 -0500 Subject: [PATCH] Improve user input - When adding or removing a client, abort and exit if the user enters an empty client name or client number. --- wireguard-install.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wireguard-install.sh b/wireguard-install.sh index a6832e7..41af954 100644 --- a/wireguard-install.sh +++ b/wireguard-install.sh @@ -99,6 +99,11 @@ if [[ "$is_container" -eq 0 ]]; then exit 1 fi +abort_and_exit () { + echo "Abort. No changes were made." >&2 + exit 1 +} + get_export_dir () { export_to_home_dir=0 export_dir=~/ @@ -509,11 +514,13 @@ else echo echo "Provide a name for the client:" read -p "Name: " unsanitized_client + [ -z "$unsanitized_client" ] && abort_and_exit # Allow a limited set of characters to avoid conflicts client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client") while [[ -z "$client" ]] || grep -q "^# BEGIN_PEER $client$" /etc/wireguard/wg0.conf; do echo "$client: invalid name." read -p "Name: " unsanitized_client + [ -z "$unsanitized_client" ] && abort_and_exit client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client") done echo @@ -541,9 +548,11 @@ else echo "Select the client to remove:" grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | nl -s ') ' read -p "Client: " client_number + [ -z "$client_number" ] && abort_and_exit until [[ "$client_number" =~ ^[0-9]+$ && "$client_number" -le "$number_of_clients" ]]; do echo "$client_number: invalid selection." read -p "Client: " client_number + [ -z "$client_number" ] && abort_and_exit done client=$(grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | sed -n "$client_number"p) echo