From 5e60794b428035db69899f229befac504d6dc043 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sun, 17 Nov 2024 04:01:08 +0000 Subject: refactor to use any unknown prefix --- Dockerfile | 4 ++-- Makefile | 10 ++++++---- bin/inet2.sh | 10 +++++----- bin/mkbirdconfig.sh | 2 +- bin/mkwgconfig.sh | 4 ++-- deployments/initd/inet2.initd | 8 +++++--- deployments/openrc/inet2.initd | 8 +++++--- lib/config.awk | 35 ----------------------------------- lib/inet2.sh | 22 ---------------------- share/config.awk | 35 +++++++++++++++++++++++++++++++++++ share/inet2.sh | 22 ++++++++++++++++++++++ 11 files changed, 83 insertions(+), 77 deletions(-) delete mode 100755 lib/config.awk delete mode 100755 lib/inet2.sh create mode 100755 share/config.awk create mode 100755 share/inet2.sh diff --git a/Dockerfile b/Dockerfile index b145165..0d57fb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM alpine RUN apk add --no-cache openrc udev-init-scripts-openrc wireguard-tools bind-tools bird -RUN mkdir -p /var/lib/inet2 +RUN mkdir -p /var/share/inet2 COPY ./bin /usr/local/bin -COPY ./lib /var/lib/inet2 +COPY ./share /usr/local/share/inet2 COPY ./deployments/openrc/inet2.initd /etc/init.d/inet2 COPY ./deployments/docker/wait.initd /etc/init.d/wait RUN sed -i 's/#rc_sys=""/rc_sys="docker"/' /etc/rc.conf && \ diff --git a/Makefile b/Makefile index 1582c1e..2aac3ba 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ .PHONY: install alpine openwrt +PREFIX=/usr/local + install: - mkdir -p /usr/local/bin - mkdir -p /var/lib/inet2 - cp ./bin/* /usr/local/bin - cp ./lib/* /var/lib/inet2 + mkdir -p ${PREFIX}/bin + mkdir -p ${PREFIX}/share/inet2 + cp ./bin/* ${PREFIX}/bin + cp ./share/* ${PREFIX}/share/inet2 alpine: apk add --no-cache wireguard-tools bind-tools bird diff --git a/bin/inet2.sh b/bin/inet2.sh index cb98c6c..00f1168 100755 --- a/bin/inet2.sh +++ b/bin/inet2.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env sh +#!/bin/sh -. /var/lib/inet2/inet2.sh +. ${INET2_PREFIX}/share/inet2/inet2.sh runscripts() { if [ -n "$(getval "interface $2" "$1")" ]; then @@ -26,7 +26,7 @@ start() { rm -fr /run/inet2/wg 2> /dev/null mkdir -p /run/inet2/wg - /usr/local/bin/mkbirdconfig.sh + ${INET2_PREFIX}/bin/mkbirdconfig.sh step "Setting loopback addresses" getval Loopback | while read -r addr; do @@ -35,7 +35,7 @@ start() { getval interface | while read -r inter; do step "Generating config for $inter" - run /usr/local/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" + run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" # create the wireguard interface *in the default namespace* step "Adding Wireguard interface $inter" @@ -90,7 +90,7 @@ stop() { reload() { getval interface | while read -r inter; do step "Generating config for $inter" - run /usr/local/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" /etc/inet2.conf + run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" /etc/inet2.conf step "Setting Wireguard config for $inter" run wg setconf "$inter" /run/inet2/wg/"$inter" diff --git a/bin/mkbirdconfig.sh b/bin/mkbirdconfig.sh index bf5c1f3..a7ca42e 100755 --- a/bin/mkbirdconfig.sh +++ b/bin/mkbirdconfig.sh @@ -1,6 +1,6 @@ #!/bin/sh -. /var/lib/inet2/inet2.sh +. ${INET2_PREFIX}/share/inet2/inet2.sh escapebird() { sed -e 's/\\/\\\\/g;s/"/\\"/g' diff --git a/bin/mkwgconfig.sh b/bin/mkwgconfig.sh index 6ee11a9..2da87be 100755 --- a/bin/mkwgconfig.sh +++ b/bin/mkwgconfig.sh @@ -1,11 +1,11 @@ -#!/usr/bin/env sh +#!/bin/sh # args: /path/to/interface-config /path/to/output.conf inter="$1" configfile=/etc/inet2.conf getval() { - /var/lib/inet2/config.awk "$configfile" "$@" + ${INET2_PREFIX}/share/inet2/config.awk "$configfile" "$@" } k() { diff --git a/deployments/initd/inet2.initd b/deployments/initd/inet2.initd index 41bb887..03b14b2 100755 --- a/deployments/initd/inet2.initd +++ b/deployments/initd/inet2.initd @@ -3,13 +3,15 @@ START=70 STOP=10 +export INET2_PREFIX=/usr/local + start() { - /usr/local/bin/inet2.sh start + ${INET2_PREFIX}/bin/inet2.sh start /sbin/service bird start } stop() { - /usr/local/bin/inet2.sh stop + ${INET2_PREFIX}/bin/inet2.sh stop /sbin/service bird stop } @@ -19,7 +21,7 @@ restart() { } reload() { - /usr/local/bin/inet2.sh reload + ${INET2_PREFIX}/bin/inet2.sh reload /sbin/service bird restart } diff --git a/deployments/openrc/inet2.initd b/deployments/openrc/inet2.initd index 3d12227..c9ebe39 100755 --- a/deployments/openrc/inet2.initd +++ b/deployments/openrc/inet2.initd @@ -4,13 +4,15 @@ description="Sets up wireguard interfaces connected via the host's internet conn extra_started_commands="reloadwg" +export INET2_PREFIX=/usr/local + start() { - /usr/local/bin/inet2.sh start + ${INET2_PREFIX}/bin/inet2.sh start rc-service bird start 2> /dev/null } stop() { - /usr/local/bin/inet2.sh stop + ${INET2_PREFIX}/bin/inet2.sh stop rc-service bird stop 2> /dev/null } @@ -20,7 +22,7 @@ restart() { } reload() { - /usr/local/bin/inet2.sh reload + ${INET2_PREFIX}/bin/inet2.sh reload rc-service bird restart 2> /dev/null } diff --git a/lib/config.awk b/lib/config.awk deleted file mode 100755 index 9d0a117..0000000 --- a/lib/config.awk +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/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/lib/inet2.sh b/lib/inet2.sh deleted file mode 100755 index 672286e..0000000 --- a/lib/inet2.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env sh - -run() { - printf '$ \x1b[32;1m%s\x1b[0m\n' "$*" - "$@" -} -step() { - printf '\x1b[34;1m>> %s\x1b[0m\n' "$*" -} - -getval() { - /var/lib/inet2/config.awk /etc/inet2.conf "$@" -} - -haskey() { - getval interface | while read -r inter; do - if getval "interface $inter" "$1"; then - echo "true" - return - fi - done -} diff --git a/share/config.awk b/share/config.awk new file mode 100755 index 0000000..9d0a117 --- /dev/null +++ b/share/config.awk @@ -0,0 +1,35 @@ +#!/usr/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/share/inet2.sh b/share/inet2.sh new file mode 100755 index 0000000..73bd279 --- /dev/null +++ b/share/inet2.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env sh + +run() { + printf '$ \x1b[32;1m%s\x1b[0m\n' "$*" + "$@" +} +step() { + printf '\x1b[34;1m>> %s\x1b[0m\n' "$*" +} + +getval() { + ${INET2_PREFIX}/share/inet2/config.awk /etc/inet2.conf "$@" +} + +haskey() { + getval interface | while read -r inter; do + if getval "interface $inter" "$1"; then + echo "true" + return + fi + done +} -- cgit v1.2.3-freya