#! Name: tempsh.sh #! Author: kittykat #! Version: 2024.10.11 #! Desc: Add support for downloading and processing of urls for a new host #! Usage: Copy this file into the ./${ScriptDir}/hosts/ folder #! #! #! ------------ REQUIRED SECTION --------------- #! @[UPDATE] HostAndDomainRegexes: This string is loaded into mad.sh and allows dynamic handling of new url data #! Format: '/HostCode/HostNick/HostFuncPrefix:HostDomainRegex@' #! HostCode: (ie. 'fh' for filehaus -- cannot be used by other hosts) #! HostNick: What is displayed throughout MAD output (ie. 'filehaus' -- "urls.txt has 10 filehaus.." will be displayed) #! HostFuncPrefix: (ie. 'fh' -- fh_DownloadFile(), fh_FetchFileInfo() .. ) #! * Note: Must begin with a letter a-z (functions beginning with numbers are no bueno) #! HostDomainRegex: The regex used to verify matching urls HostCode='tmpsh' HostNick='temp.sh' HostFuncPrefix='tmpsh' HostUrls='temp.sh' HostDomainRegex='^(http|https)://(.*\.)?temp\.sh' #! #! !! DO NOT UPDATE OR REMOVE !! #! This merges the Required HostAndDomainRegexes into mad.sh ListHostAndDomainRegexes=${ListHostAndDomainRegexes}'/'${HostCode}'/'${HostNick}'/'${HostFuncPrefix}'/'${HostUrls}':'${HostDomainRegex}'@' #! #! #! ------------ (1) Host Main Download Function --------------- # #! #! @REQUIRED: Host Main Download function #! Must be named specifically as such: #! _DownloadFile() tmpsh_DownloadFile() { local remote_url=${1} local file_url=${1} local filecnt=${2} warnAndRetryUnknownError=false exitDownloadError=false exitDownloadNotAvailable=false fileAlreadyDone=false download_inflight_path="${WorkDir}/.inflight/" mkdir -p "$download_inflight_path" completed_location="${WorkDir}/downloads/" tor_identity="${RANDOM}" finalAttempt="false" for ((z=0; z<=$MaxUrlRetries; z++)); do if [[ $z -eq $MaxUrlRetries ]] ; then finalAttempt="true" fi CLEANSTRING=${remote_url//[^a-zA-Z0-9]/} trap "rm -f ${WorkDir}/.flocks/${CLEANSTRING}; echo ""; tput cnorm; exit" 0 1 2 3 6 15 if tmpsh_FetchFileInfo $finalAttempt && tmpsh_GetFile "${filecnt}" $((z+1)) $finalAttempt ; then return 0 elif [[ $z -lt $MaxUrlRetries ]]; then if [[ "${fileAlreadyDone}" == "true" ]] ; then break fi if [[ "${warnAndRetryUnknownError}" == "true" ]] ; then if [[ "${DebugAllEnabled}" == "true" ]] ; then debugHtml "${remote_url##*/}" "error" "Retry due to an unknown issue: attempt #$((z+1)) of ${MaxUrlRetries}" fi fi if [[ "${exitDownloadError}" == "true" || "${exitDownloadNotAvailable}" == "true" ]] ; then if [[ "${DebugAllEnabled}" == "true" ]] ; then debugHtml "${remote_url##*/}" "error" "Exit due to unrecoverable issue" fi rm -f "${WorkDir}/.flocks/${remote_url//[^a-zA-Z0-9]/}" break fi echo -e "\n${YELLOW}A recoverable error occurred, retry attempt $((z+1))/${MaxUrlRetries}${NC}" sleep 3 fi done rm -f "${WorkDir}/.flocks/${remote_url//[^a-zA-Z0-9]/}" } #! #! ------------- (2) Fetch File Info Function ----------------- # #! tmpsh_FetchFileInfo() { finalAttempt=$1 maxfetchretries=4 download_url="$remote_url" echo -e "${GREEN}# Fetching file info…${NC}" for ((j=1; j<=$maxfetchretries; j++)); do printf " ." CLEANSTRING=${remote_url//[^a-zA-Z0-9]/} trap "rm -f ${WorkDir}/.flocks/${CLEANSTRING}; echo ""; tput cnorm; exit" 0 1 2 3 6 15 GetRandomUA file_header=$(tor_curl_request --insecure -s -D - -o /dev/null -X POST \ -H "Connection: keep-alive" \ -w 'EffectiveUrl=%{url_effective}' \ "$download_url") if [[ "${DebugAllEnabled}" == "true" ]] ; then debugHtml "${remote_url##*/}" "tmpsh_posthead" "download_url: ${download_url}"$'\n'"${file_header}" fi if [[ -z $file_header ]] ; then if [[ $j == $maxfetchretries ]] ; then printf "\\n" echo -e "${RED}| Failed to extract file info [1]${NC}" warnAndRetryUnknownError=true if [[ "${finalAttempt}" == "true" ]] ; then failedRetryDownload "${remote_url}" "Failed to extract file info [1]" "" fi return 1 else tor_identity="${RANDOM}" continue fi fi if grep -Eqi 'HTTP.* 404|Not found|No such file|File was deleted' <<< "$file_header"; then printf "\\n" echo -e "${RED}| The file was not found. It could be deleted or expired.${NC}" exitDownloadError=true removedDownload "${remote_url}" return 1 fi if grep -Eqi 'HTTP.* 403|Forbidden|Maximum download limit reached' <<< "$file_header"; then printf "\\n" echo -e "${RED}| The file is not available. Maximum download limit reached or it was removed.${NC}" exitDownloadError=true removedDownload "${remote_url}" return 1 fi if grep -Eqi 'HTTP.* 200' <<< "$file_header"; then printf "\\n" echo -e "${GREEN}| File info found.${NC}" if [[ "$filename_override" == "" ]] ; then filename=$(grep -oPi '(?<=filename=").*(?=")' <<< "$file_header") if [[ -z $filename ]]; then filename="${download_url##*/}" fi if [[ -z "$filename" ]]; then if [[ $j == $maxfetchretries ]] ; then printf "\\n" echo -e "${RED}| Failed to extract file name${NC}" warnAndRetryUnknownError=true if [[ "${finalAttempt}" == "true" ]] ; then failedRetryDownload "${remote_url}" "Failed to extract file name" "" fi return 1 else continue fi fi fi file_size_bytes=$(grep -oPi '(?<=content-length: ).*?(?=$)' <<< "$file_header") file_size_bytes=${file_size_bytes//[$'\t\r\n']} else err=$(grep -oPi '(?<=HTTP/.* ).*?(?=$)' <<< "$file_header") if [[ $j == $maxfetchretries ]] ; then printf "\\n" echo -e "${RED}| Failed to get file info. (Code: $err)${NC}" warnAndRetryUnknownError=true if [[ "${finalAttempt}" == "true" ]] ; then failedRetryDownload "${remote_url}" "Failed to get file info. (Code: $err)" "" fi return 1 else tor_identity="${RANDOM}" continue fi fi if [[ -z "$file_size_bytes" ]]; then if [[ $j == $maxfetchretries ]] ; then printf "\\n" echo -e "${RED}| Failed to extract file size.${NC}" warnAndRetryUnknownError=true if [[ "${finalAttempt}" == "true" ]] ; then failedRetryDownload "${remote_url}" "Failed to extract file size." "" fi return 1 else tor_identity="${RANDOM}" continue fi fi printf "\\n" break done touch "${WorkDir}/.flocks/${remote_url//[^a-zA-Z0-9]/}" if [[ ! "$filename_override" == "" ]] ; then filename="$filename_override" fi filename=$(sanitize_file_or_folder_name "${filename}") printf "\\n" echo -e "${YELLOW}| File name:${NC}\t\"${filename}\"" if [[ -z $file_size_bytes ]] ; then if [[ "${finalAttempt}" == "true" ]] ; then failedRetryDownload "${remote_url}" "Filesize not found!" "" fi echo -e "${YELLOW}| Filesize not found… retry${NC}" return 1 else file_size_readable="$(numfmt --to=iec --from=auto --format "%.2f" <<< "$file_size_bytes")" fi echo -e "${YELLOW}| File size:${NC}\t${file_size_readable}" file_path="${download_inflight_path}${filename}" flockDownload="${WorkDir}/.flocks/${filename//[^a-zA-Z0-9\.\_\-]/}.flock" if CheckFileSize "${remote_url}" "${file_size_bytes}" ; then return 1 fi if CheckDownloadExists "$remote_url" "$MoveToFolder" "$filecnt" "$filename" "$file_path" "$completed_location" ; then return 1 fi echo "${remote_url//[^a-zA-Z0-9]/}" > $flockDownload } #! #! ----------- (3) Fetch File / Download File Function --------------- # #! tmpsh_GetFile() { echo -e "${GREEN}# Downloading…${NC}" echo -e "${YELLOW}| File path:${NC}\t./.inflight/${filename}\n" fileCnt=$1 retryCnt=$2 finalAttempt=$3 flockDownload="${WorkDir}/.flocks/${filename//[^a-zA-Z0-9\.\_\-]/}.flock" for ((j=1; j<=$MaxDownloadRetries; j++)); do pd_presize=0 if [[ -f "$file_path" ]] ; then pd_presize=$(stat --format="%s" "$file_path" | tr -d '[:space:]') fi GetRandomUA tor_identity="${RANDOM}" CLEANSTRING=${remote_url//[^a-zA-Z0-9]/} trap "rm -f ${WorkDir}/.flocks/${CLEANSTRING}; rm -f $flockDownload; echo ""; tput cnorm; exit" 0 1 2 3 6 15 if [[ "${UseTorCurlImpersonate}" == "true" ]]; then if [[ "${RateMonitorEnabled}" == "true" ]]; then tor_curl_request --insecure -X POST \ --speed-limit $DownloadSpeedMin --speed-time $DownloadTimeoutInterval \ "$download_url" --continue-at - --output "$file_path" else tor_curl_request --insecure -X POST \ "$download_url" --continue-at - --output "$file_path" fi else if [[ "${RateMonitorEnabled}" == "true" ]]; then tor_curl_request --insecure -X POST \ --speed-limit $DownloadSpeedMin --speed-time $DownloadTimeoutInterval \ -H "User-Agent: $RandomUA" \ -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" \ -H "Accept-Language: en-US,en;q=0.5" \ -H "Accept-Encoding: gzip, deflate, br" \ -H "Connection: keep-alive" \ -H "Upgrade-Insecure-Requests: 1" \ -H "Sec-Fetch-Dest: document" \ -H "Sec-Fetch-Mode: navigate" \ -H "Sec-Fetch-Site: same-origin" \ -H "Sec-Fetch-User: ?1" \ "$download_url" --continue-at - --output "$file_path" else tor_curl_request --insecure -X POST \ -H "User-Agent: $RandomUA" \ -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" \ -H "Accept-Language: en-US,en;q=0.5" \ -H "Accept-Encoding: gzip, deflate, br" \ -H "Connection: keep-alive" \ -H "Upgrade-Insecure-Requests: 1" \ -H "Sec-Fetch-Dest: document" \ -H "Sec-Fetch-Mode: navigate" \ -H "Sec-Fetch-Site: same-origin" \ -H "Sec-Fetch-User: ?1" \ "$download_url" --continue-at - --output "$file_path" fi fi received_file_size=0 if [[ -f "$file_path" ]] ; then received_file_size=$(stat --format="%s" "$file_path" | tr -d '[:space:]') fi if CheckNoHtml "$remote_url" "$filename" "$file_path" "$((received_file_size - pd_presize))" ; then containsHtml=false else containsHtml=true fi downDelta=$(( received_file_size - pd_presize )) if [[ "${received_file_size}" -ne "${file_size_bytes}" ]] || [[ "$containsHtml" == "true" ]]; then if [[ "${AutoRepairBadPartials}" == "true" ]] && (( downDelta > 0 && downDelta < 1024 )) ; then if [[ -f "${file_path}" ]] ; then if ((pd_presize > 0)); then echo -e "${YELLOW}Bad node / HTML found:${NC} reverting to previous file..." truncateDownload "$remote_url" "$filename" "$pd_presize" "$received_file_size" truncate -s $pd_presize "${file_path}" else echo -e "${YELLOW}Bad node / HTML found:${NC} tainted partial removed..." rm -f "${file_path}" fi fi if ((j >= $MaxDownloadRetries)) ; then rm -f "$flockDownload"; if [[ "${finalAttempt}" == "true" ]] ; then droppedSizeBadDownload "${remote_url}" "${filename}" "${received_file_size}" fi return 1 else continue fi elif [[ "${AutoRepairBadPartials}" == "true" ]] && [[ "$containsHtml" == "true" ]] ; then if [[ -f "${file_path}" ]] ; then if ((pd_presize > 0)); then echo -e "${YELLOW}Bad node / HTML found:${NC} reverting to previous file..." truncateDownload "$remote_url" "$filename" "$pd_presize" "$received_file_size" truncate -s $pd_presize "${file_path}" else echo -e "${YELLOW}Bad node / HTML found:${NC} tainted partial removed..." rm -f "${file_path}" fi fi if ((j >= $MaxDownloadRetries)) ; then rm -f "$flockDownload"; if [[ "${finalAttempt}" == "true" ]] ; then droppedSizeBadDownload "${remote_url}" "${filename}" "${received_file_size}" fi return 1 else continue fi elif (( downDelta > 0 && downDelta < 1024 )) || [[ "$containsHtml" == "true" ]] ; then if [[ -f "$file_path" ]] ; then rm -rf "$file_path" fi echo -e "\n${YELLOW}Bad node / HTML found:${NC} tainted partial removed..." if ((j >= $MaxDownloadRetries)) ; then rm -f "$flockDownload"; if [[ "${finalAttempt}" == "true" ]] ; then droppedSizeBadDownload "${remote_url}" "${filename}" "${received_file_size}" fi return 1 else continue fi fi if [[ "${received_file_size}" -ne "${file_size_bytes}" ]]; then echo -e "\n${RED}Download failed, file is incomplete.${NC}" if ((j >= $MaxDownloadRetries)) ; then rm -f "$flockDownload"; if [[ "${finalAttempt}" == "true" ]] ; then droppedSizeBadDownload "${remote_url}" "${filename}" "${received_file_size}" fi return 1 else continue fi fi else break fi done rm -f "$flockDownload"; ProcessCompletedDownload "$remote_url" "$MoveToFolder" "$filecnt" "$filename" "$file_size_bytes" "$completed_location" "$file_path" return 0 } #! #! --------------- Host Extra Functions ------------------- # #!