virtual-dsm/run/server.sh

69 lines
2.2 KiB
Bash
Raw Permalink Normal View History

2023-03-27 19:23:50 +02:00
#!/usr/bin/env bash
set -eu
2023-05-03 05:56:11 +02:00
2023-05-09 01:16:49 +02:00
TMP_FILE=$(mktemp -q /tmp/server.XXXXXX)
2023-05-09 00:41:05 +02:00
2023-05-08 22:17:21 +02:00
stop() {
trap - SIGINT EXIT
2023-05-09 00:41:05 +02:00
{ pkill -f socat || true; } 2>/dev/null
[ -f "$TMP_FILE" ] && rm -f "$TMP_FILE"
2023-05-08 22:17:21 +02:00
}
trap 'stop' EXIT SIGINT SIGTERM SIGHUP
2023-03-27 19:23:50 +02:00
2023-05-09 12:48:24 +02:00
html()
{
2023-05-11 19:34:48 +02:00
local h="<!DOCTYPE html><HTML><HEAD><TITLE>VirtualDSM</TITLE>"
2023-05-09 17:46:36 +02:00
h="${h} <STYLE>body { color: white; background-color: #125bdb; font-family: Verdana,"
h="${h} Arial,sans-serif; } a, a:hover, a:active, a:visited { color: white; }</STYLE></HEAD>"
2023-05-09 12:48:24 +02:00
h="${h}<BODY><BR><BR><H1><CENTER>$1</CENTER></H1></BODY></HTML>"
echo "$h"
}
2023-05-09 02:01:38 +02:00
if [[ "$2" != "/"* ]]; then
2023-05-08 21:14:02 +02:00
2023-05-09 02:01:38 +02:00
BODY="$2"
if [[ "$BODY" == "install" ]]; then
2023-05-09 17:46:36 +02:00
BODY="Please wait while Virtual DSM is being installed..."
BODY="$BODY<script>setTimeout(() => { document.location.reload(); }, 9999);</script>"
2023-05-09 02:01:38 +02:00
fi
2023-05-09 12:48:24 +02:00
HTML=$(html "$BODY")
printf '%b' "HTTP/1.1 200 OK\nContent-Length: ${#HTML}\nConnection: close\n\n$HTML" > "$TMP_FILE"
2023-05-09 02:01:38 +02:00
2023-05-11 20:22:27 +02:00
socat TCP4-LISTEN:80,reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null &
2023-05-09 02:01:38 +02:00
socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null & wait $!
2023-05-11 20:22:27 +02:00
exit
fi
2023-05-09 02:01:38 +02:00
2023-05-11 20:22:27 +02:00
if [[ "$2" != "/run/ip.sh" ]]; then
2023-05-09 01:16:49 +02:00
2023-05-11 20:22:27 +02:00
cp "$2" "$TMP_FILE"
2023-05-09 12:48:24 +02:00
2023-05-11 20:22:27 +02:00
else
2023-05-09 12:48:24 +02:00
2023-05-11 20:22:27 +02:00
BODY="The location of DSM is <a href='http://\${IP}:\${PORT}'>http://\${IP}:\${PORT}</a><script>"
BODY="${BODY}setTimeout(function(){ window.location.assign('http://\${IP}:\${PORT}'); }, 3000);</script>"
WAIT="Please wait while discovering IP...<script>setTimeout(() => { document.location.reload(); }, 4999);</script>"
2023-05-09 01:16:49 +02:00
2023-05-11 20:22:27 +02:00
HTML=$(html "xxx")
2023-05-09 01:16:49 +02:00
2023-05-11 20:22:27 +02:00
{ echo "#!/bin/bash"
echo "INFO=\$(curl -s -m 2 -S http://127.0.0.1:2210/read?command=10 2>/dev/null)"
echo "rest=\${INFO#*http_port}; rest=\${rest#*:}; rest=\${rest%%,*}; PORT=\${rest%%\\\"*}"
echo "rest=\${INFO#*eth0}; rest=\${rest#*ip}; rest=\${rest#*:}; rest=\${rest#*\\\"}; IP=\${rest%%\\\"*}"
echo "HTML=\"$HTML\"; [ -z \"\${IP}\" ] && BODY=\"$WAIT\" || BODY=\"$BODY\"; HTML=\${HTML/xxx/\$BODY}"
echo "printf '%b' \"HTTP/1.1 200 OK\\nContent-Length: \${#HTML}\\nConnection: close\\n\\n\$HTML\""
} > "$TMP_FILE"
2023-05-08 21:14:02 +02:00
2023-05-08 19:32:46 +02:00
fi
2023-05-11 20:22:27 +02:00
chmod +x "$TMP_FILE"
socat TCP4-LISTEN:80,reuseaddr,fork,crlf SYSTEM:"$TMP_FILE" 2> /dev/null &
socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"$TMP_FILE" 2> /dev/null & wait $!