freyanet/bin/mkwgconfig.sh
2023-12-30 15:23:30 -05:00

48 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env sh
# args: /path/to/interface-config /path/to/output.conf
inter="$1"
configfile=/etc/inet2.conf
getval() {
/var/lib/inet2/config.awk "$configfile" "$@"
}
k() {
while read -r line; do
echo "$1 = $line"
done
}
(
echo "[Interface]"
getval "interface $inter" ListenPort | k ListenPort
getval "interface $inter" PrivateKey | k PrivateKey
getval PrivateKey | k PrivateKey
echo
getval "interface $inter" peer | while read -r peer; do
echo "[Peer]"
getval "interface $inter" "peer $peer" PublicKey | k PublicKey
getval "interface $inter" "peer $peer" AllowedIPs | k AllowedIPs
domain="$(getval "interface $inter" "peer $peer" Domain)"
if [ -n "$domain" ]; then
# it doesn't like domain names in the Endpoint field, so resolve dns here
v4="$(dig +short "$domain")"
[ ! "$?" = "0" ] && v4=""
v6="$(dig +short -t aaaa "$domain")"
[ ! "$?" = "0" ] && v6=""
if getval "interface $inter" "peer $peer" IPv4; then
v6=""
fi
addr="[$v6]"
[ "$addr" = "[]" ] && addr="$v4"
echo "Endpoint = $addr:$(getval "interface $inter" "peer $peer" Port)"
else
getval "interface $inter" "peer $peer" Endpoint | k Endpoint
fi
getval "interface $inter" "peer $peer" PersistentKeepalive | k PersistentKeepalive
echo
done
) > "$2"