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.
This commit is contained in:
Koray Yanik 2022-03-10 23:33:05 +00:00
parent d47683dc02
commit 3f6c392969
6 changed files with 60 additions and 21 deletions

@ -1 +1 @@
Subproject commit 99fcf7aa5be5210c082dd9c483717e1fb1204f93 Subproject commit ef70aa40f9be1b8006f8ec1d9cbf17f93f722f1d

View File

@ -7,6 +7,7 @@ if [ $? -eq 0 ]; then
exit 0 exit 0
fi fi
set $REQUEST_FIELDS
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
length="$1" length="$1"
key="$2" key="$2"
@ -29,7 +30,9 @@ while [[ $# -gt 0 ]]; do
done done
if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then
body="<html>\n<head>\n<title>$WAKEBOARD_TITLE</title>\n<script type='text/javascript' src='main.js'></script>\n" body="<html>\n<head>\n<title>$WAKEBOARD_TITLE</title>\n"
body+="<script type='text/javascript' src='main.js'></script>\n"
if [ $mobile ]; then if [ $mobile ]; then
body+="<link rel='stylesheet' href='mobile.css'></link>\n</head>\n" body+="<link rel='stylesheet' href='mobile.css'></link>\n</head>\n"
else else
@ -57,7 +60,7 @@ if [ -z "$REQUEST_PATH_SANE" -o "$REQUEST_PATH_SANE" == "index.html" ]; then
body+="<td>$hostname</td>" body+="<td>$hostname</td>"
body+="<td>$ip</td>" body+="<td>$ip</td>"
body+="<td>$mac</td>" body+="<td>$mac</td>"
body+="<td><button class='wol'>Wake up!</button><td>\n" body+="<td><button class='wol' value="$mac">Wake up!</button><td>\n"
body+="</tr>" body+="</tr>"
fi fi
done done

47
handle_post.sh Executable file
View File

@ -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 <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
echo -ne "$body" > 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 <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
exit 0
fi
exit 1

View File

@ -15,7 +15,7 @@ EOF
ret=1 ret=1
for i in {0..30}; do for i in {0..30}; do
ping -c 1 -w 5 -t 1 -q "$ip" ping -c 1 -w 5 -t 1 -q "$ip" > /dev/null 2>&1
ret=$? ret=$?
[ $ret -eq 0 ] && break [ $ret -eq 0 ] && break
done done
@ -46,18 +46,4 @@ EOF
exit 0 exit 0
fi fi
if [[ "$REQUEST_PATH_SANE" =~ ^wol ]]; then
mac=$(echo "$REQUEST_PATH_SANE" | cut -d '/' -f2)
wol "$mac" > /dev/null
body="{\"response\": \"ok\"}\n"
cat <<EOF
$($BASHSERV_DIR/header.sh -t "application/json" -l $(echo -ne "$body" | wc -c))
$(echo -ne $body)
EOF
exit 0
fi
exit 1 exit 1

View File

@ -69,8 +69,11 @@ var request_wol = function (host) {
} }
}); });
request.open("GET", "wol/" + host.mac); var params = "mac=" + host.mac;
request.send();
request.open("POST", "wol", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(params);
setTimeout(function () { setTimeout(function () {
request_refresh(host); request_refresh(host);

View File

@ -38,5 +38,5 @@ fi
# Ensure variable can be read by the get handler # Ensure variable can be read by the get handler
export WAKEBOARD_TITLE export WAKEBOARD_TITLE
./bashserv/bashserv.sh -s "./static" -g "./handle_get.sh" -p "$PORT" ./bashserv/bashserv.sh -s "./static" -g "./handle_get.sh" --post "./handle_post.sh" -p "$PORT"