Guest User

Untitled

a guest
Jan 29th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. VERSION="1.9.46"
  4.  
  5. HERE=$(dirname "$0")
  6. SCRIPT=$(readlink -f "$0")
  7. BASH_MIN_VER="3"
  8.  
  9. MEGA_API_URL="https://g.api.mega.co.nz"
  10. OPENSSL_AES_CTR_128_DEC="openssl enc -d -aes-128-ctr"
  11. OPENSSL_AES_CBC_128_DEC="openssl enc -a -A -d -aes-128-cbc"
  12. OPENSSL_AES_CBC_256_DEC="openssl enc -a -A -d -aes-256-cbc"
  13. OPENSSL_MD5="openssl md5"
  14.  
  15. if [ ! -d ".megadown" ]; then
  16. mkdir ".megadown"
  17. fi
  18.  
  19. # 1:message_error
  20. function showError {
  21. echo -e "\n$1\n" 1>&2
  22. exit 1
  23. }
  24.  
  25. function showHelp {
  26. echo -e "\nmegadown $VERSION - https://github.com/tonikelope/megadown"
  27. echo -e "\ncli downloader for mega.nz and megacrypter"
  28. echo -e "\nSingle url mode: megadown [OPTION]... 'URL'\n"
  29. echo -e "\tOptions:"
  30. echo -e "\t-o,\t--output FILE_NAME Store file with this name."
  31. echo -e "\t-s,\t--speed SPEED Download speed limit (integer values: 500B, K, 2M)."
  32. echo -e "\t-p,\t--password PASSWORD Password for MegaCrypter links."
  33. echo -e "\t-q,\t--quiet Quiet mode."
  34. echo -e "\t-m,\t--metadata Prints file metadata in JSON format and exits."
  35. echo -e "\n\nMulti url mode: megadown [OPTION]... -l|--list FILE\n"
  36. echo -e "\tOptions:"
  37. echo -e "\t-s,\t--speed SPEED Download speed limit (integer values: 500B, 500K, 2M)."
  38. echo -e "\t-p,\t--password PASSWORD Password for MegaCrypter links (same for every link in a list)."
  39. echo -e "\t-q,\t--quiet Quiet mode."
  40. echo -e "\t-m,\t--metadata Prints file metadata in JSON format and exits."
  41. echo -e "\tFile line format: URL [optional_file_name]\n"
  42. }
  43.  
  44. function check_deps {
  45.  
  46. local dep_error=0
  47.  
  48. if [ -n "$(command -v curl 2>&1)" ]; then
  49. DL_COM="curl --fail -s"
  50. DL_COM_PDATA="--data"
  51. elif [ -n "$(command -v wget 2>&1)" ]; then
  52. DL_COM="wget -q -O -"
  53. DL_COM_PDATA="--post-data"
  54. else
  55. echo "wget OR curl is required and it's not installed"
  56. dep_error=1
  57. fi
  58.  
  59. for i in openssl pv jq; do
  60.  
  61. if [ -z "$(command -v "$i" 2>&1)" ]; then
  62.  
  63. echo "[$i] is required and it's not installed"
  64. dep_error=1
  65.  
  66. else
  67.  
  68. case "$i" in
  69.  
  70. openssl)
  71.  
  72. openssl_sup=$(openssl enc -ciphers 2>&1)
  73.  
  74. for i in "aes-128-ctr" "aes-128-cbc" "aes-256-cbc"; do
  75.  
  76. if [ -z "$(echo -n "$openssl_sup" | grep -o "$i" | head -n1)" ]; then
  77.  
  78. echo "Your openssl binary does not support ${i}"
  79. dep_error=1
  80.  
  81. fi
  82.  
  83. done
  84. ;;
  85. esac
  86. fi
  87.  
  88. done
  89.  
  90. if [ -z "$(command -v python 2>&1)" ]; then
  91. echo "WARNING: python is required for MegaCrypter password protected links and it's not installed."
  92. fi
  93.  
  94. if [[ "$(echo -n "$BASH_VERSION" | grep -o -E "[0-9]+" | head -n1)" < "$BASH_MIN_VER" ]]; then
  95. echo "bash >= ${BASH_MIN_VER} is required"
  96. dep_error=1
  97. fi
  98.  
  99. if [ $dep_error -ne 0 ]; then
  100. showError "ERROR: there are dependencies not present!"
  101. fi
  102. }
  103.  
  104.  
  105. # 2:url
  106. function urldecode {
  107.  
  108. : "${*//+/ }"; echo -e "${_//%/\\x}";
  109. }
  110.  
  111. # 1:b64_encoded_string
  112. function urlb64_to_b64 {
  113. local b64=$(echo -n "$1" | tr '\-_' '+/' | tr -d ',')
  114. local pad=$(((4-${#1}%4)%4))
  115.  
  116. for i in $(seq 1 $pad); do
  117. b64="${b64}="
  118. done
  119.  
  120. echo -n "$b64"
  121. }
  122.  
  123. # 1:mega://enc link
  124. function decrypt_md_link {
  125.  
  126. local data=$(regex_imatch "^.*?mega:\/\/enc[0-9]*?\?([a-z0-9_,-]+).*?$" "$1" 1)
  127.  
  128. local iv="79F10A01844A0B27FF5B2D4E0ED3163E"
  129.  
  130. if [ $(echo -n "$1" | grep 'mega://enc?') ]; then
  131.  
  132. key="6B316F36416C2D316B7A3F217A30357958585858585858585858585858585858"
  133.  
  134. elif [ $(echo -n "$1" | grep 'mega://enc2?') ];then
  135.  
  136. key="ED1F4C200B35139806B260563B3D3876F011B4750F3A1A4A5EFD0BBE67554B44"
  137. fi
  138.  
  139. echo -n "https://mega.nz/#"$(echo -n "$(urlb64_to_b64 "$data")" | $OPENSSL_AES_CBC_256_DEC -K "$key" -iv "$iv")
  140. }
  141.  
  142. # 1:hex_raw_key
  143. function hrk2hk {
  144. declare -A hk
  145. hk[0]=$(( 0x${1:0:16} ^ 0x${1:32:16} ))
  146. hk[1]=$(( 0x${1:16:16} ^ 0x${1:48:16} ))
  147. printf "%016x%016x" ${hk[0]} ${hk[1]}
  148. }
  149.  
  150. # 1:link
  151. function get_mc_link_info {
  152.  
  153. local MC_API_URL=$(echo -n "$1" | grep -i -E -o 'https?://[^/]+')"/api"
  154.  
  155. local download_exit_code=1
  156.  
  157. local info_link=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "{\"m\":\"info\", \"link\":\"$1\"}" "$MC_API_URL")
  158.  
  159. download_exit_code=$?
  160.  
  161. if [ "$download_exit_code" -ne 0 ]; then
  162. echo -e "ERROR: Oooops, something went bad. EXIT CODE (${download_exit_code})"
  163. return 1
  164. fi
  165.  
  166. if [ $(echo $info_link | grep '"error"') ]; then
  167. local error_code=$(echo "$info_link" | jq -r .error)
  168. echo -e "MEGACRYPTER ERROR $error_code"
  169. return 1
  170. fi
  171.  
  172. local expire=$(echo "$info_link" | jq -r .expire)
  173.  
  174. if [ "$expire" != "false" ]; then
  175.  
  176. IFS='#' read -a array <<< "$expire"
  177.  
  178. local no_exp_token=${array[1]}
  179. else
  180. local no_exp_token="$expire"
  181. fi
  182.  
  183. local file_name=$(echo "$info_link" | jq -r .name | base64 -w 0 -i 2>/dev/null)
  184.  
  185. local path=$(echo "$info_link" | jq -r .path)
  186.  
  187. if [ "$path" != "false" ]; then
  188. path=$(echo -n "$path" | base64 -w 0 -i 2>/dev/null)
  189. fi
  190.  
  191. local mc_pass=$(echo "$info_link" | jq -r .pass)
  192.  
  193. local file_size=$(echo "$info_link" | jq -r .size)
  194.  
  195. local key=$(echo "$info_link" | jq -r .key)
  196.  
  197. echo -n "${file_name}@${path}@${file_size}@${mc_pass}@${key}@${no_exp_token}"
  198. }
  199.  
  200. # 1:file_name 2:file_size 3:formatted_file_size [4:md5_mclink]
  201. function check_file_exists {
  202.  
  203. if [ -f "$1" ]; then
  204.  
  205. local actual_size=$(stat -c %s "$1")
  206.  
  207. if [ "$actual_size" == "$2" ]; then
  208.  
  209. if [ -n "$4" ] && [ -f ".megadown/${4}" ]; then
  210. rm ".megadown/${4}"
  211. fi
  212.  
  213. showError "WARNING: File $1 exists. Download aborted!"
  214. fi
  215.  
  216. DL_MSG="\nFile $1 exists but with different size (${2} vs ${actual_size} bytes). Downloading [${3}] ...\n"
  217.  
  218. else
  219.  
  220. DL_MSG="\nDownloading $1 [${3}] ...\n"
  221.  
  222. fi
  223. }
  224.  
  225. # 1:file_size
  226. function format_file_size {
  227.  
  228. if [ "$1" -ge 1073741824 ]; then
  229. local file_size_f=$(awk "BEGIN { rounded = sprintf(\"%.1f\", ${1}/1073741824); print rounded }")" GB"
  230. elif [ "$1" -ge 1048576 ];then
  231. local file_size_f=$(awk "BEGIN { rounded = sprintf(\"%.1f\", ${1}/1048576); print rounded }")" MB"
  232. else
  233. local file_size_f="${1} bytes"
  234. fi
  235.  
  236. echo -ne "$file_size_f"
  237. }
  238.  
  239. # 1:password 2:salt 3:iterations
  240. function mc_pbkdf2 {
  241.  
  242. echo -e "import sys,hashlib,base64\nprint(base64.b64encode(hashlib.pbkdf2_hmac('sha256', b'${1}', base64.b64decode(b'${2}'), ${3})).decode())" | python
  243. }
  244.  
  245. # 1:mc_pass_info 2:pass_to_check
  246. function mc_pass_check {
  247.  
  248. IFS='#' read -a array <<< "$1"
  249.  
  250. local iter_log2=${array[0]}
  251.  
  252. local key_check=${array[1]}
  253.  
  254. local salt=${array[2]}
  255.  
  256. local iv=${array[3]}
  257.  
  258. local mc_pass_hash=$(mc_pbkdf2 "$password" "$salt" $((2**$iter_log2)))
  259.  
  260. mc_pass_hash=$(echo -n "$mc_pass_hash" | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
  261.  
  262. iv=$(echo -n "$iv" | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
  263.  
  264. if [ "$(echo -n "$key_check" | $OPENSSL_AES_CBC_256_DEC -K "$mc_pass_hash" -iv "$iv" 2>/dev/null | od -v -An -t x1 | tr -d '\n ')" != "$mc_pass_hash" ]; then
  265. echo -n "0"
  266. else
  267. echo -n "${mc_pass_hash}#${iv}"
  268. fi
  269. }
  270.  
  271. #1:string
  272. function trim {
  273.  
  274. if [[ "$1" =~ \ *([^ ]|[^ ].*[^ ])\ * ]]; then
  275. echo -n "${BASH_REMATCH[1]}"
  276. fi
  277. }
  278.  
  279. #1:pattern 2:subject 3:group
  280. function regex_match {
  281.  
  282. if [[ "$2" =~ $1 ]]; then
  283. echo -n "${BASH_REMATCH[$3]}"
  284. fi
  285. }
  286.  
  287. #1:pattern 2:subject 3:group
  288. function regex_imatch {
  289.  
  290. shopt -s nocasematch
  291.  
  292. if [[ "$2" =~ $1 ]]; then
  293. echo -n "${BASH_REMATCH[$3]}"
  294. fi
  295.  
  296. shopt -u nocasematch
  297. }
  298.  
  299. #MAIN STARTS HERE:
  300. check_deps
  301.  
  302. if [ -z "$1" ]; then
  303. showHelp
  304. exit 1
  305. fi
  306.  
  307. eval set -- "$(getopt -o "l:p:k:o:s:qm" -l "list:,password:,key:,output:,speed:,quiet,metadata" -n ${0} -- "$@")"
  308.  
  309. while true; do
  310. case "$1" in
  311. -l|--list) list="$2"; shift 2;;
  312. -p|--password) password="$2"; shift 2;;
  313. -o|--output) output="$2"; shift 2;;
  314. -s|--speed) speed="$2"; shift 2;;
  315. -q|--quiet) quiet=true; shift 1;;
  316. -m|--metadata) metadata=true; shift 1;;
  317.  
  318. --) shift; break;;
  319.  
  320. *)
  321. showHelp
  322. exit 1;;
  323. esac
  324. done
  325.  
  326. p1=$(trim $(urldecode "$1"))
  327.  
  328. if [[ "$p1" =~ ^http ]] || [[ "$p1" =~ ^mega:// ]]; then
  329. link="$p1"
  330. fi
  331.  
  332. if [ -z "$link" ]; then
  333.  
  334. if [ -z "$list" ]; then
  335.  
  336. showHelp
  337.  
  338. showError "ERROR: MEGA/MC link or --list parameter is required"
  339.  
  340. elif [ ! -f "$list" ]; then
  341.  
  342. showHelp
  343.  
  344. showError "ERROR: list file ${list} not found"
  345. fi
  346.  
  347. if [ ! $quiet ]; then
  348. echo -ne "\n(Pre)reading mc links info..."
  349. fi
  350.  
  351. link_count=0
  352.  
  353. while IFS='' read -r line || [ -n "$line" ]; do
  354.  
  355. if [ -n "$line" ] && ! [ $(echo -n "$line" | grep -E -o 'mega://enc') ];then
  356.  
  357. link=$(regex_imatch "^.*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
  358.  
  359. if [ $(echo -n "$link" | grep -E -o 'https?://[^/]+/!') ]; then
  360.  
  361. md5=$(echo -n "$link" | $OPENSSL_MD5 | grep -E -o '[0-9a-f]{32}')
  362.  
  363. if [ ! -f ".megadown/${md5}" ];then
  364.  
  365. mc_link_info=$(get_mc_link_info "$link")
  366.  
  367. if ! [ "$?" -eq 1 ];then
  368. echo -n "$mc_link_info" >> ".megadown/${md5}"
  369. fi
  370. fi
  371.  
  372. link_count=$((link_count + 1))
  373. fi
  374. fi
  375.  
  376. done < "$list"
  377.  
  378. echo -ne " OK(${link_count} MC links found)\n"
  379.  
  380. while IFS='' read -r line || [ -n "$line" ]; do
  381.  
  382. if [ -n "$line" ];then
  383.  
  384. if [ $(echo -n "$line" | grep -E -o 'mega://enc') ]; then
  385.  
  386. link=$(regex_imatch "^.*?(mega:\/\/enc\d*?\?[a-z0-9_-]+).*$" "$line" 1)
  387.  
  388. output=$(regex_imatch "^.*?mega:\/\/enc\d*?\?[a-z0-9_-]+(.*)$" "$line" 1 1)
  389.  
  390.  
  391. elif [ $(echo -n "$line" | grep -E -o 'https?://') ]; then
  392.  
  393. link=$(regex_imatch ".*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
  394.  
  395. output=$(regex_imatch "^.*?https?\:\/\/[^\/]+\/[#!0-9a-z_-]+(.*)$" "$line" 1 1)
  396.  
  397. else
  398. continue
  399. fi
  400.  
  401. $SCRIPT "$link" --output="$output" --password="$password" --speed="$speed"
  402.  
  403. fi
  404.  
  405. done < "$list"
  406.  
  407. exit 0
  408. fi
  409.  
  410. if [ $(echo -n "$link" | grep -E -o 'mega://enc') ]; then
  411. link=$(decrypt_md_link "$link")
  412. fi
  413.  
  414. if [ ! $quiet ]; then
  415. echo -e "\nReading link metadata..."
  416. fi
  417.  
  418. if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz') ]; then
  419.  
  420. #MEGA.CO.NZ LINK
  421.  
  422. file_id=$(regex_match "^.*\/#.*?!(.+)!.*$" "$link" 1)
  423.  
  424. file_key=$(regex_match "^.*\/#.*?!.+!(.+)$" "$link" 1)
  425.  
  426. hex_raw_key=$(echo -n $(urlb64_to_b64 "$file_key") | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
  427.  
  428. if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz/#!') ]; then
  429.  
  430. mega_req_json="[{\"a\":\"g\", \"p\":\"${file_id}\"}]"
  431.  
  432. mega_req_url="${MEGA_API_URL}/cs?id=&ak="
  433.  
  434. elif [ $(echo -n "$link" | grep -E -o -i 'mega(\.co)?\.nz/#N!') ]; then
  435.  
  436. mega_req_json="[{\"a\":\"g\", \"n\":\"${file_id}\"}]"
  437.  
  438. folder_id=$(regex_match "###n\=(.+)$" "$link" 1)
  439.  
  440. mega_req_url="${MEGA_API_URL}/cs?id=&ak=&n=${folder_id}"
  441. fi
  442.  
  443. mega_res_json=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "$mega_req_json" "$mega_req_url")
  444.  
  445. download_exit_code=$?
  446.  
  447. if [ "$download_exit_code" -ne 0 ]; then
  448. showError "Oooops, something went bad. EXIT CODE (${download_exit_code})"
  449. fi
  450.  
  451. if [ $(echo -n "$mega_res_json" | grep -E -o '\[ *\-[0-9]+ *\]') ]; then
  452. showError "MEGA ERROR $(echo -n "$mega_res_json" | grep -E -o '\-[0-9]+')"
  453. fi
  454.  
  455. file_size=$(echo "$mega_res_json" | jq -r .[0].s)
  456.  
  457. at=$(echo "$mega_res_json" | jq -r .[0].at)
  458.  
  459. hex_key=$(hrk2hk "$hex_raw_key")
  460.  
  461. at_dec_json=$(echo -n $(urlb64_to_b64 "$at") | $OPENSSL_AES_CBC_128_DEC -K "$hex_key" -iv "00000000000000000000000000000000" -nopad | tr -d '\0')
  462.  
  463. if [ ! $(echo -n "$at_dec_json" | grep -E -o 'MEGA') ]; then
  464. showError "MEGA bad link"
  465. fi
  466.  
  467. if [ -z "$output" ]; then
  468. file_name=$(echo -n "$at_dec_json" | grep -E -o '\{.+\}' | jq -r .n)
  469. else
  470. file_name="$output"
  471. fi
  472.  
  473. if [ $metadata ]; then
  474. echo "{\"file_name\" : \"${file_name}\", \"file_size\" : ${file_size}}"
  475. exit 0
  476. fi
  477.  
  478. check_file_exists "$file_name" "$file_size" "$(format_file_size "$file_size")"
  479.  
  480. if [ $(echo -n "$link" | grep -E -o 'mega(\.co)?\.nz/#!') ]; then
  481. mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"p\":\"$file_id\"}]"
  482. elif [ $(echo -n "$link" | grep -E -o -i 'mega(\.co)?\.nz/#N!') ]; then
  483. mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"n\":\"$file_id\"}]"
  484. fi
  485.  
  486. mega_res_json=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "$mega_req_json" "$mega_req_url")
  487.  
  488. download_exit_code=$?
  489.  
  490. if [ "$download_exit_code" -ne 0 ]; then
  491. showError "Oooops, something went bad. EXIT CODE (${download_exit_code})"
  492. fi
  493.  
  494. dl_temp_url=$(echo "$mega_res_json" | jq -r .[0].g)
  495. else
  496.  
  497. #MEGACRYPTER LINK
  498.  
  499. MC_API_URL=$(echo -n "$link" | grep -i -E -o 'https?://[^/]+')"/api"
  500.  
  501. md5=$(echo -n "$link" | $OPENSSL_MD5 | grep -E -o '[0-9a-f]{32}')
  502.  
  503. if [ -f ".megadown/${md5}" ];then
  504. mc_link_info=$(cat ".megadown/${md5}")
  505. else
  506. mc_link_info=$(get_mc_link_info "$link")
  507.  
  508. if [ "$?" -eq 1 ];then
  509. echo -e "$mc_link_info"
  510. exit 1
  511. fi
  512.  
  513. echo -n "$mc_link_info" >> ".megadown/${md5}"
  514. fi
  515.  
  516. IFS='@' read -a array <<< "$mc_link_info"
  517.  
  518. if [ -z "$output" ];then
  519. file_name=$(echo -n "${array[0]}" | base64 -d -i 2>/dev/null)
  520. else
  521. file_name="$output"
  522. fi
  523.  
  524. path=${array[1]}
  525.  
  526. if [ "$path" != "false" ]; then
  527. path=$(echo -n "$path" | base64 -d -i 2>/dev/null)
  528. fi
  529.  
  530. file_size=${array[2]}
  531.  
  532. mc_pass=${array[3]}
  533.  
  534. key=${array[4]}
  535.  
  536. no_exp_token=${array[5]}
  537.  
  538. if [ "$mc_pass" != "false" ]; then
  539.  
  540. if [ -z "$(command -v python 2>&1)" ]; then
  541.  
  542. echo "ERROR: python is required for MegaCrypter password protected links and it's not installed."
  543. exit 1
  544.  
  545. fi
  546.  
  547. echo -ne "\nLink is password protected. "
  548.  
  549. if [ -n "$password" ]; then
  550.  
  551. pass_hash=$(mc_pass_check "$mc_pass" "$password")
  552.  
  553. fi
  554.  
  555. if [ -z "$pass_hash" ] || [ "$pass_hash" == "0" ]; then
  556.  
  557. echo -ne "\n\n"
  558.  
  559. read -e -p "Enter password: " pass
  560.  
  561. pass_hash=$(mc_pass_check "$mc_pass" "$pass")
  562.  
  563. until [ "$pass_hash" != "false" ]; do
  564. read -e -p "Wrong password! Try again: " pass
  565. pass_hash=$(mc_pass_check "$mc_pass" "$pass")
  566. done
  567. fi
  568.  
  569. echo -ne "\nPassword is OK. Decrypting metadata...\n"
  570.  
  571. IFS='#' read -a array <<< "$pass_hash"
  572.  
  573. pass_hash=${array[0]}
  574.  
  575. iv=${array[1]}
  576.  
  577. hex_raw_key=$(echo -n "$key" | $OPENSSL_AES_CBC_256_DEC -K "$pass_hash" -iv "$iv" | od -v -An -t x1 | tr -d '\n ')
  578.  
  579. if [ -z "$output" ]; then
  580. file_name=$(echo -n "$file_name" | $OPENSSL_AES_CBC_256_DEC -K "$pass_hash" -iv "$iv")
  581. fi
  582. else
  583. hex_raw_key=$(echo -n $(urlb64_to_b64 "$key") | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
  584. fi
  585.  
  586. if [ $metadata ]; then
  587. echo "{\"file_name\" : \"${file_name}\", \"file_size\" : ${file_size}}"
  588. exit 0
  589. fi
  590.  
  591. if [ "$path" != "false" ] && [ "$path" != "" ]; then
  592.  
  593. if [ ! -d "$path" ]; then
  594.  
  595. mkdir -p "$path"
  596. fi
  597.  
  598. file_name="${path}${file_name}"
  599. fi
  600.  
  601. check_file_exists "$file_name" "$file_size" "$(format_file_size "$file_size")" "$md5"
  602.  
  603. hex_key=$(hrk2hk "$hex_raw_key")
  604.  
  605. dl_link=$($DL_COM --header 'Content-Type: application/json' $DL_COM_PDATA "{\"m\":\"dl\", \"link\":\"$link\", \"noexpire\":\"$no_exp_token\"}" "$MC_API_URL")
  606.  
  607. download_exit_code=$?
  608.  
  609. if [ "$download_exit_code" -ne 0 ]; then
  610. showError "Oooops, something went bad. EXIT CODE (${download_exit_code})"
  611. fi
  612.  
  613. if [ $(echo $dl_link | grep '"error"') ]; then
  614.  
  615. error_code=$(echo "$dl_link" | jq -r .error)
  616.  
  617. showError "MEGACRYPTER ERROR $error_code"
  618. fi
  619.  
  620. dl_temp_url=$(echo "$dl_link" | jq -r .url)
  621.  
  622. if [ "$mc_pass" != "false" ]; then
  623.  
  624. iv=$(echo "$dl_link" | jq -r .pass | base64 -d -i 2>/dev/null | od -v -An -t x1 | tr -d '\n ')
  625.  
  626. dl_temp_url=$(echo -n "$dl_temp_url" | $OPENSSL_AES_CBC_256_DEC -K "$pass_hash" -iv "$iv")
  627. fi
  628. fi
  629.  
  630. if [ -z "$speed" ]; then
  631. DL_COMMAND="$DL_COM"
  632. else
  633. DL_COMMAND="$DL_COM --limit-rate $speed"
  634. fi
  635.  
  636. if [ "$output" == "-" ]; then
  637.  
  638. hex_iv="${hex_raw_key:32:16}0000000000000000"
  639.  
  640. $DL_COMMAND "$dl_temp_url" | $OPENSSL_AES_CTR_128_DEC -K "$hex_key" -iv "$hex_iv"
  641.  
  642. exit 0
  643. fi
  644.  
  645. if [ ! $quiet ]; then
  646. echo -e "$DL_MSG"
  647. fi
  648.  
  649. if [ ! $quiet ]; then
  650. PV_CMD="pv"
  651. else
  652. PV_CMD="pv -q"
  653. fi
  654.  
  655. download_exit_code=1
  656.  
  657. until [ "$download_exit_code" -eq 0 ]; do
  658.  
  659. if [ -f "${file_name}.temp" ]; then
  660.  
  661. echo -e "(Resuming previous download ...)\n"
  662.  
  663. temp_size=$(stat -c %s "${file_name}.temp")
  664.  
  665. offset=$(($temp_size-$(($temp_size%16))))
  666.  
  667. iv_forward=$(printf "%016x" $(($offset/16)))
  668.  
  669. hex_iv="${hex_raw_key:32:16}$iv_forward"
  670.  
  671. truncate -s $offset "${file_name}.temp"
  672.  
  673. $DL_COMMAND "$dl_temp_url/$offset" | $PV_CMD -s $(($file_size-$offset)) | $OPENSSL_AES_CTR_128_DEC -K "$hex_key" -iv "$hex_iv" >> "${file_name}.temp"
  674. else
  675. hex_iv="${hex_raw_key:32:16}0000000000000000"
  676.  
  677. $DL_COMMAND "$dl_temp_url" | $PV_CMD -s $file_size | $OPENSSL_AES_CTR_128_DEC -K "$hex_key" -iv "$hex_iv" > "${file_name}.temp"
  678. fi
  679.  
  680. download_exit_code=${PIPESTATUS[0]}
  681.  
  682. if [ "$download_exit_code" -ne 0 ]; then
  683. showError "Oooops, download failed! EXIT CODE (${download_exit_code})"
  684. fi
  685. done
  686.  
  687. if [ ! -f "${file_name}.temp" ]; then
  688. showError "ERROR: FILE COULD NOT BE DOWNLOADED :(!"
  689. fi
  690.  
  691. mv "${file_name}.temp" "${file_name}"
  692.  
  693. if [ -f ".megadown/${md5}" ];then
  694. rm ".megadown/${md5}"
  695. fi
  696.  
  697. if [ ! $quiet ]; then
  698. echo -e "\nFILE DOWNLOADED!\n"
  699. fi
  700.  
  701. exit 0
Add Comment
Please, Sign In to add comment