-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmegafetch.sh
More file actions
executable file
·78 lines (65 loc) · 1.91 KB
/
Copy pathmegafetch.sh
File metadata and controls
executable file
·78 lines (65 loc) · 1.91 KB
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
#!/bin/bash
# Based on community mega URL parser scripts used by MapleLegends Linux installers.
# Input : MEGA file URL
# Output:
# line1 = direct download URL
# line2 = file name
# line3 = AES key (hex)
# line4 = IV (hex)
set -euo pipefail
URL="${1:-}"
if [[ ! "$URL" =~ ^https?://mega(\.co)?\.nz ]]; then
echo "Usage: ${0##*/} <mega-url>" >&2
exit 1
fi
command -v curl >/dev/null 2>&1 || { echo "curl not found" >&2; exit 1; }
command -v openssl >/dev/null 2>&1 || { echo "openssl not found" >&2; exit 1; }
# Parse ID / key from both old and new MEGA URL patterns.
if [[ "$URL" =~ .*/file/[^#]*#[^#]* ]]; then
id="${URL#*file/}"
id="${id%%#*}"
key="${URL##*file/}"
key="${key##*#}"
else
id="${URL#*!}"
id="${id%%!*}"
key="${URL##*!}"
fi
raw_hex=$(
echo "${key}=" | tr '\-_' '+/' | tr -d ',' | base64 -d -i 2>/dev/null \
| od -v -An -t x1 | tr -d '\n '
)
hex=$(printf "%016x%016x" \
$((0x${raw_hex:0:16} ^ 0x${raw_hex:32:16})) \
$((0x${raw_hex:16:16} ^ 0x${raw_hex:48:16}))
)
json=$(curl -s -H 'Content-Type: application/json' \
-d '[{"a":"g", "g":"1", "p":"'"$id"'"}]' \
'https://g.api.mega.co.nz/cs?id=&ak=')
json="${json#"[{"}"
json="${json%"}]"}"
file_url="${json##*'"g":'}"
file_url="${file_url%%,*}"
file_url="${file_url//'"'/}"
json=$(curl -s -H 'Content-Type: application/json' \
-d '[{"a":"g", "p":"'"$id"'"}]' \
'https://g.api.mega.co.nz/cs?id=&ak=')
at="${json##*'"at":'}"
at="${at%%,*}"
at="${at//'"'/}"
meta=$(
echo "${at}==" | tr '\-_' '+/' | tr -d ',' \
| openssl enc -a -A -d -aes-128-cbc -K "$hex" -iv "00000000000000000000000000000000" -nopad \
| tr -d '\0'
)
meta="${meta#"MEGA{"}"
meta="${meta%"}"}"
file_name="${meta##*'"n":'}"
if [[ "$file_name" == *,* ]]; then
file_name="${file_name%%,*}"
fi
file_name="${file_name//'"'/}"
echo "$file_url"
echo "$file_name"
echo "$hex"
echo "${raw_hex:32:16}0000000000000000"