Skip to content

Commit

Permalink
Internal webserver
Browse files Browse the repository at this point in the history
Internal webserver
  • Loading branch information
kroese committed May 11, 2023
2 parents 790edae + 952caa0 commit 3eb9d12
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
1 change: 0 additions & 1 deletion run/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
set -Eeuo pipefail

# Display wait message
/run/server.sh 80 install &
/run/server.sh 5000 install &

# Download the required files from the Synology website
Expand Down
3 changes: 1 addition & 2 deletions run/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ if [[ "${DHCP}" == [Yy1]* ]]; then
# Configuration for DHCP IP
configureDHCP

# Display IP on port 80 and 5000
{ pkill -f server.sh || true; } 2>/dev/null

/run/server.sh 80 /run/ip.sh &
/run/server.sh 5000 /run/ip.sh &

else
Expand Down
45 changes: 24 additions & 21 deletions run/server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,36 @@ if [[ "$2" != "/"* ]]; then
HTML=$(html "$BODY")
printf '%b' "HTTP/1.1 200 OK\nContent-Length: ${#HTML}\nConnection: close\n\n$HTML" > "$TMP_FILE"

socat TCP4-LISTEN:80,reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null &
socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null & wait $!

exit

fi

else

if [[ "$2" != "/run/ip.sh" ]]; then

cp "$2" "$TMP_FILE"

else
if [[ "$2" != "/run/ip.sh" ]]; then

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>"
cp "$2" "$TMP_FILE"

HTML=$(html "xxx")
else

{ 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"
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>"

fi
HTML=$(html "xxx")

chmod +x "$TMP_FILE"
socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"$TMP_FILE" 2> /dev/null & wait $!
{ 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"

fi

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 $!

0 comments on commit 3eb9d12

Please sign in to comment.