pivpn/scripts/wireguard/enableCONF.sh

125 lines
2.7 KiB
Bash
Raw Normal View History

2020-10-21 21:35:29 +00:00
#!/bin/bash
setupVars="/etc/pivpn/wireguard/setupVars.conf"
if [ ! -f "${setupVars}" ]; then
2022-07-26 13:20:35 +00:00
echo "::: Missing setup vars file!"
exit 1
2020-10-21 21:35:29 +00:00
fi
# shellcheck disable=SC1090
2020-10-21 21:35:29 +00:00
source "${setupVars}"
helpFunc(){
2022-07-26 13:20:35 +00:00
echo "::: Enables client conf profiles"
echo ":::"
echo "::: Usage: pivpn <-on|on> [-h|--help] [-v] [<client-1> ... [<client-2>] ...] "
echo ":::"
echo "::: Commands:"
echo "::: [none] Interactive mode"
echo "::: <client> Client"
echo "::: -y,--yes Enable client(s) without confirmation"
echo "::: -v Show disabled clients only"
echo "::: -h,--help Show this help dialog"
2020-10-21 21:35:29 +00:00
}
# Parse input arguments
2022-07-26 13:20:35 +00:00
while test $# -gt 0; do
_key="$1"
case "$_key" in
-h|--help)
helpFunc
exit 0
;;
-y|--yes)
CONFIRM=true
;;
-v)
DISPLAY_DISABLED=true
;;
2020-10-21 21:35:29 +00:00
*)
2022-07-26 13:20:35 +00:00
CLIENTS_TO_CHANGE+=("$1")
;;
esac
shift
2020-10-21 21:35:29 +00:00
done
cd /etc/wireguard || exit
2020-10-21 21:35:29 +00:00
if [ ! -s configs/clients.txt ]; then
2022-07-26 13:20:35 +00:00
echo "::: There are no clients to change"
exit 1
2020-10-21 21:35:29 +00:00
fi
if [ "$DISPLAY_DISABLED" ]; then
2022-07-26 13:20:35 +00:00
grep '\[disabled\] ### begin' wg0.conf | sed 's/#//g; s/begin//'
exit 1
2020-10-21 21:35:29 +00:00
fi
mapfile -t LIST < <(awk '{print $1}' configs/clients.txt)
2020-10-21 21:35:29 +00:00
if [ "${#CLIENTS_TO_CHANGE[@]}" -eq 0 ]; then
2022-07-26 13:20:35 +00:00
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 be enabled from the list above: " CLIENTS_TO_CHANGE
if [ -z "${CLIENTS_TO_CHANGE}" ]; then
echo "::: You can not leave this blank!"
exit 1
fi
2020-10-21 21:35:29 +00:00
fi
CHANGED_COUNT=0
for CLIENT_NAME in "${CLIENTS_TO_CHANGE[@]}"; do
2022-07-26 13:20:35 +00:00
re='^[0-9]+$'
if [[ ${CLIENT_NAME} =~ $re ]] ; then
CLIENT_NAME=${LIST[$((CLIENT_NAME -1))]}
fi
2020-10-21 21:35:29 +00:00
2022-07-26 13:20:35 +00:00
if ! grep -q "^${CLIENT_NAME} " configs/clients.txt; then
echo -e "::: \e[1m${CLIENT_NAME}\e[0m does not exist"
else
if [ -n "$CONFIRM" ]; then
REPLY="y"
else
read -r -p "Confirm you want to enable $CLIENT_NAME? [Y/n] "
fi
2020-10-21 21:35:29 +00:00
2022-07-26 13:20:35 +00:00
if [[ $REPLY =~ ^[Yy]$ ]]; then
2020-10-21 21:35:29 +00:00
2022-07-26 13:20:35 +00:00
# Enable the peer section from the server config
echo "${CLIENT_NAME}"
sed -e "/begin ${CLIENT_NAME}/,/end ${CLIENT_NAME}/ s/#\[disabled\] //" -i wg0.conf
echo "::: Updated server config"
2020-10-21 21:35:29 +00:00
2022-07-26 13:20:35 +00:00
((CHANGED_COUNT++))
echo "::: Successfully enabled ${CLIENT_NAME}"
2020-10-21 21:35:29 +00:00
2022-07-26 13:20:35 +00:00
fi
fi
2020-10-21 21:35:29 +00:00
done
# Restart WireGuard only if some clients were actually deleted
if [ "${CHANGED_COUNT}" -gt 0 ]; then
2022-07-26 13:20:35 +00:00
if [ "${PLAT}" == 'Alpine' ]; then
if rc-service wg-quick restart; then
echo "::: WireGuard reloaded"
else
echo "::: Failed to reload WireGuard"
fi
else
if systemctl reload wg-quick@wg0; then
echo "::: WireGuard reloaded"
else
echo "::: Failed to reload WireGuard"
fi
fi
2020-10-21 21:35:29 +00:00
fi