wagon/back/lib/wg_user_list

32 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# FILE: wgapi:back/lib/wg/user/list
# DESCRIPTION: List all devices from all users
# USAGE: list json|tsv
# ERRORS:
# 3: bad usage
# 4: nslookup failed
# 5: Config not found
# 6: wg failed
CONFIG_FILE='/etc/wgapi/config'
[ ${#} -eq 1 ] || exit 3
[ -f "${CONFIG_FILE}" ] || exit 5
source "${CONFIG_FILE}"
#/usr/bin/wg show ${TLD} allowed-ips
/usr/bin/wg show "${TLD}" allowed-ips \
| while IFS=$' ' read -r pubkey ipv4 ipv6; do
ipv4="${ipv4%%/*}"
ipv6="${ipv6%%/*}"
domain="$("${LIB_DIR}/ns_lookup_rdns" "${ipv4}")" || exit 4
username="$(<<<"${domain}" cut -d'.' -f2)"
hostname="$(<<<"${domain}" cut -d'.' -f1)"
case "${2}" in
#TODO Remove json keys with empty ("") values
'json') printf '{"domain":"%s.%s.%s","ipv4":"%s","ipv6":"%s","pubkey":"%s}"},' \
"${hostname}" "${username}" "${TLD}" "${ipv4}" "${ipv6}" "${pubkey}";
*) printf '%s.%s.%s\t%s\t%s\t%s\n' \
"${hostname}" "${username}" "${TLD}" "${ipv4}" "${ipv6}" "${pubkey}";;
fi
done | sed 's/,$//' # Remove trailing comma from json