summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/config.awk35
-rwxr-xr-xbin/mkwgconfig.sh51
2 files changed, 86 insertions, 0 deletions
diff --git a/bin/config.awk b/bin/config.awk
new file mode 100755
index 0000000..f4833ae
--- /dev/null
+++ b/bin/config.awk
@@ -0,0 +1,35 @@
+#!/run/current-system/profile/bin/awk -f
+
+BEGIN {
+ FS = "[ ]"; # use a single space as field separator and don't trim input
+ ind = 0; # indentation level
+ last = ARGC - 3; # last argument index
+ exitcode = 1; # whether anything has been matched
+ if(last < 0) { # there should be at least one argument after the filename
+ exit 1;
+ }
+ ARGC = 2; # don't read ARGV[2] and onward as files
+}
+
+END {
+ exit exitcode;
+}
+
+$0 != "" { # exit when the indentation block is exited
+ for(i = 0; i < ind; i++) {
+ if(! sub(/^\t/, "")) {
+ exit exitcode;
+ }
+ }
+}
+
+# if on the last argument, interpret it as a key and print the value
+ind == last && $1 == ARGV[ind + 2] {
+ exitcode = 0;
+ print substr($0, length($1) + 2);
+}
+# if not on the last argument, find the string exactly and increment indentation
+ind != last && $0 == ARGV[ind + 2] {
+ ind++;
+}
+
diff --git a/bin/mkwgconfig.sh b/bin/mkwgconfig.sh
new file mode 100755
index 0000000..3afa221
--- /dev/null
+++ b/bin/mkwgconfig.sh
@@ -0,0 +1,51 @@
+#!/usr/bin/env 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"