pivpn/scripts/wireguard/qrcodeCONF.sh
4s3ti 4a5804a24c Multiple fixes and Shellcheck complaiance
fixes for Issue #1306, qrcode now defaults to ansiutf8,
added flag -a256 or --ansi256 if their fonts are having trouble

Shellcheck compliance #1233 for qrcodeCONF.sh,
LIST array is now created with mapfile instead
quoted multiple variables.

fixes for Issue #1307, User creation won't allow user creation with name
starting with "-", qrcodeCONF.sh won't accept wrong options or users starting with "-"
and exit with error code 1
2021-05-08 15:51:27 +02:00

77 lines
2.2 KiB
Bash
Executable file

#!/bin/bash
helpFunc(){
echo "::: Show the qrcode of a client for use with the mobile app"
echo ":::"
echo "::: Usage: pivpn <-qr|qrcode> [-h|--help] [Options] [<client-1>] ... [<client-n>] ..."
echo ":::"
echo "::: Options:"
echo "::: -a256|ansi256 Shows QR Code in ansi256 characters"
echo "::: Commands:"
echo "::: [none] Interactive mode"
echo "::: <client> Client(s) to show"
echo "::: -h,--help Show this help dialog"
}
# Parse input arguments
encoding="ansiutf8"
while test $# -gt 0
do
_key="$1"
case "$_key" in
-h|--help)
helpFunc
exit 0
;;
-a256|--ansi256)
encoding="ansi256"
;;
*)
CLIENTS_TO_SHOW+=("$1")
;;
esac
shift
done
cd /etc/wireguard/configs || exit
if [ ! -s clients.txt ]; then
echo "::: There are no clients to show"
exit 1
fi
mapfile -t LIST < <(awk '{print $1}' clients.txt)
if [ "${#CLIENTS_TO_SHOW[@]}" -eq 0 ]; then
echo -e "::\e[4m Client list \e[0m::"
len=${#LIST[@]}
COUNTER=1
while [ $COUNTER -le "${len}" ]; do
printf "%0${#len}s) %s\r\n" "${COUNTER}" "${LIST[(($COUNTER-1))]}"
((COUNTER++))
done
read -r -p "Please enter the Index/Name of the Client to show: " CLIENTS_TO_SHOW
if [ -z "${CLIENTS_TO_SHOW}" ]; then
echo "::: You can not leave this blank!"
exit 1
fi
fi
for CLIENT_NAME in "${CLIENTS_TO_SHOW[@]}"; do
re='^[0-9]+$'
if [[ ${CLIENT_NAME:0:1} == "-" ]]; then
echo "${CLIENT_NAME} is not a valid client name or option"
exit 1
elif [[ ${CLIENT_NAME} =~ $re ]] ; then
CLIENT_NAME=${LIST[$((CLIENT_NAME -1))]}
fi
if grep -qw "${CLIENT_NAME}" clients.txt; then
echo -e "::: Showing client \e[1m${CLIENT_NAME}\e[0m below"
echo "====================================================================="
qrencode -t "${encoding}" < "${CLIENT_NAME}.conf"
echo "====================================================================="
else
echo -e "::: \e[1m${CLIENT_NAME}\e[0m does not exist"
fi
done