From dc464efdd6c8f3da9892559880e133d630387c31 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sat, 22 Apr 2023 16:49:21 -0400 Subject: initial --- socat.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 socat.sh diff --git a/socat.sh b/socat.sh new file mode 100755 index 0000000..9d41445 --- /dev/null +++ b/socat.sh @@ -0,0 +1,57 @@ +#!/bin/bash +UNIT=socat-dns + +DEST=${1:-149.56.44.185} +PORT=${2:-53} +LOCAL_PORT=${3:-$PORT} + +COMMAND="socat -v UDP-RECVFROM:$LOCAL_PORT,fork UDP-SENDTO:$DEST:$PORT" + +verify_service_availability() +{ + true + return + dig . @localhost +} + +watchdog() +{ + PID=$1 + + enable -f /usr/lib/bash/sleep sleep || true + while verify_service_availability + do + systemd-notify --pid="$PID" WATCHDOG=1 + sleep $((${WATCHDOG_USEC:-2000000} / 2000000)) + done +} + +main() +{ + case "$1" in + install) + if systemctl is-active "$UNIT" >/dev/null + then + systemctl stop "$UNIT" + fi + systemctl reset-failed "$UNIT" 2>/dev/null + + systemd-run \ + --unit "$UNIT" \ + --property Restart="on-failure" \ + --property WatchdogSec="5" \ + -- \ + "$0" watchdog + ;; + watchdog) + watchdog $$ & + exec $COMMAND + ;; + *) + echo "Usage: $0 " >&2 + exit 1 + ;; + esac +} + +main "$@" -- cgit v1.2.3