forked from mcrapet/plowshare-modules-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigfile.sh
461 lines (362 loc) · 13.8 KB
/
bigfile.sh
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Plowshare bigfile.to module
# Copyright (c) 2016 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare. If not, see <http://www.gnu.org/licenses/>.
MODULE_BIGFILE_REGEXP_URL='https\?://\(www\.\)\?\(bigfile\.to\|uploadable\.ch\)/'
MODULE_BIGFILE_DOWNLOAD_OPTIONS="
AUTH,a,auth,a=USER:PASSWORD,User account"
MODULE_BIGFILE_DOWNLOAD_RESUME=no
MODULE_BIGFILE_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no
MODULE_BIGFILE_DOWNLOAD_SUCCESSIVE_INTERVAL=
MODULE_BIGFILE_UPLOAD_OPTIONS="
AUTH,a,auth,a=USER:PASSWORD,User account
FOLDER,,folder,s=FOLDER,Folder to upload files into"
MODULE_BIGFILE_UPLOAD_REMOTE_SUPPORT=yes
MODULE_BIGFILE_LIST_OPTIONS=""
MODULE_BIGFILE_LIST_HAS_SUBFOLDERS=no
MODULE_BIGFILE_PROBE_OPTIONS=""
MODULE_BIGFILE_DELETE_OPTIONS=""
# Static function. Proceed with login
# $1: authentication
# $2: cookie file
# $3: base URL
# stdout: account type ("free" or "premium") on success.
bigfile_login() {
local -r AUTH=$1
local -r COOKIE_FILE=$2
local -r BASE_URL=$3
local CV PAGE MSG LOGIN_DATA NAME TYPE
if CV=$(storage_get 'cookie_file'); then
echo "$CV" >"$COOKIE_FILE"
# Check for expired session.
PAGE=$(curl -b "$COOKIE_FILE" "$BASE_URL/indexboard.php") || return
if ! match '>Dashboard<' "$PAGE"; then
storage_set 'cookie_file'
return $ERR_EXPIRED_SESSION
fi
log_debug 'session (cached)'
MSG='reused login for'
else
LOGIN_DATA='userName=$USER&userPassword=$PASSWORD&autoLogin=on&action__login=normalLogin'
PAGE=$(post_login "$AUTH" "$COOKIE_FILE" "$LOGIN_DATA" \
"$BASE_URL/login.php") || return
if ! match 'Logging in' "$PAGE"; then
return $ERR_LOGIN_FAILED
fi
storage_set 'cookie_file' "$(cat "$COOKIE_FILE")"
PAGE=$(curl -b "$COOKIE_FILE" "$BASE_URL/indexboard.php") || return
log_debug 'session (new)'
MSG='logged in as'
fi
NAME=$(parse_quiet 'id="dashboard_box"' '>\([^<]*\)<' 4 <<< "$PAGE")
if match '>Upgrade Now<' "$PAGE"; then
TYPE='free'
else
TYPE='premium'
fi
log_debug "Successfully $MSG '$TYPE' member '$NAME'"
echo $TYPE
}
# Output a bigfile file download URL and name
# $1: cookie file
# $2: bigfile url
# stdout: file download link
bigfile_download() {
local -r COOKIE_FILE=$1
local URL=$2
local -r BASE_URL='https://www.bigfile.to'
local FILE_ID ACCOUNT PAGE JSON FILE_URL WAIT_TIME
FILE_ID=$(parse . '/file/\([^/]\+\)' <<< "$URL") || return
URL="$BASE_URL/file/$FILE_ID"
readonly URL
if [ -n "$AUTH" ]; then
ACCOUNT=$(bigfile_login "$AUTH" "$COOKIE_FILE" "$BASE_URL") || return
fi
# Note: Save HTTP headers to catch premium users' "direct downloads".
PAGE=$(curl -i -c "$COOKIE_FILE" -b "$COOKIE_FILE" "$URL") || return
if match 'File not available\|cannot be found on the server\|no longer available\|Page not found' "$PAGE"; then
return $ERR_LINK_DEAD
fi
# If this is a premium download, we already have a download link.
if [ "$ACCOUNT" = 'premium' ]; then
MODULE_BIGFILE_DOWNLOAD_RESUME=yes
# Get a download link, if this was a direct download.
FILE_URL=$(grep_http_header_location_quiet <<< "$PAGE")
if [ -z "$FILE_URL" ]; then
PAGE=$(curl -b "$COOKIE_FILE" \
-d 'download=premium' \
-i "$URL") || return
FILE_URL=$(grep_http_header_location <<< "$PAGE") || return
fi
echo "$FILE_URL"
return 0
fi
if match 'var reCAPTCHA_publickey' "$PAGE"; then
local PUBKEY WCI CHALLENGE WORD ID
# http://www.google.com/recaptcha/api/challenge?k=
PUBKEY=$(parse 'var reCAPTCHA_publickey' "var reCAPTCHA_publickey='\([^']\+\)" <<< "$PAGE") || return
fi
JSON=$(curl -b "$COOKIE_FILE" \
-d 'downloadLink=wait' \
"$URL") || return
WAIT_TIME=$(parse_json 'waitTime' <<< "$JSON") || return
wait $WAIT_TIME || return
JSON=$(curl -b "$COOKIE_FILE" \
-d 'checkDownload=check' \
"$URL") || return
if match '"fail":"timeLimit"' "$JSON"; then
local HOURS MINS SECS
PAGE=$(curl -b "$COOKIE_FILE" \
-d 'checkDownload=showError' \
-d 'errorType=timeLimit' \
"$URL") || return
HOURS=$(parse_quiet '>Please wait' \
'[^[:digit:]]\([[:digit:]]\+\) hours\?' <<< "$PAGE")
MINS=$(parse_quiet '>Please wait' \
'[^[:digit:]]\([[:digit:]]\+\) minutes\?' <<< "$PAGE")
SECS=$(parse_quiet '>Please wait' \
'[^[:digit:]]\([[:digit:]]\+\) seconds\?' <<< "$PAGE")
log_error 'Download limit reached.'
# Note: Always use decimal base instead of octal if there are leading zeros.
echo $(( (( 10#$HOURS * 60 ) + 10#$MINS ) * 60 + 10#$SECS ))
return $ERR_LINK_TEMP_UNAVAILABLE
elif ! match '"success":"showCaptcha"' "$JSON"; then
log_error "Unexpected response: $JSON"
return $ERR_FATAL
fi
if [ -n "$PUBKEY" ]; then
WCI=$(recaptcha_process $PUBKEY) || return
{ read WORD; read CHALLENGE; read ID; } <<<"$WCI"
JSON=$(curl -b "$COOKIE_FILE" \
-d "recaptcha_challenge_field=$CHALLENGE" \
-d "recaptcha_response_field=$WORD" \
-d "recaptcha_shortencode_field=$FILE_ID" \
"$BASE_URL/checkReCaptcha.php") || return
if ! match '"success":1' "$JSON"; then
captcha_nack $ID
log_error 'Wrong captcha'
return $ERR_CAPTCHA
fi
captcha_ack $ID
log_debug 'Correct captcha'
fi
PAGE=$(curl -b "$COOKIE_FILE" \
-d 'downloadLink=show' \
"$URL") || return
PAGE=$(curl -b "$COOKIE_FILE" \
-d 'download=normal' \
-i "$URL") || return
grep_http_header_location <<< "$PAGE" || return
}
# Check if specified folder name is valid.
# $1: folder name selected by user
# $2: cookie file (logged into account)
# $3: base url
# stdout: folder ID
bigfile_check_folder() {
local -r NAME=$1
local -r COOKIE_FILE=$2
local -r BASE_URL=$3
local JSON FOLDERS FOLDERS_N FOLDER_ID
log_debug 'Getting folder data'
JSON=$(curl -b "$COOKIE_FILE" \
-d 'current_page=1' \
-d 'extra=folderPanel' \
"$BASE_URL/file-manager-expand-folder.php") || return
FOLDERS=$(replace_all '{', $'\n{' <<< "$JSON") || return
FOLDERS=$(replace_all '}', $'}\n' <<< "$FOLDERS") || return
FOLDERS_N=$(parse_all_quiet '"folderName":"' '"folderName":"\([^"]\+\)' <<< "$FOLDERS")
if ! match "^$NAME$" "$FOLDERS_N"; then
log_debug "Creating folder: '$NAME'"
JSON=$(curl -b "$COOKIE_FILE" \
-d "newFolderName=$NAME" \
-d 'createFolderDest=0' \
"$BASE_URL/file-manager-action.php") || return
if ! match '"success":true' "$JSON"; then
log_error 'Failed to create folder.'
return $ERR_FATAL
fi
JSON=$(curl -b "$COOKIE_FILE" \
-d 'current_page=1' \
-d 'extra=folderPanel' \
"$BASE_URL/file-manager-expand-folder.php") || return
FOLDERS=$(replace_all '{', $'\n{' <<< "$JSON") || return
FOLDERS=$(replace_all '}', $'}\n' <<< "$FOLDERS") || return
fi
FOLDER_ID=$(parse "\"folderName\":\"$NAME\"" '"folderId":"\([^"]\+\)' <<< "$FOLDERS") || return
log_debug "Folder ID: '$FOLDER_ID'"
echo "$FOLDER_ID"
}
# Upload a file to bigfile
# $1: cookie file
# $2: file path or remote url
# $3: remote filename
# stdout: download link + delete link
bigfile_upload() {
local -r COOKIE_FILE=$1
local -r FILE=$2
local -r DESTFILE=$3
local -r BASE_URL='https://www.bigfile.to'
local ACCOUNT PAGE JSON UPLOAD_URL FILE_ID FILE_NAME DEL_CODE
# Sanity checks
if [ -z "$AUTH" ]; then
if [ -n "$FOLDER" ]; then
log_error 'You must be registered to use folders.'
return $ERR_LINK_NEED_PERMISSIONS
elif match_remote_url "$FILE"; then
log_error 'You must be registered to do remote uploads.'
return $ERR_LINK_NEED_PERMISSIONS
fi
fi
if match_remote_url "$FILE"; then
if [ -n "$FOLDER" ]; then
log_error 'You cannot choose folder for remote link.'
return $ERR_LINK_NEED_PERMISSIONS
fi
fi
if [ -n "$AUTH" ]; then
ACCOUNT=$(bigfile_login "$AUTH" "$COOKIE_FILE" "$BASE_URL") || return
fi
if [ -n "$FOLDER" ]; then
FOLDER_ID=$(bigfile_check_folder "$FOLDER" "$COOKIE_FILE" "$BASE_URL") || return
fi
PAGE=$(curl -c "$COOKIE_FILE" -b "$COOKIE_FILE" \
"$BASE_URL/index.php") || return
if ! match_remote_url "$FILE"; then
local MAX_SIZE SZ
SZ=$(get_filesize "$FILE")
if [ "$ACCOUNT" = 'premium' ]; then
MAX_SIZE='5368709120' # 5 GiB
else
MAX_SIZE='2147483648' # 2 GiB
fi
log_debug "Max size: $MAX_SIZE"
if [ "$SZ" -gt "$MAX_SIZE" ]; then
log_debug "File is bigger than $MAX_SIZE."
return $ERR_SIZE_LIMIT_EXCEEDED
fi
fi
# Upload remote file
if match_remote_url "$FILE"; then
if ! match '^https\?://' "$FILE" && ! match '^ftp://' "$FILE"; then
log_error 'Unsupported protocol for remote upload.'
return $ERR_BAD_COMMAND_LINE
fi
PAGE=$(curl -b "$COOKIE_FILE" \
-d "urls=$FILE" \
-d 'remoteUploadFormType=web' \
-d 'showPage=remoteUploadFormWeb.tpl' \
"$BASE_URL/uploadremote.php") || return
if ! match 'Upload Successful' "$PAGE"; then
log_error 'Remote upload failed.'
return $ERR_FATAL
fi
log_error 'Once remote upload completed, check your account for link.'
return $ERR_ASYNC_REQUEST
# Upload local file
else
UPLOAD_URL=$(parse 'var uploadUrl' "var uploadUrl = '\([^']\+\)" <<< "$PAGE") || return
JSON=$(curl_with_log -X PUT \
-H "X-File-Name: $DESTFILE" \
-H "X-File-Size: $SZ" \
-H "Origin: $BASE_URL" \
--data-binary "@$FILE" \
"$UPLOAD_URL") || return
DEL_CODE=$(parse_json 'deleteCode' <<< "$JSON") || return
FILE_NAME=$(parse_json 'fileName' <<< "$JSON") || return
FILE_ID=$(parse_json 'shortenCode' <<< "$JSON") || return
fi
if [ -n "$FOLDER" ]; then
local UPLOAD_ID
log_debug "Moving file to folder '$FOLDER'..."
# Get root folder content dorted by upload date DESC
# Last uploaded file will be on top
JSON=$(curl -b "$COOKIE_FILE" \
-d 'parent_folder_id=0' \
-d 'current_page=1' \
-d 'sort_field=2' \
-d 'sort_order=DESC' \
"$BASE_URL/file-manager-expand-folder.php") || return
JSON=$(replace_all '{', $'\n{' <<< "$JSON") || return
JSON=$(replace_all '}', $'}\n' <<< "$JSON") || return
UPLOAD_ID=$(parse "$FILE_ID" '"uploadId":"\([^"]\+\)' <<< "$JSON") || return
log_debug "Upload ID: '$UPLOAD_ID'"
JSON=$(curl -b "$COOKIE_FILE" \
-d "moveFolderId=$UPLOAD_ID" \
-d "moveFolderDest=$FOLDER_ID" \
-d 'CurrentFolderId=0' \
"$BASE_URL/file-manager-action.php") || return
if ! match '"successCount":1' "$JSON"; then
log_error 'Could not move file into folder.'
fi
fi
echo "${BASE_URL}/file/$FILE_ID/$FILE_NAME"
echo "${BASE_URL}/file/$FILE_ID/delete/$DEL_CODE"
}
# Probe a download URL
# $1: cookie file (unused here)
# $2: bigfile url
# $3: requested capability list
# stdout: 1 capability per line
bigfile_probe() {
local -r URL=$2
local -r REQ_IN=$3
local PAGE FILE_NAME FILE_SIZE REQ_OUT
PAGE=$(curl -L "$URL") || return
if match 'File not available\|cannot be found on the server\|no longer available\|Page not found' "$PAGE"; then
return $ERR_LINK_DEAD
fi
REQ_OUT=c
if [[ $REQ_IN = *f* ]]; then
parse_attr '"file_name"' 'title' <<< "$PAGE" &&
REQ_OUT="${REQ_OUT}f"
fi
if [[ $REQ_IN = *s* ]]; then
FILE_SIZE=$(parse '"filename_normal"' '>(\([^)]\+\)' <<< "$PAGE") && \
translate_size "$FILE_SIZE" && REQ_OUT="${REQ_OUT}s"
fi
echo $REQ_OUT
}
# List a bigfile web folder URL
# $1: folder URL
# $2: recurse subfolders (null string means not selected)
# stdout: list of links and file names (alternating)
bigfile_list() {
local -r URL=$1
local -r REC=$2
local PAGE LINKS NAMES
PAGE=$(curl -L "$URL") || return
if match 'File not available\|cannot be found on the server\|no longer available\|Page not found' "$PAGE"; then
return $ERR_LINK_DEAD
fi
NAMES=$(parse_all_quiet 'filename_normal' '">\(.*\) <span' <<< "$PAGE")
LINKS=$(parse_all_attr_quiet 'filename_normal' 'href' <<< "$PAGE")
list_submit "$LINKS" "$NAMES"
}
# Delete a file uploaded to bigfile
# $1: cookie file (unused here)
# $2: delete url
bigfile_delete() {
local URL=$2
local PAGE
PAGE=$(curl -L "$URL") || return
if match 'File not available\|cannot be found on the server\|no longer available\|Page not found\|File Delete Fail' "$PAGE"; then
return $ERR_LINK_DEAD
fi
if ! match 'File Deleted' "$PAGE"; then
return $ERR_FATAL
fi
}