2022-09-10 19:01:36 -06:00
|
|
|
#!/bin/bash
|
2022-11-08 21:05:34 -07:00
|
|
|
# FILE: dashboard/peer/list
|
2022-09-14 22:28:02 -06:00
|
|
|
# DESCRIPTION: List a user's peers
|
2022-11-08 21:05:34 -07:00
|
|
|
# USAGE: list $remote_ip
|
2022-09-10 19:01:36 -06:00
|
|
|
|
2023-04-02 13:46:52 -06:00
|
|
|
source /etc/wagon/config
|
2022-09-10 19:01:36 -06:00
|
|
|
ip="${1}"
|
2022-12-03 17:07:03 -07:00
|
|
|
printf '%s requested peer listing...\n' "${ip}" >&2
|
2022-09-10 19:01:36 -06:00
|
|
|
|
|
|
|
# Create token if needed
|
2023-04-02 13:46:52 -06:00
|
|
|
token="$(grep "${ip}" /var/local/wagon/tokens | cut -f2)"
|
2022-09-10 19:01:36 -06:00
|
|
|
if [ "${token}" == "" ]; then
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'Creating token for %s...\n' "${ip}" >&2
|
2022-09-10 19:01:36 -06:00
|
|
|
token="$(</dev/urandom tr -dc '[:alnum:]' | fold -w 64 | head -n 1)"
|
2023-04-02 13:46:52 -06:00
|
|
|
printf '%s\t%s\n' "${ip}" "${token}" >>/var/local/wagon/tokens
|
2022-09-10 19:01:36 -06:00
|
|
|
fi
|
|
|
|
|
2022-09-14 22:28:02 -06:00
|
|
|
# Get peer IP list
|
|
|
|
if ! wg_output="$(sudo /usr/bin/wg show "${TLD}" allowed-ips)"; then
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'ERROR! Wireguard failed!\n' >&2
|
2023-04-02 13:46:52 -06:00
|
|
|
/usr/lib/wagon/http_res 500; exit
|
2022-09-14 22:28:02 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Filter out this user's
|
|
|
|
user_peers="$(grep "${ip%[.:]*}" <<<"${wg_output}" 2>/dev/null)"
|
2022-09-15 08:47:17 -06:00
|
|
|
if [ "${user_peers}" == '' ]; then
|
2022-12-03 17:07:03 -07:00
|
|
|
printf "ERROR! %s accessed the dashboard but isn't on the network!\n" "${ip}" >&2
|
2023-04-02 13:46:52 -06:00
|
|
|
printf 'User not found!\n' | /usr/lib/wagon/http_res 403; exit
|
2022-09-14 22:28:02 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get domains for each one
|
2023-04-02 13:46:52 -06:00
|
|
|
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'
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'Sent peers to user %s\n' "${ip}" >&2
|
2022-09-10 19:01:36 -06:00
|
|
|
else
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'ERROR: Failed to lookup domains for user: %s\n' "${ip}" >&2
|
2023-04-02 13:46:52 -06:00
|
|
|
/usr/lib/wagon/http_res 500; exit
|
2022-09-10 19:01:36 -06:00
|
|
|
fi
|