cam config www bricking fixed (#330)

* fix bricking, improve cam.cgi interface

* Delete cam2.cgi

* Delete cam3.cgi

* fix for url encoding of POST parameters
This commit is contained in:
virmaior 2022-10-13 16:33:25 +09:00 committed by GitHub
parent da43a02fac
commit 851578ee02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 231 additions and 159 deletions

View File

@ -0,0 +1,59 @@
var feed_interval_frequency = 1000;
// https://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript#14794066
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
function enable_submit()
{
alert("no guarantees at all on this one. Change values at your own risk");
document.getElementById("update").disabled = false;
}
window.onload = function()
{
var feed = document.getElementById("current_feed");
function update_image()
{
feed.src = feed.src.split("&")[0] + "&load=" + new Date().getTime();
}
feed_interval = setInterval(update_image, feed_interval_frequency);
var sels = document.querySelectorAll('.ii_select').forEach(function(item){
var row = item.getAttribute("row");
item.addEventListener('change',function(e){
var row = this.getAttribute("row");
document.querySelector(".ii_value[row='" + row + "']").value = this.value;
});
var b = document.querySelector(".ii_value[row='" + row + "']");
item.value = b.value;
b.classList.add('ii_shared');
b.addEventListener('change',function(e){
var row = this.getAttribute("row");
document.querySelector(".ii_select[row='" + row + "']").value = this.value;
});
});
document.querySelector('[name="update_config"]').addEventListener('submit',
function(e){
var changed = 0;
const values = document.getElementsByClassName('ii_value');
Array.from(values).forEach(function(item){
if (item.getAttribute('default_value') == item.value) { item.disabled = true; } else { changed++; }
});
if (changed == 0) {
e.preventDefault();
Array.from(values).forEach(function(item){ item.disabled = false; });
}
});
}

View File

@ -0,0 +1,18 @@
bitRate=120 # (60=360p or SD, 120=HD)
res=1 # (1=SD or HD, 2=360p)
drawBoxSwitch=2 # Motion Box (1=Enabled, 2=Disabled)
MASwitch=2 # Detects Motion (1=Enabled, 2=Disabled)
AASwitch=2 # Detects Sound (1=Enabled, 2=Disabled)
SASwitch=2 # Smoke Alarm Sound (1=Enabled, 2=Disabled)
CASwitch=2 # CO Alarm Sound (1=Enabled, 2=Disabled)
recordType=1 # Record to SD (1=Continous, 2=Events Only, 3=Disabled)
nightVision=3 # Night Vision Mode (1=On, 2=Off, 3=Auto)
night_cut_thr=1 # Nightvision Conditions (1=Dusk, 2=Dark)
night_led_ex=1 # Night Vision IR Lights Near (1=Near, 2=Off or Far)
NIGHT_LED_flag=2 # Night Vision IR Lights Far (1=Far, 2=Off or Near)
indicator=2 # Camera Status Light (1=Enabled, 2=Disabled)
verSwitch=1 # Flip Vertical (1=Disabled, 2=Enabled)
horSwitch=1 # Flip Horizontal (1=Disabled, 2=Enabled)
osdSwitch=2 # Show Timestamp (1=Enabled, 2=Disabled)
watermark_flag=2 # Show WYZE Logo (1=Enabled, 2=Disabled)
AST=2 # Record Sound (1=Enabled, 2=Disabled)

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/opt/wz_mini/bin/bash
# This serves a rudimentary webpage based on wz_mini.conf
. /opt/wz_mini/www/cgi-bin/shared.cgi
@ -22,7 +22,7 @@ then
echo "$cam_config exists and not empty"
else
echo "$cam_config exists but empty"
echo "if you reboot then the camera will revert to defaults or possibly fail "
echo "if you reboot then the camera will revert to defaults "
exit
fi
else
@ -34,29 +34,10 @@ fi
}
reboot_camera() {
die_no_config
reboot_wait=90
echo "rebooting camera (refreshing screen in $reboot_wait seconds)"
echo '<script type="text/javascript">setTimeout(function(){ document.location.href = "/cgi-bin/cam.cgi"; },'$reboot_wait' * 1000)</script>'
handle_css config.css
version_info "display_BAR"
reboot
exit
}
shft() {
# SE loop did not work -- thanks ash!
suff=8
while [ "$suff" -gt 0 ] ;
do
if [[ -f "$1.$suff" ]] ; then
nxt=$((suff + 1))
mv -f "$1.$suff" "$1.$nxt"
fi
suff=$((suff-1))
done
mv -f "$1" "$1.1"
function revert_config
{
mv "$cam_config" "$cam_config.old"
mv "$cam_config.$1" "$cam_config"
}
@ -78,6 +59,15 @@ if [[ $REQUEST_METHOD = 'GET' ]]; then
if [[ "$GET_action" = "reboot" ]]; then
reboot_camera
fi
if [[ "$GET_action" = "revert" ]]; then
revert_config "$GET_version"
fi
if [[ "$GET_action" = "show_revert" ]]; then
cam_config="$cam_config.$GET_version"
fi
fi
@ -90,48 +80,26 @@ if [[ $REQUEST_METHOD = 'POST' ]]; then
done
fi
output="$cam_config.new"
cp $cam_config $output
#since ash does not handle arrays we create variables using eval
IFS='&'
for PAIR in $POST_DATA
do
K=$(echo $PAIR | cut -f1 -d=)
FK=$(echo $PAIR | cut -f1 -d= )
VA=$(echo $PAIR | cut -f2 -d= )
VB=\"${VA//%3A/:}\"
#echo "<div>$K=$VB</div>"
eval POST_$K=\"$VB\"
FK=$(urldecode "$FK")
VA=$(urldecode "$VA")
if [ "${FK:0:3}" == "row" ]; then
K=$(echo "$FK" | cut -f2 -d[ | cut -f1 -d])
# echo "<div>match: $K=$VA</div>"
sed -i s/$K.*/$K=$VA/ $output
fi
done
#switch back to going through the config file
output="$cam_config.new"
#name our output file
while IFS= read -r \ARGUMENT; do
#cycle through each line of the current config
#copy through all comments
if [ -z "$ARGUMENT" ]; then
echo -ne "\n" >> $output
elif [[ ${ARGUMENT:0:1} == "#" ]] ; then
#echo $ARGUMENT $'\n'
echo -ne $ARGUMENT"\n" >> $output
else
#for non-comments check to see if we have an entry in the POST data by deciphering the key from the ini file and using eval for our fake array
KEY=$(echo $ARGUMENT | cut -f1 -d=)
test=$(eval echo \$POST_$KEY)
#echo "key was $KEY test was ... $test <br /> "
if [[ "$test" ]]; then
#if in the fake array then we use the new value
#echo "<div style=\"color:#c00\">matched </div>"
echo -ne $KEY=\"$test\""\n" >> $output
else
#if not in the fake array we use the current value
#echo "<div>key not found</div>"
echo -ne $ARGUMENT"\n" >> $output
fi
fi
done < $cam_config
shft $cam_config
mv $output $cam_config
updated=true
@ -140,7 +108,6 @@ fi
function documentation_to_html
{
fname="$www_dir"'cam-'"$1.md"
@ -151,43 +118,36 @@ function documentation_to_html
fi
}
function select_block
{
fname="$www_dir"'cam-values.txt'
testval=$(grep "$1" "$fname" | cut -f2 -d# | cut -f2 -d"(" | cut -f1 -d")" | tr " " "Q")
if [[ -n "$testval" ]]; then
echo '<select class="ii_select" name="'SELECT_$2'" row="'$2'"><option value="">...</option>'
IFS=" "
for OPTI in ${testval//,/ }
do
pv=$(echo $OPTI | tr "Q" " " | xargs )
val=$(echo $pv | cut -f1 -d= | tr "Q" " " | xargs )
echo '<option value="'$val'">'$pv'</option>'
done
echo '</select>'
fi
}
function ini_to_html_free
{
classes=""
if [ "$1" = "USB_DIRECT_MAC_ADDR" ]; then
classes=" mac_addr"
fi
if grep -q -wi "$1" cam-numerics.txt; then
classes=" numeric"
fi
printf '<div class="ii"><div class="ii_key_DIV">%s</div><div class="ii_value_DIV"><input class="ii_value'$classes'" type="text" name="%s" value="%s" /></div>' $1 $1 $2
printf '<div class="ii"><div class="ii_key_DIV">%s</div><div class="ii_value_DIV">' $1
select_block $1 $3
printf '<input class="ii_value'$classes'" type="text" name="%s" value="%s" default_value="%s" row="%s" /></div>' "row_$3[$1]" $2 $2 $3
documentation_to_html $1
printf '</div>'
}
function ini_to_html_tf
{
printf '<div class="ii"><div class="ii_key_DIV">%s</div>' $1
printf '<div class="ii_value_DIV">'
if [[ "$2" == "true" ]]; then
printf '<input class="ii_radio" type="radio" name="%s" value="true" checked="checked" /> True &nbsp;' $1
printf '<input class="ii_radio" type="radio" name="%s" value="false" /> False &nbsp;' $1
else
printf '<input class="ii_radio" type="radio" name="%s" value="true" /> True &nbsp;' $1
printf '<input class="ii_radio" type="radio" name="%s" value="false" checked="checked" /> False &nbsp;' $1
fi
printf '</div>'
documentation_to_html $1
printf '</div>'
}
#function to handle camera feed
function html_cam_feed
{
printf '<img id="current_feed" src="/cgi-bin/jpeg.cgi?channel=1" class="feed" />'
}
@ -195,7 +155,7 @@ function html_cam_feed
echo -ne "<html><head><title>$title</title>"
handle_css config.css
echo '<script type="text/javascript" src="/config.js" ></script>'
echo '<script type="text/javascript" src="/cam.js" ></script>'
echo -ne "</head>"
@ -212,12 +172,19 @@ fi
html_cam_feed
if [ $base_cam_config != $cam_config ]; then
echo '<div><a href="?action=revert&version='$GET_version'">Revert</a> to this version</a></div>'
fi
echo -ne '<form name="update_config" method="POST" enctype="application/x-www-form-urlencoded" >'
CONFIG_BLOCK=0
row=0
while IFS= read -r ARGUMENT; do
row=$((row+1))
if [ -z "$ARGUMENT" ] ; then
echo -ne ""
elif [[ ${ARGUMENT:0:1} == "[" ]] ; then
@ -232,12 +199,7 @@ while IFS= read -r ARGUMENT; do
else
KEY=$(echo $ARGUMENT | cut -f1 -d=)
VAL=$(echo $ARGUMENT | cut -f2 -d=)
VALUE=${VAL//\"/}
case "$VALUE" in
"true") ini_to_html_tf $KEY $VALUE ;;
"false") ini_to_html_tf $KEY $VALUE ;;
*) ini_to_html_free $KEY $VALUE
esac
ini_to_html_free $KEY $VAL $row
fi
done < $cam_config
if [ "$CONFIG_BLOCK" -gt 0 ]; then
@ -250,6 +212,8 @@ echo -ne '<input type="submit" name="update" id="update" value="Update" disabled
echo -ne '</form>'
echo -ne '<button onclick="enable_submit();" >Enable Submit</button>';
revert_menu $base_hack_ini $cam_config
version_info "display_BAR"

View File

@ -34,32 +34,6 @@ fi
}
reboot_camera() {
die_no_config
reboot_wait=90
echo "rebooting camera (refreshing screen in $reboot_wait seconds)"
echo '<script type="text/javascript">setTimeout(function(){ document.location.href = "/cgi-bin/config.cgi"; },'$reboot_wait' * 1000)</script>'
handle_css config.css
version_info "display_BAR"
reboot
exit
}
shft() {
# SE loop did not work -- thanks ash!
suff=8
while [ "$suff" -gt 0 ] ;
do
if [[ -f "$1.$suff" ]] ; then
nxt=$((suff + 1))
mv -f "$1.$suff" "$1.$nxt"
fi
suff=$((suff-1))
done
mv -f "$1" "$1.1"
}
function revert_config
{
mv "$hack_ini" "$hack_ini.old"
@ -67,29 +41,6 @@ function revert_config
}
function revert_menu
{
echo '<h2 id="revert" >Revert Menu</a>'
echo '<div class="old_configs">'
echo 'Prior Versions : '
xuff=0
while [ "$xuff" -lt 9 ] ;
do
xuff=$((xuff + 1))
if [[ -f "$1.$xuff" ]] ; then
filedate=$(date -r "$1.$xuff" )
class=""
if [ "$1.$xuff" = "$2" ];
then
class="current_revert"
fi
echo '<div class="revert_DIV '$class'"><div><a href="?action=show_revert&version='"$xuff"'">'"$xuff </a></div><div> $filedate</div></div>"
fi
done
echo '</div>'
}
if [[ $REQUEST_METHOD = 'GET' ]]; then
#since ash does not handle arrays we create variables using eval
@ -128,6 +79,7 @@ if [[ $REQUEST_METHOD = 'POST' ]]; then
do
K=$(echo $PAIR | cut -f1 -d=)
VA=$(echo $PAIR | cut -f2 -d=)
VA=$(urldecode $VA)
VB=\"${VA//%3A/:}\"
#echo "<div>$K=$VB</div>"
eval POST_$K=\"$VB\"
@ -214,13 +166,6 @@ function ini_to_html_tf
printf '</div>'
}
#function to handle camera feed
function html_cam_feed
{
printf '<img id="current_feed" src="/cgi-bin/jpeg.cgi?channel=1" class="feed" />'
}
echo -ne "<html><head><title>$title</title>"

