Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to the read-only version 0.1 #93

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added readonlysd/.ssh/authorized_keys
Empty file.
Empty file added readonlysd/.ssh/known_hosts
Empty file.
Empty file added readonlysd/.ssh/rsa.id
Empty file.
4 changes: 4 additions & 0 deletions readonlysd/Videos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
30 changes: 30 additions & 0 deletions readonlysd/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
###########################
# ZS-GX1 Hacks Config File
###########################

## Silence Voices
# YES = Silence Voices
# NO = Keep Voices
# Default = NO
# Possibly causes some cameras to fail - use at your own risk

VOICE=NO

## Directory where log files are written
logdir=/media

## Directory where saved video files are written
videoDir=/media/Videos
## RTSP stream password
rtspPasswd=

## A minimum free space in Mb, which should be kept free on the SD. If this limit is reached, old video files are deleted.
MinFreeSpaceMb=60

## RTSP port forwarding to a given server via SSH settings
# SSH username (authentication is via private key, private key should be placed on the SD: /.ssh/rsa.id
sshTunnelUser=zoneminder
# SSH server address. Server should be added to the known hosts file in (SD root)/.ssh/known_hosts
sshTunnelServer=
# SSH tunnel bind address, where the RTSP port could be opened for video stream
sshTunnelBindAddress=localhost:8001
45 changes: 36 additions & 9 deletions readonlysd/debug_cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ fi
# confirm hack type
touch /home/HACKSD

# possibly needed but may not be
mount --bind /media/hack/group /etc/group
(sleep 70 && mount --bind /media/hack/passwd /etc/passwd) &
mount --bind /media/hack/shadow /etc/shadow

mkdir -p /home/busybox

# install updated version of busybox
Expand All @@ -23,25 +28,25 @@ mount --bind /media/hack/busybox /bin/busybox
# set new env
mount --bind /media/hack/profile /etc/profile

# possibly needed but may not be
mount --bind /media/hack/group /etc/group
mount --bind /media/hack/passwd /etc/passwd
mount --bind /media/hack/shadow /etc/shadow

# update hosts file to prevent communication
mount --bind /media/hack/hosts.new /etc/hosts

# include config
. /media/config.txt

# busybox httpd
/home/busybox/httpd -p 8080 -h /media/hack/www
/home/busybox/httpd -p 8080 -h /media/hack/www -r "Identify yourself:" -c /media/hack/httpd.conf

# setup and install dropbear ssh server - no password login
/media/hack/dropbearmulti dropbear -r /media/hack/dropbear_ecdsa_host_key -B
# setup and install dropbear ssh server - password login. hack/shadow file should contain a password!
mkdir /etc/dropbear
ln -s /media/.ssh/authorized_keys /etc/dropbear/authorized_keys
/media/hack/dropbearmulti dropbear -r /media/hack/dropbear_ecdsa_host_key -E >$logdir/dropbear.log 2>&1

# start ftp server
(/home/busybox/tcpsvd -E 0.0.0.0 21 /home/busybox/ftpd -w / ) &

# sync the time
(sleep 20 && /home/busybox/ntpd -q -p 0.uk.pool.ntp.org ) &
(while true; do sleep 20 && /home/busybox/ntpd -q -p 0.uk.pool.ntp.org; done ) &

# silence the voices - uncomment if needed
#if [ ! -f /home/VOICE-orig.tgz ]; then
Expand All @@ -52,3 +57,25 @@ mount --bind /media/hack/hosts.new /etc/hosts

#
############
export PATH=$PATH:/home/busybox

syslogd -O /media/syslog -b 4 -s 512

if [ -n $sshTunnelUser -a -n $sshTunnelServer -a -n $sshTunnelBindAddress ]
then
ln -s /media/.ssh/ /root/.ssh
/media/hack/ssh.sh 2>&1 | while IFS= read -r line; do echo "$(date) $line"; done >>$logdir/ssh_error.log &
/media/hack/sshwatchdog.sh 2>&1 | while IFS= read -r line; do echo "$(date) $line"; done >>$logdir/ssh_watchdog.log &
fi
/media/hack/ffmpeg.sh 2>&1 | while IFS= read -r line; do echo "$(date) $line"; done >>$logdir/ffmpeg.log &
/media/hack/loadcheck.sh 2>&1 | while IFS= read -r line; do exho "$(date) $line"; done >>$logdir/load.log &
/media/hack/videomove.sh 2>&1 | while IFS= read -r line; do echo "$(date) $line"; done >>$logdir/videomove.log &
(while true; do sleep 10; killall telnetd; if [ $? -eq 0 ]; then break; fi; done;) &

#(while true; do ifconfig >>$logdir/ifconf.log 2>&1; iwconfig >>$logdir/ifconf.log 2>&1; iwlist ra0 scanning >>$logdir/ifconf.log 2>&1; done;) &
#cp -f $logdir/wpa_supplicant.conf /home/wpa_supplicant.conf
#cp -f /home/wpa_supplicant.conf $logdir/backup/
#cp -f /home/config.cfg $logdir/backup/
#cp -f /home/config.xml $logdir/backup/
#cp -f /home/devParam.dat $logdir/backup/
#cp -f $logdir/config/* /home/
Binary file modified readonlysd/hack/busybox
Binary file not shown.
Binary file added readonlysd/hack/ffmpeg
Binary file not shown.
16 changes: 16 additions & 0 deletions readonlysd/hack/ffmpeg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

############################################################
## Starts ffmpeg process to save the video stream locally ##
## Author: DBaluxa ##
############################################################

# include config
. /media/config.txt

