Fixed missing usernumber

master
Keith Irwin 2022-09-13 12:07:05 -06:00
parent 89bce70a60
commit f5b4db8168
Signed by: ki9
GPG Key ID: DF773B3F4A88DA86
1 changed files with 8 additions and 5 deletions

View File

@ -22,10 +22,10 @@ SERVERS_FILE='/etc/wgapi/servers'
if ! [ ${#} -eq 2 ]; then if ! [ ${#} -eq 2 ]; then
printf 'ERROR! Bad input: %s %s\n' "${0}" "${*}" >>"${LOGFILE}" printf 'ERROR! Bad input: %s %s\n' "${0}" "${*}" >>"${LOGFILE}"
exit 3 exit 3
fi; if ! [ -x '/usr/bin/wg' ]; then fi & if ! [ -x '/usr/bin/wg' ]; then
printf 'ERROR! %s could not find /usr/bin/wg\n' "${0}" >>"${LOGFILE}" printf 'ERROR! %s could not find /usr/bin/wg\n' "${0}" >>"${LOGFILE}"
exit 5 exit 5
fi; if ! [ -f "${CONFIG_FILE}" ]; then fi & if ! [ -f "${CONFIG_FILE}" ]; then
printf 'ERROR! %s could not find %s!\n' "${0}" "${CONFIG_FILE}" >>"${LOGFILE}" printf 'ERROR! %s could not find %s!\n' "${0}" "${CONFIG_FILE}" >>"${LOGFILE}"
exit 4 exit 4
fi fi
@ -33,7 +33,7 @@ source "${CONFIG_FILE}"
if ! [ -f "${SERVERS_FILE}" ]; then if ! [ -f "${SERVERS_FILE}" ]; then
printf 'ERROR! %s could not find %s!\n' "${0}" "${SERVERS_FILE}" >>"${LOGFILE}" printf 'ERROR! %s could not find %s!\n' "${0}" "${SERVERS_FILE}" >>"${LOGFILE}"
exit 12 exit 12
fi; if ! [ -f "${TOKENS_FILE}" ]; then fi & if ! [ -f "${TOKENS_FILE}" ]; then
printf 'ERROR! %s could not find %s!\n' "${0}" "${TOKENS_FILE}" >>"${LOGFILE}" printf 'ERROR! %s could not find %s!\n' "${0}" "${TOKENS_FILE}" >>"${LOGFILE}"
exit 9 exit 9
fi fi
@ -81,11 +81,14 @@ domain="${hostname:?}.${username:?}.${TLD:?}"
printf 'New domain will be %s\n' "${domain}" >>"${LOGFILE}" printf 'New domain will be %s\n' "${domain}" >>"${LOGFILE}"
# Create new IPs # Create new IPs
peers="$("${LIB_DIR}/wg_peer_list" "${1}" tsv)" || exit 10 if ! peers="$("${LIB_DIR}/wg_peer_list" "${ip}" tsv)"; then
printf 'ERROR! Failed to retrieve peers for %s!\n' "${ip}" >>"${LOGFILE}"
exit 10
fi
hostnames="$(<<<"${peers}" awk '{print $1}' | cut -d'.' -f1)" hostnames="$(<<<"${peers}" awk '{print $1}' | cut -d'.' -f1)"
ipv4s="$(<<<"${peers}" awk '{print $2}')" ipv4s="$(<<<"${peers}" awk '{print $2}')"
ipv6s="$(<<<"${peers}" awk '{print $3}')" ipv6s="$(<<<"${peers}" awk '{print $3}')"
u="$(<<<"${ipv4s}" head -n1 | cut -d'.' -f3)" usernumber="$(<<<"${ipv4s}" head -n1 | cut -d'.' -f3)"
# Increment hostnumber from 1 until an unused one is found # Increment hostnumber from 1 until an unused one is found
used_hostnumbers="$(<<<"${ipv4s}" cut -d'.' -f4 | sort | uniq)" used_hostnumbers="$(<<<"${ipv4s}" cut -d'.' -f4 | sort | uniq)"
hostnumber=1; while <<<"${used_hostnumbers}" grep -q "${hostnumber}" hostnumber=1; while <<<"${used_hostnumbers}" grep -q "${hostnumber}"