View File

@ -48,6 +48,13 @@ H1 {
.ii_value {
width:75%;
}
.ii_shared {
width:20% !important;
}
.ii_select {
width: 55%;
}
.feed {
position:absolute;
top:10px;
@ -154,3 +161,16 @@ H1 {
color:red;
font-family: monospace;
}
.github_link {
border:1px solid white;
border-radius:5px;
padding:2px;
padding-right:2px !important;
width:40px;
margin-right:15px;
}
.github_link A {
color:white;
font-size:12px;
}

View File

@ -8,12 +8,13 @@ www_dir=/opt/wz_mini/www/cgi-bin/
if [ -f /opt/wz_mini/tmp/.T31 ]; then
camtype=T31
camfirmware=$(tail -n1 /configs/app.ver | cut -f2 -d= )
cam_config="/configs/.user_config"
base_cam_config="/configs/.user_config"
elif [ -f /opt/wz_mini/tmp/.T20 ]; then
camtype=T20
camfirmware=$(tail -n1 /system/bin/app.ver | cut -f2 -d= )
cam_config="/configs//parameters"
base_cam_config="/configs/.parameters"
fi
cam_config=$base_cam_config
cammodel=$(/opt/wz_mini/etc/init.d/s04model start | grep detected | cut -f1 -d ' ' )
@ -40,8 +41,73 @@ function version_info
echo "<div class='ver_DIV' vertype='Firmware'>$camfirmware</div>"
echo "<div class='ver_DIV' vertype='wz_mini'>$hackver</div>"
echo "<div class='ver_DIV' vertype='Hostname'> $HOSTNAME</div>"
echo '<div class="github_link" ><a style="color:white" href="https://github.com/gtxaspec/wz_mini_hacks">Project</a></div>';
echo '<div class="github_link" ><a target="_new" href="https://github.com/gtxaspec/wz_mini_hacks">Project</a></div>';
echo "</div>"
}
#function to handle camera feed
function html_cam_feed
{
printf '<img id="current_feed" src="/cgi-bin/jpeg.cgi?channel=1" class="feed" />'
}
#code for rebooting the camera
reboot_camera() {
die_no_config
reboot_wait=90
echo "rebooting camera (refreshing screen in $reboot_wait seconds)"
echo '<script type="text/javascript">setTimeout(function(){
document.location.href = window.location.href.split('?')[0] + "?" + load=" + new Date().getTime();
},'$reboot_wait' * 1000)</script>'
handle_css config.css
version_info "display_BAR"
reboot
exit
}
#creates backup files
shft() {
# SE loop did not work -- thanks ash!
suff=8
while [ "$suff" -gt 0 ] ;
do
if [[ -f "$1.$suff" ]] ; then
nxt=$((suff + 1))
mv -f "$1.$suff" "$1.$nxt"
fi
suff=$((suff-1))
done
mv -f "$1" "$1.1"
}
#displays backup files using $1 to identify the file and $2 to identify if one is currently open
function revert_menu
{
echo '<h2 id="revert" >Revert Menu</a>'
echo '<div class="old_configs">'
echo 'Prior Versions : '
xuff=0
while [ "$xuff" -lt 9 ] ;
do
xuff=$((xuff + 1))
if [[ -f "$1.$xuff" ]] ; then
filedate=$(date -r "$1.$xuff" )
class=""
if [ "$1.$xuff" = "$2" ];
then
class="current_revert"
fi
echo '<div class="revert_DIV '$class'"><div><a href="?action=show_revert&version='"$xuff"'">'"$xuff </a></div><div> $filedate</div></div>"
fi
done
echo '</div>'
}
urldecode(){
a=${1//+/ }
b=${a//%/\\x}
echo -e "$b"
}