check-up (2704B)
1 #!/bin/bash 2 # 3 # Copyright (C) <2021> <nixx@firemail.cc> 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # 18 # DEPENDS: coreutils, bash, torsocks 19 20 # For the benefit of cron jobs, set the path 21 [ $# -ne 1 ] && echo "Path must be specified." && exit 1 22 FILEPATH="${1}" 23 24 OUTPUT=${FILEPATH}sites_up.txt 25 26 createfile () { 27 echo "${1}" >> "${2}" 28 } 29 30 doconnections () { 31 # Timeout in seconds 32 WAIT=10 33 # File length 34 LENGTH=$(wc -l "${1}" | cut -d \ -f 1) 35 # Do we use torsocks, or not? 36 [ $3 -eq 1 ] && TORSOCKS="torsocks -q" || TORSOCKS="" 37 38 # For easiness' sake, make a temp file 39 TMPOUT=$(mktemp) 40 41 COUNT=1 42 while [ $COUNT -le $LENGTH ]; do 43 DOMAIN=$(head -n $COUNT "${1}" | tail -n 1) 44 # Find the domain in the output file 45 POSITION=$(grep -n "^${DOMAIN} " "${OUTPUT}" | cut -d : -f 1) 46 if [ -z $POSITION ]; then 47 # If the domain is a new addition 48 DOWNFOR=0 49 else 50 # Get the current number of occurences it has been down 51 DOWNFOR=$(head -n $POSITION "${OUTPUT}" | tail -n 1 | cut -d \ -f 2) 52 fi 53 54 # If it doesn't connect, increment 55 if ! timeout $WAIT ${TORSOCKS} curl -s "$DOMAIN" > /dev/null; then 56 echo "${DOMAIN} $((DOWNFOR + 1))" >> "${TMPOUT}" 57 # If it does, reset count to 0 58 else 59 echo "${DOMAIN} 0" >> "${TMPOUT}" 60 fi 61 62 ((COUNT++)) 63 done 64 65 cat "${TMPOUT}" | sort -k2n -k1 > "${2}" 66 rm "${TMPOUT}" 67 } 68 69 CLEARDOMAINS=$(grep "^clearnet" "${FILEPATH}webring.csv" | cut -d , -f 3) 70 ONIONDOMAINS=$(grep "^onion" "${FILEPATH}webring.csv" | cut -d , -f 3) 71 72 CLEARTMP=$(mktemp) 73 ONIONTMP=$(mktemp) 74 echo "${CLEARDOMAINS}" > "${CLEARTMP}" 75 echo "${ONIONDOMAINS}" > "${ONIONTMP}" 76 77 # If either domain file does not exist, exit 78 [ -z "${CLEARTMP}" ] && exit 2 79 [ -z "${ONIONTMP}" ] && exit 3 80 81 # If output file does not exist, create it 82 [ -z "${OUTPUT}" ] && createfile "${CLEARTMP}" "${OUTPUT}" && createfile "${ONIONTMP}" "${OUTPUT}" && sed -i "s/$/ 0/" "${OUTPUT}" 83 84 TMPO1=$(mktemp) 85 TMPO2=$(mktemp) 86 doconnections "${CLEARTMP}" "${TMPO1}" 0 87 doconnections "${ONIONTMP}" "${TMPO2}" 1 88 89 rm "${CLEARTMP}" 90 rm "${ONIONTMP}" 91 92 cat "${TMPO1}" > "${OUTPUT}" 93 cat "${TMPO2}" >> "${OUTPUT}"