#!/bin/bash UNIT=socat-dns verify_service_availability() { dig . @127.0.0.1 >/dev/null 2>&1 } 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) shift DEST=${1:-149.56.44.185} PORT=${2:-53} LOCAL_PORT=${3:-$PORT} COMMAND="socat UDP-RECVFROM:$LOCAL_PORT,fork UDP-SENDTO:$DEST:$PORT" 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="always" \ --property WatchdogSec="5" \ -- \ "$0" watchdog "$COMMAND" ;; watchdog) shift COMMAND="$1" watchdog $$ & exec $COMMAND ;; *) echo "Usage: $0 install [destination] [destination port] [local port]" >&2 exit 1 ;; esac } main "$@"