45 lines
1.6 KiB
Bash
Executable File
45 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
# FILE: wg_peer_list
|
|
# DESCRIPTION: Get peers in the same subnet as an ip
|
|
# USAGE: list ip json|tsv
|
|
# ERRORS:
|
|
# 3: bad usage
|
|
# 4: nslookup failed
|
|
# 5: wg failed
|
|
# 6: Config file not found
|
|
# 7: wg not found
|
|
# 8: wg found no peers
|
|
|
|
CONFIG_FILE='/etc/wgapi/config'
|
|
[ -f "${CONFIG_FILE}" ] || (
|
|
printf 'ERROR! %s Config file not found' "${0}" >>"${LOGFILE}"
|
|
exit 6; )
|
|
[ ${#} -eq 2 ] || (
|
|
printf 'ERROR! Bad usage: %s %s' "$0" "$*" >>"${LOGFILE}"
|
|
exit 3; )
|
|
[ -x /usr/bin/wg ] || (
|
|
printf 'ERROR! %s /usr/bin/wg not found' "${0}" >>"${LOGFILE}"
|
|
exit 5; )
|
|
|
|
source "${CONFIG_FILE}"
|
|
wg_output="$(/usr/bin/wg show "${TLD}" allowed-ips)" || (
|
|
printf 'ERROR! Wireguard failed!\n' >>"${LOGFILE}"
|
|
exit 5; )
|
|
user_peers="$(grep "${1%[.:]*}" <<<"${wg_output}" 2>/dev/null)"
|
|
[ "${user_peers}" == "" ] && (
|
|
printf "ERROR! %s accessed the dashboard but isn't on the network!\n" "${1}" >>"${LOGFILE}"
|
|
exit 8; )
|
|
while IFS= read -r line; do # TODO: Do these dns lookups in parallel
|
|
pubkey="$(<<<"${line}" cut -d ' ' -f1)"
|
|
ips="$(<<<"${line}" cut -d ' ' -f2 | tr ' ' '\n')"
|
|
ipv4="$(<<<"${ips}" grep '\.')"
|
|
ipv6="$(<<<"${ips}" grep ':')"
|
|
ipv4="${ipv4%%/*}"
|
|
ipv6="${ipv6%%/*}"
|
|
domain="$("${LIB_DIR}/ns_lookup_rdns" "${ipv4}" | xargs)" || exit 4
|
|
case "${2}" in
|
|
'json') printf '{"domain":"%s","ipv4":"%s","ipv6":"%s","pubkey":"%s"},' \
|
|
"${domain}" "${ipv4}" "${ipv6}" "${pubkey}";;
|
|
'tsv') printf '%s\t%s\t%s\t%s\n' "${domain}" "${ipv4}" "${ipv6}" "${pubkey}";;
|
|
esac
|
|
done <<<"${user_peers}" | sed 's/\n//g' | sed 's/,$//' # Remove trailing comma and newline |