while true
do
sleep 30
/media/hack/ffmpeg -i rtsp://admin:[email protected]:8001/0/av0 -vcodec copy -acodec copy -map 0 -f segment -segment_time 1200 -v error $videoDir/%04d.mkv </dev/null
done

1 change: 1 addition & 0 deletions readonlysd/hack/httpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/:ptz:$1$ySkNQ4l3$e0XVD1KUVue9He8gsldnD.
29 changes: 29 additions & 0 deletions readonlysd/hack/loadcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

############################################################
# Load monitoring to workaround p2pcam CPU useage problems #
############################################################

maxload=12

#Load monitoring starts only after 20 mins..
sleep 1200

#We check the load every 5 mins
while true
do
#Field 6 is the 15 min avg load
load=`uptime | cut -d "," -f 6`
echo $load

#Bash does not support float numbers, therefore we use awk
status=`echo $load HIGH LOW $maxload | awk '{if ($4 < $1) print $2; else print $3;}'`
if [ $status = HIGH ]
then
echo Load is high: $load, rebooting...
sleep 1
reboot
fi
sleep 300
done

39 changes: 39 additions & 0 deletions readonlysd/hack/ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

##################################
## Build SSH tunnel to a host ##
## Author: DBaluxa ##
##################################

# include config
. /media/config.txt

HOME=/root
tunnelPort=`echo $sshTunnelBindAddress | cut -d : -f 2`
sshKillCmd='if [ `sudo netstat -tlpn | grep :'"$tunnelPort"' | grep sshd |wc -l` -gt 0 ]; then echo Killing sshd...; nohup sh -c "(sleep 5 ; sudo killall sshd ; sudo /etc/init.d/ssh restart)" >/dev/null 2>&1 & fi; exit'

rm /tmp/ssh_chg.log
sleep 40

while true
do
date +%s >/tmp/ssh_chg.log

#Check if there is sticky non-working tunnel, and if there is one, restart SSHD to get rid of it
/media/hack/dropbearmulti ssh -i /media/.ssh/rsa.id $sshTunnelUser@$sshTunnelServer $sshKillCmd </dev/zero >/tmp/pfcheck.log
success=$?
cat /tmp/pfcheck.log
if [ $success -eq 0 ]
then
echo Successfull testconnect, starting portfoward...
if [ `grep Killing /tmp/pfcheck.log | wc -l` -gt 0 ]
then
echo Waiting for SSHD restart...
sleep 10
fi
/media/hack/dropbearmulti ssh -i /media/.ssh/rsa.id -T -R $sshTunnelBindAddress:localhost:8001 $sshTunnelUser@$sshTunnelServer "while true; do date +%s; sleep 40; done" </dev/zero >>/tmp/ssh_chg.log
fi
rm /tmp/ssh_chg.log
sleep 10
done

38 changes: 38 additions & 0 deletions readonlysd/hack/sshwatchdog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh

###########################################################################################################################
## SSH watchdog process: periodically checks the SSH output for changes, if the output is stalled, kills the SSH process ##
## Author DBaluxa ##
###########################################################################################################################

re='^[0-9]+$'

while true
do
currdate=`date +%s`
#echo $currdate

if [ -e /tmp/ssh_chg.log ]
then
lastdate=`/home/busybox/tail -n 1 /tmp/ssh_chg.log`

if [ -n $lastdate ]
then
delta=`expr $currdate - $lastdate`
#echo $delta

if [ $delta -gt 90 ]
then
echo Current time: `date`, ssh last update: `date -d @$lastdate`
echo SSH is not responding till $delta secs, killing it...
kill -9 `ps ax |grep dropbearmulti |grep ssh | /home/busybox/cut -c 1-6`
fi
else
echo Last update date is not number: $lastdate
fi
else
echo /tmp/ssh_chg.log does not exits!
fi
sleep 10
done
echo ERROR:Check process exited
68 changes: 68 additions & 0 deletions readonlysd/hack/videomove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh

###################################################################################################
## Moves ffmpeg's output the corresponding directory, and renames it to show the recording date. ##
## Deletes old videos when there is not enough free space on the SD card. ##
## Author: DBaluxa ##
###################################################################################################

# include config
. /media/config.txt
files=$videoDir/*.mkv

while true
do
sleep 2

#Moving video files to directories if the time sync is OK
year=`date +%Y`
if [ $year != 1970 ]
then
day=`date +%Y-%m-%d`
ts=`date +%Y%m%d_%H_%M_%S`
if [ ! -d $videoDir/$day ]
then
mkdir $videoDir/$day
fi
for i in $files
do
if [ "$i" != "$files" ]
then
mv $i $videoDir/$day/${ts}_`basename $i`
fi
done
fi

#Removing files when filesystem is full
freeMb=`df -Phm $videoDir | tail -1 | awk '{print $4}' | sed 's/G/ 1000/g' | sed 's/M/ 1/g' |awk '{printf "%.0f\n", $1 * $2}'`
#echo Free space: $freeMb
if [ $freeMb -le $MinFreeSpaceMb ]
then
for dir in `ls $videoDir`
do
if [ -d $videoDir/$dir ]
then
dirEmpty=true
for file in `ls $videoDir/$dir`
do
freeMb=`df -Phm $videoDir | tail -1 | awk '{print $4}' | sed 's/G/ 1000/g' | sed 's/M/ 1/g' |awk '{printf "%.0f\n", $1 * $2}'`
echo Free space: $freeMb
if [ $freeMb -le $MinFreeSpaceMb ]
then
echo Removing $videoDir/$dir/$file
rm $videoDir/$dir/$file
else
dirEmpty=false
break;
fi
done
if [ $dirEmpty = true ]
then
echo Removing empty directory $dir
rmdir $videoDir/$dir
fi
fi
done
sync
fi
done