improved execution logic

This commit is contained in:
Pavel Muhortov 2024-01-07 15:49:39 +03:00
parent d888b08c50
commit ebac39dc33

View File

@ -163,7 +163,7 @@ startsendtlgm() {
} }
####################################### #######################################
# Create wireguard client certificates. # Create wireguard client peer.
# Globals: # Globals:
# clientname # clientname
# clientaddr # clientaddr
@ -171,10 +171,20 @@ startsendtlgm() {
# Arguments: # Arguments:
# None # None
####################################### #######################################
createcert() { createpeer() {
wg genkey | tee "/etc/wireguard/pki/${clientname}-private.key" | wg pubkey > "/etc/wireguard/pki/${clientname}-public.key" if [ -f "/etc/wireguard/pki/${clientname}-private.key" ]; then
addtologs "${clientname} private key exists, create skipped"
else
wg genkey | tee "/etc/wireguard/pki/${clientname}-private.key" > /dev/null 2>&1
addtologs "created ${clientname} new private key"
fi
wg pubkey > "/etc/wireguard/pki/${clientname}-public.key" < "/etc/wireguard/pki/${clientname}-private.key"
addtologs "created ${clientname} wireguard certificates"
clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key") clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key")
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key") if grep -q -w "${clientpublkey}" "${servercfgname}"; then
addtologs "${clientname} peer configuration exists, create skipped"
else
wg set "${iface_name}" peer "${clientpublkey}" \ wg set "${iface_name}" peer "${clientpublkey}" \
allowed-ips "${clientaddr}/32" \ allowed-ips "${clientaddr}/32" \
persistent-keepalive 5 persistent-keepalive 5
@ -184,9 +194,15 @@ createcert() {
printf "%s\n" " AllowedIPs = ${clientaddr}/32" printf "%s\n" " AllowedIPs = ${clientaddr}/32"
printf "%s\n" " PersistentKeepalive = 5" printf "%s\n" " PersistentKeepalive = 5"
} >> "${servercfgname}" } >> "${servercfgname}"
addtologs "created ${clientname} wireguard certificate" addtologs "created ${clientname} wireguard peer configuration"
fi
if ip ro | grep -q -w "${clientaddr}"; then
addtologs "${clientname} peer address exists in routes, create skipped"
else
ip -4 route add "${clientaddr}/32" dev "${iface_name}" ip -4 route add "${clientaddr}/32" dev "${iface_name}"
addtologs "created route to ${clientname} peer" addtologs "created ${clientname} peer route"
fi
} }
####################################### #######################################
@ -201,6 +217,7 @@ createcert() {
# None # None
####################################### #######################################
createconf() { createconf() {
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key")
clientconf=$(cat "${clientconfdef}") clientconf=$(cat "${clientconfdef}")
clientconf=${clientconf//clientaddr/${clientaddr}} clientconf=${clientconf//clientaddr/${clientaddr}}
clientconf=${clientconf//clientprivkey/${clientprivkey}} clientconf=${clientconf//clientprivkey/${clientprivkey}}
@ -224,14 +241,14 @@ createinfo() {
"" \ "" \
"${additional}" \ "${additional}" \
"" \ "" \
"Peer Address: \`${clientaddr}\`" \ "Peer Address: \`$(grep 'Address' "/etc/wireguard/${clientname}.conf" | awk '{print $3}')\`" \
) )
qrencode -o "/etc/wireguard/${clientname}.png" -t png -s 6 < "/etc/wireguard/${clientname}.conf" qrencode -o "/etc/wireguard/${clientname}.png" -t png -s 6 < "/etc/wireguard/${clientname}.conf"
addtologs "created ${clientname} qr code" addtologs "created ${clientname} qr code"
} }
####################################### #######################################
# Delete wireguard client certificates. # Delete wireguard client peer.
# Globals: # Globals:
# clientname # clientname
# clientpublkey # clientpublkey
@ -239,13 +256,15 @@ createinfo() {
# Arguments: # Arguments:
# None # None
####################################### #######################################
deletecert() { deletepeer() {
if [ -f "/etc/wireguard/pki/${clientname}-private.key" ]; then
wg pubkey > "/etc/wireguard/pki/${clientname}-public.key" < "/etc/wireguard/pki/${clientname}-private.key"
fi
if [ -f "/etc/wireguard/pki/${clientname}-public.key" ]; then
clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key") clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key")
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key") if grep -q -w "${clientpublkey}" "${servercfgname}"; then
wg set "${iface_name}" peer "${clientpublkey}" remove wg set "${iface_name}" peer "${clientpublkey}" remove
rm -f "/etc/wireguard/pki/${clientname}-public.key"
rm -f "/etc/wireguard/pki/${clientname}-private.key"
addtologs "deleted ${clientname} wireguard certificate"
# PublicKey = # PublicKey =
s2=$(grep -n "${clientpublkey}" "${servercfgname}" | cut -d":" -f1) s2=$(grep -n "${clientpublkey}" "${servercfgname}" | cut -d":" -f1)
# [Peer] # [Peer]
@ -255,8 +274,25 @@ deletecert() {
# PersistentKeepalive = # PersistentKeepalive =
s4=$(( s2 + 2 )) s4=$(( s2 + 2 ))
sed -i "${s1}d;${s2}d;${s3}d;${s4}d" "${servercfgname}" sed -i "${s1}d;${s2}d;${s3}d;${s4}d" "${servercfgname}"
addtologs "deleted ${clientname} wireguard peer configuration"
else
addtologs "${clientname} peer configuration does not exist, delete skipped"
fi
rm -f "/etc/wireguard/pki/${clientname}-public.key" > /dev/null 2>&1
rm -f "/etc/wireguard/pki/${clientname}-private.key" > /dev/null 2>&1
addtologs "deleted ${clientname} wireguard certificates"
else
addtologs "${clientname} certificates do not exist, delete skipped"
fi
if ip ro | grep -q -w "${clientaddr} dev ${iface_name}"; then
ip -4 route del "${clientaddr}/32" dev "${iface_name}" ip -4 route del "${clientaddr}/32" dev "${iface_name}"
addtologs "deleted route to ${clientname} peer" addtologs "deleted ${clientname} peer route"
else
addtologs "${clientname} peer route does not exist, delete skipped"
fi
} }
####################################### #######################################
@ -267,8 +303,12 @@ deletecert() {
# None # None
####################################### #######################################
deleteconf() { deleteconf() {
if [ -f "/etc/wireguard/${clientname}.conf" ]; then
rm -f "/etc/wireguard/${clientname}.conf" rm -f "/etc/wireguard/${clientname}.conf"
addtologs "deleted ${clientname} wireguard config file" addtologs "deleted ${clientname} wireguard config file"
else
addtologs "${clientname} wireguard config does not exist, delete skipped"
fi
} }
####################################### #######################################
@ -279,8 +319,12 @@ deleteconf() {
# None # None
####################################### #######################################
deleteinfo() { deleteinfo() {
if [ -f "/etc/wireguard/${clientname}.png" ]; then
rm -f "/etc/wireguard/${clientname}.png" rm -f "/etc/wireguard/${clientname}.png"
addtologs "deleted ${clientname} qr code" addtologs "deleted ${clientname} qr code"
else
addtologs "${clientname} qr code does not exist, delete skipped"
fi
} }
# #
@ -307,27 +351,33 @@ done
time=$(date +%s) time=$(date +%s)
logs=/dev/null logs=/dev/null
if ! command -v qrencode &> /dev/null || \
! command -v /usr/local/bin/sendmail.py &> /dev/null || \
! command -v python3 &> /dev/null; then
execerror "Not found dependencies"
fi
# #
# MAIN: # MAIN:
# #
if checkroot; then if checkroot; then
if ! command -v qrencode &> /dev/null || \
! command -v grepcidr &> /dev/null || \
! command -v /usr/local/bin/sendmail.py &> /dev/null || \
! command -v python3 &> /dev/null || \
! command -v curl &> /dev/null; then
execerror "Not found dependencies"
fi
getconfig getconfig
if [ "${clienttodo}" == "add" ] && \ if [ "${clienttodo}" == "add" ] && \
[ -n "${iface_name}" ] && \
[ -n "${clientname}" ] && \ [ -n "${clientname}" ] && \
grepcidr "0.0.0.0/0" <(echo "${clientaddr}") > /dev/null; then grepcidr "0.0.0.0/0" <(echo "${clientaddr}") > /dev/null; then
if [ -f "/etc/wireguard/${clientname}.conf" ] || \ if ip ro | grep -q -w "${clientaddr}" || \
grep -q -w "${clientaddr}/32" "${servercfgname}"; then grep -q -w "${clientaddr}/32" "${servercfgname}"; then
execerror "wireguard config exist or address used, exit" addtologs "${clientaddr} address used, create peer, conf skipped"
else else
createcert createpeer
createconf createconf
fi
if [ ! -f "/etc/wireguard/${clientname}.conf" ]; then
addtologs "${clientname}.conf not found, create info skipped"
else
createinfo createinfo
startsendmail startsendmail
startsendtlgm startsendtlgm
@ -337,17 +387,12 @@ if checkroot; then
systemctl restart "wg-quick@${iface_name}" systemctl restart "wg-quick@${iface_name}"
fi fi
elif [ "${clienttodo}" == "del" ] && \ elif [ "${clienttodo}" == "del" ] && \
[ -n "${iface_name}" ] && \
[ -n "${clientname}" ] && \ [ -n "${clientname}" ] && \
grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then grepcidr "0.0.0.0/0" <(echo "${clientaddr}") >/dev/null; then
if [ -f "/etc/wireguard/${clientname}.conf" ]; then
deleteconf deleteconf
fi
if [ -f "/etc/wireguard/${clientname}.png" ]; then
deleteinfo deleteinfo
fi deletepeer
if grep -q -w "${clientaddr}/32" "${servercfgname}"; then
deletecert
fi
if [ "${resetforce}" -eq 1 ];then if [ "${resetforce}" -eq 1 ];then
addtologs "restarting wg-quick@${iface_name}..." addtologs "restarting wg-quick@${iface_name}..."
systemctl "restart wg-quick@${iface_name}" systemctl "restart wg-quick@${iface_name}"