From 3f6c392969d7e09dd8df5a5cb75010a9263dc3cd Mon Sep 17 00:00:00 2001 From: Koray Yanik Date: Thu, 10 Mar 2022 23:33:05 +0000 Subject: [PATCH] Made /wol a POST request instead of a GET Since it does modify state. * Also fixed a bug where ping did sometimes still write to the response. --- bashserv | 2 +- handle_get.sh | 7 +++++-- handle_post.sh | 47 ++++++++++++++++++++++++++++++++++++++++++++++ handle_requests.sh | 16 +--------------- static/main.js | 7 +++++-- wakeboard | 2 +- 6 files changed, 60 insertions(+), 21 deletions(-) create mode 100755 handle_post.sh diff --git a/bashserv b/bashserv index 99fcf7a..ef70aa4 160000 --- a/bashserv +++ b/bashserv @@ -1 +1 @@ -Subproject commit 99fcf7aa5be5210c082dd9c483717e1fb1204f93 +Subproject commit ef70aa40f9be1b8006f8ec1d9cbf17f93f722f1d diff --git a/handle_get.sh b/handle_get.sh index 7c48d54..5f050d7 100755 --- a/handle_get.sh +++ b/handle_get.sh @@ -7,6 +7,7 @@ if [ $? -eq 0 ]; then exit 0 fi +set $REQUEST_FIELDS while [[ $# -gt 0 ]]; do length="$1" key="$2" @@ -29,7 +30,9 @@ while [[ $# -gt 0 ]]; do done if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then - body="\n\n$WAKEBOARD_TITLE\n\n" + body="\n\n$WAKEBOARD_TITLE\n" + body+="\n" + if [ $mobile ]; then body+="\n\n" else @@ -57,7 +60,7 @@ if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then body+="$hostname" body+="$ip" body+="$mac" - body+="\n" + body+="\n" body+="" fi done diff --git a/handle_post.sh b/handle_post.sh new file mode 100755 index 0000000..e89d154 --- /dev/null +++ b/handle_post.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +if [[ "$REQUEST_PATH_SANE" =~ ^refresh ]]; then + ip=$(echo "$REQUEST_PATH_SANE" | cut -d '/' -f2) + + body="{\n\"time\": \"$(date +%T)\",\n" + body+="\"status\": \"unknown\"\n}\n" + +cat < status/$ip + + ret=1 + for i in {0..30}; do + ping -c 1 -w 5 -t 1 -q "$ip" > /dev/null 2>&1 + ret=$? + [ $ret -eq 0 ] && break + done + + echo -e "{\n\"time\": \"$(date +%T)\"," > status/$ip + + if [ $ret -eq 0 ]; then + echo -e "\"status\": \"up\"\n}\n" >> status/$ip + else + echo -e "\"status\": \"down\"\n}\n" >> status/$ip + fi + exit 0 +fi + +if [[ "$REQUEST_PATH_SANE" =~ ^wol ]]; then + mac=$(echo "$POST_DATA" | cut -d '=' -f2) + + wol "$mac" > /dev/null + body="{\"response\": \"ok\"}\n" + +cat < /dev/null 2>&1 ret=$? [ $ret -eq 0 ] && break done @@ -46,18 +46,4 @@ EOF exit 0 fi -if [[ "$REQUEST_PATH_SANE" =~ ^wol ]]; then - mac=$(echo "$REQUEST_PATH_SANE" | cut -d '/' -f2) - - wol "$mac" > /dev/null - body="{\"response\": \"ok\"}\n" - -cat <