add PersistentKeepalive option to wg0.conf

This commit is contained in:
Pavel Muhortov 2023-07-31 05:58:46 +03:00
parent 142a389d26
commit c2626bc09b

View File

@ -113,11 +113,14 @@ createcert() {
wg genkey | tee "/etc/wireguard/pki/${clientname}-private.key" | wg pubkey > "/etc/wireguard/pki/${clientname}-public.key"
clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key")
clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key")
wg set wg0 peer "${clientpublkey}" allowed-ips "${clientaddr}/32"
wg set wg0 peer "${clientpublkey}" \
allowed-ips "${clientaddr}/32" \
persistent-keepalive 5
{
echo -e "[Peer]"
echo -e " PublicKey = ${clientpublkey}"
echo -e " AllowedIPs = ${clientaddr}/32"
echo -e " PersistentKeepalive = 5"
} >> ${servercfgname}
ip -4 route add "${clientaddr}/32" dev wg0
}
@ -173,10 +176,15 @@ deletecert() {
wg set wg0 peer "${clientpublkey}" remove
rm -f "/etc/wireguard/pki/${clientname}-public.key"
rm -f "/etc/wireguard/pki/${clientname}-private.key"
# PublicKey =
s2=$(grep -n "${clientpublkey}" ${servercfgname} | cut -d":" -f1)
# [Peer]
s1=$(( s2 - 1 ))
# AllowedIPs =
s3=$(( s2 + 1 ))
sed -i "${s1}d;${s2}d;${s3}d" ${servercfgname}
# PersistentKeepalive =
s4=$(( s2 + 2 ))
sed -i "${s1}d;${s2}d;${s3}d;${s4}d" ${servercfgname}
ip -4 route del "${clientaddr}/32" dev wg0
}