freyanet/bin/mkwgconfig.sh
2023-11-14 23:25:25 -05:00

51 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
# args: /path/to/interface-config /path/to/output.conf
inter="$1"
configfile="$3"
if [ -z "$configfile" ]; then
configfile=/run/inet2/inet2.conf
fi
getval() {
/usr/local/bin/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"