39 lines
1.3 KiB
Bash
Executable File
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/wagon/config
|
|
ip="${1}"
|
|
printf '%s requested peer listing...\n' "${ip}" >&2
|
|
|
|
# Create token if needed
|
|
token="$(grep "${ip}" /var/local/wagon/tokens | 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}" >>/var/local/wagon/tokens
|
|
fi
|
|
|
|
# Get peer IP list
|
|
if ! wg_output="$(sudo /usr/bin/wg show "${TLD}" allowed-ips)"; then
|
|
printf 'ERROR! Wireguard failed!\n' >&2
|
|
/usr/lib/wagon/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' | /usr/lib/wagon/http_res 403; exit
|
|
fi
|
|
|
|
# Get domains for each one
|
|
if peers="[$(/usr/lib/wagon/ips_to_peers json <<<"${user_peers}")]"; then
|
|
printf '{"token":"%s","peers":%s}' "${token}" "${peers}" | /usr/lib/wagon/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
|
|
/usr/lib/wagon/http_res 500; exit
|
|
fi
|