summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@lasker.childrenofmay.org>2023-04-24 12:39:41 -0400
committerroot <root@lasker.childrenofmay.org>2023-04-24 12:39:41 -0400
commit0822c8328c1d41382c18c03bd8d0deb899269052 (patch)
tree8051d785fa3c6278a54d3f23347e2b5c8f072593
parent2e87b684ecc657706e6ca0749643c84c6caee375 (diff)
improve factoring and add makefile
-rw-r--r--Makefile6
-rwxr-xr-xsocat.sh46
2 files changed, 42 insertions, 10 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..09918b8
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
1
2all:
3
4status install:
5 ./socat.sh $@
6
diff --git a/socat.sh b/socat.sh
index e7ae9f6..1afb4a4 100755
--- a/socat.sh
+++ b/socat.sh
@@ -1,4 +1,5 @@
1#!/bin/bash 1#!/bin/bash
2
2UNIT=socat-dns 3UNIT=socat-dns
3 4
4verify_service_availability() 5verify_service_availability()
@@ -6,15 +7,40 @@ verify_service_availability()
6 dig . @127.0.0.1 >/dev/null 2>&1 7 dig . @127.0.0.1 >/dev/null 2>&1
7} 8}
8 9
10service_launch_command()
11{
12 local dest port local_port
13 dest=${1:-149.56.44.185}
14 case "$dest" in
15 '') return 1 ;;
16 *:*)
17 port=${dest#*:}
18 dest=${dest%%:*}
19 ;;
20 *)
21 port=${2:-53}
22 ;;
23 esac
24 local_port=${3:-$port}
25
26 printf "socat UDP-RECVFROM:%s,fork UDP-SENDTO:%s\n" "$local_port" "$dest:$port"
27}
28
29####
30
9watchdog() 31watchdog()
10{ 32{
11 PID=$1 33 PID=$1
34 VERIFY=$2
35
36 TWO_MILLION=2000000
37 SLEEP_TIME=$(( ${WATCHDOG_USEC:-$TWO_MILLION} / $TWO_MILLION ))
12 38
13 enable -f /usr/lib/bash/sleep sleep || true 39 enable -f /usr/lib/bash/sleep sleep || true
14 while verify_service_availability 40 while $VERIFY
15 do 41 do
16 systemd-notify --pid="$PID" WATCHDOG=1 42 systemd-notify --pid="$PID" WATCHDOG=1
17 sleep $((${WATCHDOG_USEC:-2000000} / 2000000)) 43 sleep "$SLEEP_TIME"
18 done 44 done
19} 45}
20 46
@@ -23,10 +49,7 @@ main()
23 case "$1" in 49 case "$1" in
24 install) 50 install)
25 shift 51 shift
26 DEST=${1:-149.56.44.185} 52 COMMAND=$(service_launch_command "$@")
27 PORT=${2:-53}
28 LOCAL_PORT=${3:-$PORT}
29 COMMAND="socat UDP-RECVFROM:$LOCAL_PORT,fork UDP-SENDTO:$DEST:$PORT"
30 if systemctl is-active "$UNIT" >/dev/null 53 if systemctl is-active "$UNIT" >/dev/null
31 then 54 then
32 systemctl stop "$UNIT" 55 systemctl stop "$UNIT"
@@ -38,16 +61,19 @@ main()
38 --property Restart="always" \ 61 --property Restart="always" \
39 --property WatchdogSec="5" \ 62 --property WatchdogSec="5" \
40 -- \ 63 -- \
41 "$0" watchdog "$COMMAND" 64 "$0" watchdog "$COMMAND" verify_service_availability
42 ;; 65 ;;
43 watchdog) 66 watchdog)
44 shift 67 shift
45 COMMAND="$1" 68 watchdog $$ "$2" &
46 watchdog $$ & 69 exec $1
47 exec $COMMAND
48 ;; 70 ;;
71 status)
72 systemctl status "$UNIT"
73 ;;
49 *) 74 *)
50 echo "Usage: $0 install [destination [destination port [local port]]]" >&2 75 echo "Usage: $0 install [destination [destination port [local port]]]" >&2
76 echo " $0 status" >&2
51 exit 1 77 exit 1
52 ;; 78 ;;
53 esac 79 esac