diff options
author | Andrew Cady <d@jerkface.net> | 2023-04-22 16:49:21 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-04-22 16:49:21 -0400 |
commit | dc464efdd6c8f3da9892559880e133d630387c31 (patch) | |
tree | 57670b2cc1a0edae4a8f8828b43ada55e21f4544 |
initial
-rwxr-xr-x | socat.sh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/socat.sh b/socat.sh new file mode 100755 index 0000000..9d41445 --- /dev/null +++ b/socat.sh | |||
@@ -0,0 +1,57 @@ | |||
1 | #!/bin/bash | ||
2 | UNIT=socat-dns | ||
3 | |||
4 | DEST=${1:-149.56.44.185} | ||
5 | PORT=${2:-53} | ||
6 | LOCAL_PORT=${3:-$PORT} | ||
7 | |||
8 | COMMAND="socat -v UDP-RECVFROM:$LOCAL_PORT,fork UDP-SENDTO:$DEST:$PORT" | ||
9 | |||
10 | verify_service_availability() | ||
11 | { | ||
12 | true | ||
13 | return | ||
14 | dig . @localhost | ||
15 | } | ||
16 | |||
17 | watchdog() | ||
18 | { | ||
19 | PID=$1 | ||
20 | |||
21 | enable -f /usr/lib/bash/sleep sleep || true | ||
22 | while verify_service_availability | ||
23 | do | ||
24 | systemd-notify --pid="$PID" WATCHDOG=1 | ||
25 | sleep $((${WATCHDOG_USEC:-2000000} / 2000000)) | ||
26 | done | ||
27 | } | ||
28 | |||
29 | main() | ||
30 | { | ||
31 | case "$1" in | ||
32 | install) | ||
33 | if systemctl is-active "$UNIT" >/dev/null | ||
34 | then | ||
35 | systemctl stop "$UNIT" | ||
36 | fi | ||
37 | systemctl reset-failed "$UNIT" 2>/dev/null | ||
38 | |||
39 | systemd-run \ | ||
40 | --unit "$UNIT" \ | ||
41 | --property Restart="on-failure" \ | ||
42 | --property WatchdogSec="5" \ | ||
43 | -- \ | ||
44 | "$0" watchdog | ||
45 | ;; | ||
46 | watchdog) | ||
47 | watchdog $$ & | ||
48 | exec $COMMAND | ||
49 | ;; | ||
50 | *) | ||
51 | echo "Usage: $0 <watchdog|install>" >&2 | ||
52 | exit 1 | ||
53 | ;; | ||
54 | esac | ||
55 | } | ||
56 | |||
57 | main "$@" | ||