wagon/back/lib/dashboard/peer/list

39 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# FILE: dashboard/peer/list
# DESCRIPTION: List a user's peers
# USAGE: list $remote_ip
source /etc/wgapi/config
ip="${1}"
printf '%s requested peer listing...\n' "${ip}" >&2
# Create token if needed
token="$(grep "${ip}" "${TOKENS_FILE}" | cut -f2)"
if [ "${token}" == "" ]; then
printf 'Creating token for %s...\n' "${ip}" >&2
token="$(</dev/urandom tr -dc '[:alnum:]' | fold -w 64 | head -n 1)"
printf '%s\t%s\n' "${ip}" "${token}" >>"${TOKENS_FILE}"
fi
# Get peer IP list
if ! wg_output="$(sudo /usr/bin/wg show "${TLD}" allowed-ips)"; then
printf 'ERROR! Wireguard failed!\n' >&2
"${LIB_DIR}/http_res" 500; exit
fi
# Filter out this user's
user_peers="$(grep "${ip%[.:]*}" <<<"${wg_output}" 2>/dev/null)"
if [ "${user_peers}" == '' ]; then
printf "ERROR! %s accessed the dashboard but isn't on the network!\n" "${ip}" >&2
printf 'User not found!\n' | "${LIB_DIR}/http_res" 403; exit
fi
# Get domains for each one
if peers="[$("${LIB_DIR}/ips_to_peers" json <<<"${user_peers}")]"; then
printf '{"token":"%s","peers":%s}' "${token}" "${peers}" | "${LIB_DIR}/http_res" 200 'application/json'
printf 'Sent peers to user %s\n' "${ip}" >&2
else
printf 'ERROR: Failed to lookup domains for user: %s\n' "${ip}" >&2
"${LIB_DIR}/http_res" 500; exit
fi