Skip to content

Commit 00b0d9a

Browse files
committed
Add show QR code option
- New: Add an option to regenerate QR code for an existing client. Ref: #29.
1 parent fab2a6d commit 00b0d9a

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

wireguard-install.sh

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Based on the work of Nyr and contributors at:
66
# https://github.com/Nyr/wireguard-install
77
#
8-
# Copyright (c) 2022-2023 Lin Song <[email protected]>
8+
# Copyright (c) 2022-2024 Lin Song <[email protected]>
99
# Copyright (c) 2020-2023 Nyr
1010
#
1111
# Released under the MIT License, see the accompanying file LICENSE.txt
@@ -519,7 +519,7 @@ EOF
519519
show_header2() {
520520
cat <<'EOF'
521521
522-
Copyright (c) 2022-2023 Lin Song
522+
Copyright (c) 2022-2024 Lin Song
523523
Copyright (c) 2020-2023 Nyr
524524
EOF
525525
}
@@ -798,10 +798,11 @@ else
798798
echo " 1) Add a new client"
799799
echo " 2) List existing clients"
800800
echo " 3) Remove an existing client"
801-
echo " 4) Remove WireGuard"
802-
echo " 5) Exit"
801+
echo " 4) Show QR code for a client"
802+
echo " 5) Remove WireGuard"
803+
echo " 6) Exit"
803804
read -rp "Option: " option
804-
until [[ "$option" =~ ^[1-5]$ ]]; do
805+
until [[ "$option" =~ ^[1-6]$ ]]; do
805806
echo "$option: invalid selection."
806807
read -rp "Option: " option
807808
done
@@ -845,6 +846,7 @@ else
845846
elif [ -n "$num_of_clients" ]; then
846847
printf '\n%s\n' "Total: $num_of_clients clients"
847848
fi
849+
exit
848850
;;
849851
3)
850852
num_of_clients=$(grep -c '^# BEGIN_PEER' /etc/wireguard/wg0.conf)
@@ -893,6 +895,38 @@ else
893895
exit
894896
;;
895897
4)
898+
num_of_clients=$(grep -c '^# BEGIN_PEER' /etc/wireguard/wg0.conf)
899+
if [[ "$num_of_clients" = 0 ]]; then
900+
echo
901+
echo "There are no existing clients!"
902+
exit
903+
fi
904+
echo
905+
echo "Select the client to show QR code for:"
906+
grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | nl -s ') '
907+
read -rp "Client: " client_num
908+
[ -z "$client_num" ] && abort_and_exit
909+
until [[ "$client_num" =~ ^[0-9]+$ && "$client_num" -le "$num_of_clients" ]]; do
910+
echo "$client_num: invalid selection."
911+
read -rp "Client: " client_num
912+
[ -z "$client_num" ] && abort_and_exit
913+
done
914+
client=$(grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | sed -n "$client_num"p)
915+
echo
916+
get_export_dir
917+
wg_file="$export_dir$client.conf"
918+
if [ ! -f "$wg_file" ]; then
919+
echo "Error: Cannot show QR code. Missing client config file $wg_file" >&2
920+
echo " You may instead re-run this script and add a new client." >&2
921+
exit 1
922+
fi
923+
qrencode -t UTF8 < "$wg_file"
924+
echo -e '\xE2\x86\x91 That is a QR code containing the client configuration.'
925+
echo
926+
echo "Configuration for '$client' is available in: $wg_file"
927+
exit
928+
;;
929+
5)
896930
echo
897931
read -rp "Confirm WireGuard removal? [y/N]: " remove
898932
until [[ "$remove" =~ ^[yYnN]*$ ]]; do
@@ -985,7 +1019,7 @@ else
9851019
fi
9861020
exit
9871021
;;
988-
5)
1022+
6)
9891023
exit
9901024
;;
9911025
esac

0 commit comments

Comments
 (0)