freyanet/bin/mkwgconfig.sh

49 lines
1.3 KiB
Bash
Raw Normal View History

2024-11-17 04:01:08 +00:00
#!/bin/sh
2023-11-11 00:44:48 +00:00
# args: /path/to/interface-config /path/to/output.conf
inter="$1"
2023-12-30 20:23:30 +00:00
configfile=/etc/inet2.conf
2023-11-11 00:44:48 +00:00
getval() {
2024-11-17 04:01:08 +00:00
${INET2_PREFIX}/share/inet2/config.awk "$configfile" "$@"
2023-11-11 00:44:48 +00:00
}
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"