-
Notifications
You must be signed in to change notification settings - Fork 0
/
megadl
executable file
·401 lines (377 loc) · 13.4 KB
/
megadl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env bash
set +xv
# megadl - Download mega.nz and MegaCrypter files
version=2.1
bash_min=3
Help(){ # I:name,version
cat <<-EOH
$name v$version - Download mega.nz and MegaCrypter files
Usage: $name [<options>] <URL> | -l|--list <file>
-s|--speed <speed> Download speed limit <int>B|K|M, example: 20K
-p|-password <password> Password for MegaCrypter links (same for all)
-m|--metadata Only display file metadata in JSON format
-q|--quiet Quiet mode
-h|--help This help text
Single <URL> mode options:
-o|--output <filename> Store the output file with this name
URL list in <file>:
Line format in file (FILENAME is optional): URL FILENAME
Repo: https://github.com/pepa65/misc
EOH
}
Error(){ # $1:message
echo -e "\n$1" >&2
exit 1
}
Check_deps(){ # O:dlcmd[opt],openssl_sup I:bash_min
local err=0 s c
if type -p curl >/dev/null
then dlcmd="curl --fail -s" dlcmdopt="--data"
elif type -p wget >/dev/null
then dlcmd="wget -q -O -" dlcmdopt="--post-data"
else echo "Required but not in PATH: wget | curl"; err=1
fi
for s in openssl pv jq
do
if ! type -p $s >/dev/null
then echo "Required and not installed: $s"; err=1
elif [[ $s = openssl ]]
then
openssl_sup=$(openssl enc -ciphers 2>&1)
for c in aes-128-ctr aes-128-cbc aes-256-cbc
do [[ -z $(grep -o "$c" <<<"$openssl_sup"|head -n1) ]] &&
echo "This openssl does not support $c" && err=1
done
fi
done
[[ $BASH_VERSION < $bash_min ]] &&
echo "At least bash version $bash_min is required" && err=1
((err)) && Error "ERROR: missing dependencies"
}
Urldecode(){ # $2:URL
: "${*//+/ }"; echo -e "${_//%/\\x}";
}
Urlb64_to_b64(){ # $1:base64_string
local b64=$(tr '_-' '/+' <<<"$1" |tr -d ',')
local pad=$(((4-${#1}%4)%4))
for i in $(seq 1 $pad)
do b64+==
done
echo -n "$b64"
}
Decrypt_md_link(){ # $1:mega_enc_link
local data=$(Regexi "^.*?mega:\/\/enc[0-9]*?\?([a-z0-9_,-]+).*?$" "$1" 1)
local iv="79F10A01844A0B27FF5B2D4E0ED3163E" key
grep -q 'mega://enc?' <<<"$1" &&
key=6B316F36416C2D316B7A3F217A30357958585858585858585858585858585858
grep 'mega://enc2?' <<<"$1" &&
key=ED1F4C200B35139806B260563B3D3876F011B4750F3A1A4A5EFD0BBE67554B44
$openssl_AES256_CBC -K "$key" -iv "$iv" <<<"https://mega.nz/#$(Urlb64_to_b64 "$data")"
}
Hrk2hk(){ # $1:hex_raw_key
local hk1=$((0x${1:0:16}^0x${1:32:16})) hk2=$((0x${1:16:16}^0x${1:48:16}))
printf '%016x' $hk1 $hk2
}
Get_mc_link_info(){ # $1:link
local MC_API_URL=$(grep -i -E -o 'https?://[^/]+' <<<"$1")/api
local rtn error_code expire no_exp_token
local info_link=$($dlcmd --header 'Content-Type: application/json' $dlcmdopt \
"{\"m\":\"info\",\"link\":\"$1\"}" "$MC_API_URL")
rtn=$?
((rtn)) &&
echo -e "ERROR: Download exit code $rtn" && return 1
grep '"error"' <<<"$info_link" &&
error_code=$(jq -r .error <<<"$info_link") &&
echo -e "MegaCrypter error $error_code" && return 1
expire=$(jq -r .expire <<<"$info_link") no_exp_token=$expire
[[ ! $expire = false ]] && IFS='#' read -a array <<<"$expire" &&
no_exp_token=${array[1]}
local file_name path mc_pass file_size key
file_name=$(jq -r .name <<<"$info_link" |base64 -w 0 -i 2>/dev/null)
path=$(jq -r .path <<<"$info_link")
[[ $path = false ]] || path=$(base64 -w 0 -i 2>/dev/null <<<"$path")
mc_pass=$(jq -r .pass <<<"$info_link")
file_size=$(jq -r .size <<<"$info_link")
key=$(jq -r .key <<<"$info_link")
echo -n "$file_name@$path@$file_size@$mc_pass@$key@$no_exp_token"
}
Checkfile(){ # $1:name $2:size $3:formattedsize [$4:md5_mclink] O:DL_MSG
[[ ! -f $1 ]] && DL_MSG="\nDownloading $1 [$3] ...\n" && return
local actual_size=$(stat -c %s "$1")
if [[ $actual_size = $2 ]]
then
[[ $4 && -f $dir/$4 ]] && rm "$dir/$4"
Error "WARNING: File $1 already exists. Download aborted!"
fi
DL_MSG="\nFile $1 exists but with different size ($2 vs $actual_size bytes). Downloading [$3] ...\n"
}
Format_file_size(){ # $1:filesize
local size gb=1073741824 mb=1048576 kb=1024
if (($1>=gb))
then size="$(bc <<<"scale=1; $1/$gb") GB"
elif (($1>=mb))
then size="$(bc <<<"scale=1; $1/$mb") MB"
elif (($1>=kb))
then size="$(bc <<<"scale=1; $1/$kb") kB"
else size="$1 B"
fi
echo -n "$size"
}
Mc_pbkdf2(){ # $1:password $2:salt $3:iterations
local python="import sys,hashlib,base64\nprint(base64.b64encode(hashlib."
python+="pbkdf2_hmac('sha256',b'$1',base64.b64decode(b'$2'),$3)).decode())"
echo -e "$python" |python
}
Mc_pass_check(){ # $1:mc_pass_info $2:pass_to_check I:openssl_AES256_CBC
local array iter_log2 key_check salt iv mc_pass_hash
IFS='#' read -a array <<<"$1"
iter_log2=${array[0]} key_check=${array[1]} salt=${array[2]} iv=${array[3]}
mc_pass_hash=$(Mc_pbkdf2 "$password" "$salt" $((2**iter_log2)))
mc_pass_hash=$(base64 -d -i 2>/dev/null <<<"$mc_pass_hash" |od -v -An -t x1 |
tr -d '\n ')
iv=$(base64 -d -i 2>/dev/null <<<"$iv" |od -v -An -t x1 |tr -d '\n ')
[[ ! $($openssl_AES256_CBC -K "$mc_pass_hash" -iv "$iv" 2>/dev/null <<<"$key_check" |
od -v -An -t x1 |tr -d '\n ') = $mc_pass_hash ]] &&
echo -n "0" || echo -n "$mc_pass_hash#$iv"
}
Trim(){ # $1:string
[[ $1 =~ \ *([^ ]|[^ ].*[^ ])\ * ]] && echo -n "${BASH_REMATCH[1]}"
}
Regex(){ # $1:pattern $2:subject $3:group
[[ $2 =~ $1 ]] && echo -n "${BASH_REMATCH[$3]}"
}
Regexi(){ # $1:pattern $2:subject $3:group
shopt -s nocasematch
[[ $2 =~ $1 ]] && echo -n "${BASH_REMATCH[$3]}"
shopt -u nocasematch
}
self=$(readlink -e "$0")
name=${self##*/}
dir=.$name
mega_api='https://g.api.mega.co.nz'
openssl_AES128_CTR='openssl enc -d -aes-128-ctr'
openssl_AES128_CBC='openssl enc -a -A -d -aes-128-cbc'
openssl_AES256_CBC='openssl enc -a -A -d -aes-256-cbc'
openssl_MD5='openssl md5'
Check_deps
list= password= output= speed= quiet=0 metadata=0
eval set -- "$(getopt -o "l:p:k:o:s:qhm" -l "list:,password:,key:,output:,speed:,quiet,help,metadata" -n $0 -- "$@")"
while :
do
case $1 in
-h|--help) Help; exit 0 ;;
-l|--list) list=$2; shift 2 ;;
-p|--password) password=$2; shift 2 ;;
-o|--output) output=$2; shift 2 ;;
-s|--speed) speed=$2; shift 2 ;;
-q|--quiet) quiet=1; shift ;;
-m|--metadata) metadata=1; shift ;;
--) shift; break ;;
*) Help; exit 1
esac
done
yen=$(echo $1 |sed 's|#|!|g' |sed 's|file/|#!|g')
p1=$(Trim $(Urldecode "$yen"))
[[ $p1 =~ ^http || $p1 =~ ^mega:// ]] && link=$p1
if [[ -z $link ]]
then if [[ -z $list ]]
then
Help
Error 'ERROR: mega.nz/MegaCrypter URL or -l|--list with FILE required'
elif [[ ! -f $list ]]
then
Help
Error "ERROR: file $list with the list of URLs not found"
fi
((quiet)) || echo -ne "\nPrereading MegaCrypter URLs"
link_count=0
while IFS='' read -r line || [[ $line ]]
do
if [[ $line && -z $(grep -E -o 'mega://enc' <<<"$line") ]]
then
link=$(Regexi "^.*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
if [[ $(grep -E -o 'https?://[^/]+/!' <<<"$link") ]]
then
md5=$($openssl_MD5 <<<"$link" |grep -E -o '[0-9a-f]{32}')
if [[ ! -f $dir/$md5 ]]
then
mc_link_info=$(Get_mc_link_info "$link")
(($?!=1)) && echo -n "$mc_link_info" >>"$dir/$md5"
fi
((++link_count))
fi
fi
done <"$list"
echo -ne " OK ($link_count MegaCrypter links found)"
while IFS='' read -r line || [[ $line ]]
do
if [[ $line ]]
then
if grep -qE -o 'mega://enc' <<<"$line"
then
link=$(Regexi "^.*?(mega:\/\/enc\d*?\?[a-z0-9_-]+).*$" "$line" 1)
output=$(Regexi "^.*?mega:\/\/enc\d*?\?[a-z0-9_-]+(.*)$" "$line" 1 1)
elif grep -qE -o 'https?://' <<<"$line"
then
link=$(Regexi ".*?(https?\:\/\/[^\/]+\/[#!0-9a-z_-]+).*$" "$line" 1)
output=$(Regexi "^.*?https?\:\/\/[^\/]+\/[#!0-9a-z_-]+(.*)$" "$line" 1 1)
else continue
fi
$self "$link" --output="$output" --password="$password" --speed="$speed"
fi
done <"$list"
exit 0
fi
grep -qE -o 'mega://enc' <<<"$link" && link=$(Decrypt_md_link "$link")
((quiet)) || echo -e "\nReading link metadata..."
[[ ! -d $dir ]] && mkdir $dir
if grep -qE -o 'mega(\.co)?\.nz' <<<"$link"
then # mega.nz link
file_id=$(Regex "^.*\/#.*?!(.+)!.*$" "$link" 1)
file_key=$(Regex "^.*\/#.*?!.+!(.+)$" "$link" 1)
hex_raw_key=$(echo -n $(Urlb64_to_b64 "$file_key") |
base64 -d -i 2>/dev/null |od -v -An -t x1 |tr -d '\n ')
if grep -qE -o 'mega(\.co)?\.nz/#!' <<<"$link"
then
mega_req_json="[{\"a\":\"g\", \"p\":\"$file_id\"}]"
mega_req_url="$mega_api/cs?id=&ak="
elif grep -qE -o -i 'mega(\.co)?\.nz/#N!' <<<"$link"
then
mega_req_json="[{\"a\":\"g\", \"n\":\"$file_id\"}]"
folder_id=$(Regex "###n\=(.+)$" "$link" 1)
mega_req_url="$mega_api/cs?id=&ak=&n=$folder_id"
fi
mega_res_json=$($dlcmd --header 'Content-Type: application/json' $dlcmdopt "$mega_req_json" "$mega_req_url")
rtn=$?
((rtn)) && Error "Download exit code $rtn"
grep -qEo '\[ *\-[0-9]+ *\]' <<<"$mega_res_json" &&
Error "MEGA ERROR $(grep -E -o '\-[0-9]+' <<<"$mega_res_json")"
file_size=$(jq -r .[0].s <<<"$mega_res_json")
at=$(jq -r .[0].at <<<"$mega_res_json")
hex_key=$(Hrk2hk "$hex_raw_key")
at_dec_json=$($openssl_AES128_CBC -K "$hex_key" -iv "00000000000000000000000000000000" -nopad <<<"$(Urlb64_to_b64 "$at")" |tr -d '\0')
grep -qEo 'MEGA' <<<"$at_dec_json" || Error "MEGA bad link"
[[ $output ]] && file_name="$output" ||
file_name=$(grep -E -o '\{.+\}' <<<"$at_dec_json" |jq -r .n)
((metadata)) &&
echo "{\"file_name\":\"$file_name\",\"file_size\":$file_size}" &&
exit 0
Checkfile "$file_name" "$file_size" "$(Format_file_size "$file_size")"
grep -qEo 'mega(\.co)?\.nz/#!' <<<"$link" &&
mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"p\":\"$file_id\"}]"
grep -qEoi 'mega(\.co)?\.nz/#N!' <<<"$link" &&
mega_req_json="[{\"a\":\"g\", \"g\":\"1\", \"n\":\"$file_id\"}]"
mega_res_json=$($dlcmd --header 'Content-Type: application/json' $dlcmdopt "$mega_req_json" "$mega_req_url")
rtn=$?
((rtn)) && Error "ERROR: download exit code $rtn"
dl_temp_url=$(jq -r .[0].g <<<"$mega_res_json")
else
# MegaCrypter link
MC_API_URL=$(grep -i -qE -o 'https?://[^/]+')"/api" <<<"$link"
md5=$(echo -n "$link" |$openssl_MD5 |grep -E -o '[0-9a-f]{32}')
if [[ -f $dir/$md5 ]]
then read -rd '' mc_link_info <"$dir/$md5"
else
mc_link_info=$(Get_mc_link_info "$link")
(($?==1)) && echo -e "$mc_link_info" && exit 3
echo -n "$mc_link_info" >>"$dir/$md5"
fi
IFS='@' read -a array <<<"$mc_link_info"
[[ $output ]] && file_name="$output" ||
file_name=$(base64 -d -i 2>/dev/null <<<"${array[0]}")
path=${array[1]}
[[ ! $path = false ]] && path=$(base64 -d -i 2>/dev/null <<<"$path")
file_size=${array[2]}
mc_pass=${array[3]}
key=${array[4]}
no_exp_token=${array[5]}
if [[ ! $mc_pass = false ]]
then
! type -p python >/dev/null &&
echo "ERROR: python required for MegaCrypter links but not installed" &&
exit 4
echo -ne "\nLink is password protected. "
[[ $password ]] && pass_hash=$(Mc_pass_check "$mc_pass" "$password")
if [[ -z $pass_hash || $pass_hash = 0 ]]
then
echo -e "\n"
read -erp "Enter password: " pass
pass_hash=$(Mc_pass_check "$mc_pass" "$pass")
until [[ ! $pass_hash = false ]]
do
read -erp "Wrong password! Try again: " pass
pass_hash=$(Mc_pass_check "$mc_pass" "$pass")
done
fi
echo -e "\nPassword OK. Decrypting metadata..."
IFS='#' read -a array <<<"$pass_hash"
pass_hash=${array[0]}
iv=${array[1]}
hex_raw_key=$($openssl_AES256_CBC -K "$pass_hash" -iv "$iv" <<<"$key" |
od -v -An -t x1 |tr -d '\n ')
[[ $output ]] || file_name=$($openssl_AES256_CBC -K "$pass_hash" -iv "$iv"
<<<"$file_name")
else
hex_raw_key=$(base64 -d -i 2>/dev/null <<<$(Urlb64_to_b64 "$key") |
od -v -An -t x1 |tr -d '\n ')
fi
[[ $metadata ]] &&
echo "{\"file_name\":\"$file_name\",\"file_size\":$file_size}" &&
exit 0
if [[ $path && ! $path = false ]]
then [[ -d $path ]] || mkdir -p "$path"
file_name="$path$file_name"
fi
Checkfile "$file_name" "$file_size" "$(Format_file_size "$file_size")" "$md5"
hex_key=$(Hrk2hk "$hex_raw_key")
dl_link=$($dlcmd --header 'Content-Type: application/json' $dlcmdopt \
"{\"m\":\"dl\",\"link\":\"$link\",\"noexpire\":\"$no_exp_token\"}" "$MC_API_URL")
rtn=$?
((rtn)) && Error "ERROR: download exit code $rtn"
grep -q '"error"' <<<"$dl_link" &&
error_code=$(jq -r .error <<<"$dl_link") &&
Error "MegaCrypter error $error_code"
dl_temp_url=$(jq -r .url <<<"$dl_link")
if [[ ! $mc_pass = false ]]
then
iv=$(jq -r .pass <<<"$dl_link" |base64 -d -i 2>/dev/null |od -v -An -t x1 |
tr -d '\n ')
dl_temp_url=$($openssl_AES256_CBC -K "$pass_hash" -iv "$iv"
<<<"$dl_temp_url")
fi
fi
[[ $speed ]] && dlcmd+=" --limit-rate $speed"
if [[ $output = '-' ]]
then
hex_iv="${hex_raw_key:32:16}0000000000000000"
$dlcmd "$dl_temp_url" |$openssl_AES128_CTR -K "$hex_key" -iv "$hex_iv"
exit 0
fi
((quiet)) || echo -e "$DL_MSG"
((quiet)) && PV_CMD="pv -q" || PV_CMD="pv"
rtn=1
until ((!rtn))
do
if [[ -f $file_name.temp ]]
then
echo -e "(Resuming previous download ...)\n"
temp_size=$(stat -c %s "$file_name.temp")
offset=$((temp_size-temp_size%16))
printf -v iv_forward '%016x' $((offset/16))
hex_iv="${hex_raw_key:32:16}$iv_forward"
truncate -s $offset "$file_name.temp"
$dlcmd "$dl_temp_url/$offset" |$PV_CMD -s $((file_size-offset)) |
$openssl_AES128_CTR -K "$hex_key" -iv "$hex_iv" >>"$file_name.temp"
else
hex_iv="${hex_raw_key:32:16}0000000000000000"
$dlcmd "$dl_temp_url" |$PV_CMD -s $file_size |
$openssl_AES128_CTR -K "$hex_key" -iv "$hex_iv" >"$file_name.temp"
fi
rtn=${PIPESTATUS[0]}
((rtn)) && Error "ERROR: download failed, exit code $rtn"
done
[[ ! -f $file_name.temp ]] && Error "ERROR: file could not be downloaded"
mv "$file_name.temp" "$file_name"
[[ -f $dir/$md5 ]] && rm "$dir/$md5"
((quiet)) || echo -e "\nFile downloaded\n"
exit 0