wagon/back/lib/ips_to_peers

36 lines
1.2 KiB
Plaintext
Raw Normal View History

2022-10-03 11:00:01 -06:00
#!/bin/bash
# FILE: ips_to_peers
2022-10-03 11:00:01 -06:00
# DESCRIPTION: Takes a list of IPs from wg show allowed-ips
2023-03-31 14:21:18 -06:00
# and adds domains using axfr lookups
# USAGE: ips_to_peers json <<<"${user_peers}"
2022-10-03 11:00:01 -06:00
# ERRORS:
# 3: bad usage
# 4: nslookup failed
# 5: Invalid format
# 6: Config file not found
source /etc/wagon/config
2022-10-03 11:00:01 -06:00
format="${1}"
2022-10-03 11:14:37 -06:00
# Perform AXFR lookup
res="$(/usr/lib/wagon/ns_lookup_axfr)" || exit 4
2022-10-03 11:14:37 -06:00
2022-10-03 11:00:01 -06:00
# This should read from stdin
2022-10-03 12:14:25 -06:00
# TODO: Run this loop in parallel
2022-10-03 11:05:23 -06:00
while IFS= read -r line; do
2022-10-03 11:20:04 -06:00
pubkey="$(<<<"${line}" cut -d ' ' -f1)"
ips="$(<<<"${line}" cut -d ' ' -f2 | tr ' ' '\n')"
ipv4="$(<<<"${ips}" grep '\.')"
ipv6="$(<<<"${ips}" grep ':')"
ipv4="${ipv4%%/*}"
ipv6="${ipv6%%/*}"
2022-10-03 12:40:03 -06:00
domain=$(<<<"${res}" grep -B1 " ${ipv4}$" | sed '/--/d' | awk '{print $2}' | paste -d " " - - | awk '{print $1}')
2022-10-03 12:41:31 -06:00
case "${format}" in
'json') printf '{"domain":"%s","ipv4":"%s","ipv6":"%s","pubkey":"%s"},' \
2022-10-03 12:44:41 -06:00
"${domain}" "${ipv4}" "${ipv6}" "${pubkey}";;
'tsv') printf '%s\t%s\t%s\t%s\n' "${domain}" "${ipv4}" "${ipv6}" "${pubkey}";;
*) printf 'ERROR! Invalid format for %s: %s\n' "${0}" "${format}" >&2
2022-10-03 12:41:31 -06:00
exit 5;
esac
2022-10-03 12:51:40 -06:00
done | sed 's/\n//g' | sed 's/,$//' # Remove trailing comma and newlines