2022-09-10 19:01:36 -06:00
|
|
|
#!/bin/bash
|
2022-09-14 22:28:02 -06:00
|
|
|
# FILE: admin/peer/list
|
|
|
|
# DESCRIPTION: List peers for a user, or all peers
|
2022-11-08 21:05:34 -07:00
|
|
|
# USAGE: list $remote_ip $querystring
|
|
|
|
# QUERYSTRING: ?un=$username
|
2022-09-10 19:01:36 -06:00
|
|
|
|
2023-04-02 13:46:52 -06:00
|
|
|
source /etc/wagon/config
|
2022-12-04 08:53:22 -07:00
|
|
|
ip="${1}"; qs="$(<<<"${2}" tr '&' '\n' | sed 's/?//')"
|
2022-09-15 16:40:20 -06:00
|
|
|
un="$(<<<"${qs}" grep -oP 'un=(.*)' | sed 's/^un=//' | xargs)"
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'Admin %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-14 22:28:02 -06:00
|
|
|
token="$(</dev/urandom tr -dc '[:alnum:]' | fold -w64 | head -n1)"
|
2023-04-02 13:46:52 -06:00
|
|
|
printf '%s\t%s\n' "${ip}" "${token}" >>/var/local/wagon/tokens
|
2022-09-15 21:35:56 -06:00
|
|
|
fi
|
2022-09-14 22:28:02 -06:00
|
|
|
|
|
|
|
# Get peer IP list
|
2022-09-15 08:47:17 -06:00
|
|
|
if ! peers="$(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
|
|
|
|
|
2022-09-15 08:47:17 -06:00
|
|
|
# Filter out single user (if provided)
|
|
|
|
if [ "${un}" != '' ]; then
|
2022-09-15 16:42:52 -06:00
|
|
|
peers="$(grep "${IPV4_NET%.*.*}\.${un}\." <<<"${peers}" 2>/dev/null)"
|
2022-09-15 08:47:17 -06:00
|
|
|
if [ "${peers}" == '' ]; then
|
2022-12-03 17:07:03 -07:00
|
|
|
printf 'User number %s not found!\n' "${un}" >&2
|
2023-04-02 13:46:52 -06:00
|
|
|
printf 'User not found!\n' | /usr/lib/wagon/http_res 404; exit
|
2022-09-15 08:47:17 -06:00
|
|
|
fi
|
2022-09-10 19:01:36 -06:00
|
|
|
fi
|
|
|
|
|
2022-09-14 22:28:02 -06:00
|
|
|
# Get domains for each peer
|
2023-04-02 13:46:52 -06:00
|
|
|
if peers="[$(/usr/lib/wagon/ips_to_peers json <<<"${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 admin %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 user domain: %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
|