bashbotBlockRecover: notify bot about block

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-09 08:47:13 +02:00
parent 78c066e1e5
commit eb4924122e
1 changed files with 25 additions and 19 deletions

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.96-dev3-26-gde74571 #### $$VERSION$$ v0.96-dev3-27-g78c066e
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -140,6 +140,7 @@ LOGDIR="${RUNDIR:-.}/logs"
if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then
LOGDIR="${RUNDIR:-.}" LOGDIR="${RUNDIR:-.}"
fi fi
ERRORLOG="${LOGDIR}/ERROR.log"
# we assume everthing is already set up correctly if we have TOKEN # we assume everthing is already set up correctly if we have TOKEN
if [ -z "${BOTTOKEN}" ]; then if [ -z "${BOTTOKEN}" ]; then
@ -360,19 +361,17 @@ else
sendJsonResult "${res}" "sendJson (wget)" "$@" sendJsonResult "${res}" "sendJson (wget)" "$@"
} }
sendUpload() { sendUpload() {
printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${LOGDIR}/ERROR.log" printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload" >>"${ERRORLOG}"
BOTSENT[OK]="false" BOTSENT[OK]="false"
[ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" &
} }
fi fi
# retry sendJson # retry sendJson
# $1 function to retry # $1 function $2 sleep $3 ... $n arguments
# $2 seconds to sleep before
# $2 ... $n arguments
sendJsonRetry(){ sendJsonRetry(){
local retry="${1}"; shift local retry="${1}"; shift
sleep "${1}"; shift [[ "${1}" =~ ^[0-9.]+$ ]] && sleep "${1}"; shift
case "${retry}" in case "${retry}" in
'sendJson'*) 'sendJson'*)
sendJson "$@" sendJson "$@"
@ -381,15 +380,14 @@ sendJsonRetry(){
sendUpload "$@" sendUpload "$@"
;; ;;
*) *)
echo "SendJsonRetry: unknown, cannot rerty ${retry}" >>"${LOGDIR}/ERROR.log" printf '%s: SendJsonRetry: unknown, cannot retry %s' "$(date)" "${retry}" >>"${ERRORLOG}"
;; ;;
esac esac
} }
# process sendJson result # process sendJson result
# $1 result # stdout is written to ERROR.log
# $2 function # $1 result $2 function $3 .. $n original arguments, $3 is Chat_id
# $3 .. $n original arguments, $3 is Chat_id
sendJsonResult(){ sendJsonResult(){
BOTSENT=( ) BOTSENT=( )
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")"
@ -409,38 +407,46 @@ sendJsonResult(){
BOTSENT[DESCRIPTION]="Timeout or broken/no connection" BOTSENT[DESCRIPTION]="Timeout or broken/no connection"
fi fi
# log error # log error
printf "%s: RESULT=%s CHAT[ID]=%s ACTION=%s ERROR=%s DESC=%s\n" "$(date)"\ printf "%s: RESULT=%s ACTION=%s CHAT[ID]=%s ERROR=%s DESC=%s\n" "$(date)"\
"${BOTSENT[OK]}" "${3}" "${2}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" >>"${LOGDIR}/ERROR.log" "${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}"
# warm path, do not retry on error # warm path, do not retry on error
[ -n "${BOTSEND_RETRY}" ] && return [ -n "${BOTSEND_RETRY}" ] && return
# OK, we can retry sendJson, let's see wehat's failed # OK, we can retry sendJson, let's see what's failed
# throttled, telegram say we send to much messages # throttled, telegram say we send to much messages
if [ -n "${BOTSENT[RETRY]}" ]; then if [ -n "${BOTSENT[RETRY]}" ]; then
BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))" BOTSEND_RETRY="(( ${BOTSENT[RETRY]} * 15/10 ))"
echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..." >>"${LOGDIR}/ERROR.log" echo "Retry ${2} in ${BOTSEND_RETRY} seconds ..."
sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}"
unset BOTSEND_RETRY unset BOTSEND_RETRY
return
fi fi
# timeout, failed connection or blocked # timeout, failed connection or blocked
if [ "${BOTSENT[ERROR]}" == "999" ];then if [ "${BOTSENT[ERROR]}" == "999" ];then
# check if default curl and args are OK # check if default curl and args are OK
if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then if ! curl -sL -k -m 2 "${URL}" >/dev/null 2>&1 ; then
echo "BASHBOT is blocked!" >>"${LOGDIR}/ERROR.log" echo "BASHBOT IP Adress is blocked!"
# user provided function to recover or notify block
if _exec_if_function bashbotBlockRecover; then
BOTSEND_RETRY="2"
echo "Function bashbotBlockRecover returned true, retry ${2}."
sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}"
unset BOTSEND_RETRY
fi
return return
# can we recover from block? currently not.
fi fi
# if we are not blocked, default curl and args are working # if we are not blocked, so default curl and args is working
if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then if [ -n "${BASHBOT_CURL_ARGS}" ] || [ -n "${BASHBOT_CURL}" ]; then
BOTSEND_RETRY="2" BOTSEND_RETRY="2"
echo "Timeout or broken connection, retry ${2} with default curl parameters ..." >>"${LOGDIR}/ERROR.log" printf 'Possible Problem with "%s %s", retry %s with default curl config ...'\
"${BASHBOT_CURL}" "${BASHBOT_CURL_ARGS}" "${2}"
unset BASHBOT_CURL BASHBOT_CURL_ARGS unset BASHBOT_CURL BASHBOT_CURL_ARGS
sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}" sendJsonRetry "${2}" "${BOTSEND_RETRY}" "${@:2}"
unset BOTSEND_RETRY unset BOTSEND_RETRY
fi fi
fi fi
fi fi
} } >>"${ERRORLOG}"
# escape / remove text charaters for json strings, eg. " -> \" # escape / remove text charaters for json strings, eg. " -> \"