wagon/back/lib/ips_to_peers

37 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# FILE: ips_to_peers
# DESCRIPTION: Takes a list of IPs from wg show allowed-ips
# and adds domains using axfr lookups
# USAGE: ips_to_peers json <<<"${user_peers}"
# ERRORS:
# 3: bad usage
# 4: nslookup failed
# 5: Invalid format
# 6: Config file not found
source /etc/wagon/config
format="${1}"
# Perform AXFR lookup
res="$(/usr/lib/wagon/ns_lookup_axfr_rdns4)" || exit 4
# This should read from stdin
# TODO: Run this loop in parallel
while IFS= read -r line; do
pubkey="$(<<<"${line}" cut -d ' ' -f1)"
ips="$(<<<"${line}" cut -d ' ' -f2 | tr ' ' '\n')"
ipv4="$(<<<"${ips}" grep '\.')"
ipv6="$(<<<"${ips}" grep ':')"
ipv4="${ipv4%%/*}"
ipv6="${ipv6%%/*}"
rev_ipv4="$(/usr/lib/wagon/ns_update_rev_ipv4 ${ipv4})"
domain="$(<<<"${res}" grep "^${rev_ipv4} name = " | cut -f2 | cut -d' ' -f3 | sed 's/\.$//')"
case "${format}" 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}";;
*) printf 'ERROR! Invalid format for %s: %s\n' "${0}" "${format}" >&2
exit 5;
esac
done | sed 's/\n//g' | sed 's/,$//' # Remove trailing comma and newlines