-
Notifications
You must be signed in to change notification settings - Fork 28
/
myaria2
executable file
·287 lines (233 loc) · 6.84 KB
/
myaria2
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
#!/usr/bin/env bash
# Github: https://github.com/Karmenzind/dotfiles-and-scripts/tree/master/others/aria2
# a script to manage aria2
# -----------------------------------------------------------------------------
# /* custom variables */
# url to fetch bt-tracker list
# if your have no idea what it is
# see http://www.senra.me/solutions-to-aria2-bt-metalink-download-slowly/
# trackerlist_url="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best_ip.txt"
trackerlist_url="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_ip.txt"
if command -v proxychains >/dev/null && [[ ${USE_PROXYCHAINS_FIRST:=false} == "true" ]]; then
use_pc=true
else
use_pc=false
fi
# command -v proxychains && [[ ${USE_PROXYCHAINS_FIRST:=false} == "true" ]] && trackerlist_url="proxychains $"
# update frequency
update_interval_hours=4
# to save your trackerlist
# no need to change
config_dir=${HOME}/.config/aria2 # trackerlist
conffile=${config_dir}/aria2.conf
conffileworking=${conffile}.lock
# parse from configuration
eval log_path=$(sed -n 's/^log=//p' $conffile)
# log_path=${log_path/\$\{HOME\}/$HOME}
eval download_dir=$(sed -n 's/^dir=//p' $conffile)
# echo $download_dir
# TODO(k): <2020-12-29>
local_dir=${HOME}/.local/aria2
dat_file=${local_dir}/session.dat
# open a web manage page while running this script
web_url='http://aria2c.com/'
browser=$(which chromium)
# move log file to specific dir when restart
history_dir=${HOME}/Downloads/aria2/logs
# init dirs and files
init_files() {
! [[ -d "$config_dir" ]] && mkdir -p $config_dir && echo "Created $config_dir"
! [[ -d "$download_dir" ]] && mkdir -p $download_dir && echo "Created $download_dir"
if ! [[ "$log_path" == "-" ]]; then
if [[ -n "$log_path" ]]; then
local log_dir=$(dirname $log_path)
! [[ -d "$log_dir" ]] && mkdir -p $log_dir
! [[ -e "$log_path" ]] && touch $log_path
fi
fi
! [[ -d "$local_dir" ]] && mkdir -p $local_dir && echo "Created $local_dir"
! [[ -e "$dat_file" ]] && touch $dat_file
! [[ -d "$history_dir" ]] && mkdir -p $history_dir && echo "Created $history_dir"
}
# -----------------------------------------------------------------------------
cut_off='--------------------------------------------'
save_trackerlist_path=${config_dir}/trackerlist
trackers=$(cat $save_trackerlist_path)
help() {
echo $cut_off
cat <<EOF
This is a script to manage your aria2.
Usage:
$0 [OPTION]
Options:
start [params] Start your aria2c and update bt-tracker list if conformed
*params: aria2c running paramters. See aria2c's man page
restart [params]
stop
update Force-update bt-tracker list and restart service
log Output appended log as the file grows
status Check running status
web Open web ui
rotate Backup log files
...(to be continued)
Feedback:
github: Karmenzind
email: valesail@gmail.com
EOF
}
install_service_if_not_exists() {
[[ -n "$(systemctl --user list-unit-files | grep aria2cd)" ]] && return
echo "Installing service..."
mkdir -p ~/.config/systemd/user
cat >~/.config/systemd/user/aria2cd.service <<EOF
[Unit]
Description=Aria2 Daemon
[Service]
Type=forking
ExecStart=/usr/bin/aria2c --conf-path=${conffileworking}
[Install]
WantedBy=default.target
EOF
systemctl --user enable aria2cd
}
status_service() {
echo "Current status:"
systemctl --no-pager --user status aria2cd
}
start_service() {
systemctl --user start aria2cd
}
restart_service() {
systemctl --user restart aria2cd
}
stop_service() {
systemctl --user stop aria2cd
}
try_exec() {
which $1 >/dev/null 2>&1
(($? == 0)) && $@
}
confirm_configration() {
! [[ -e $conffileworking ]] && cp $conffile $conffileworking
}
stop() {
stop_service
}
status() {
status_service
log
}
restart() {
install_service_if_not_exists
update_config
restart_service
status_service
}
start() {
install_service_if_not_exists
update_config
if is_running; then
echo "Service has been already started."
else
start_service
fi
status_service
}
log() {
if [[ "$log_path" == "-" ]]; then
journalctl --user -fu aria2cd.service
else
echo -e '\nLOG:'
tail $log_path $1
fi
}
# return 1: need update
internal_update_condition() {
if [[ ! -e $save_trackerlist_path ]]; then
return 0
else
last_modify=$(stat $save_trackerlist_path | grep -i Modify)
last_modify_stamp=$(date +%s -d "${last_modify#*:}")
now_ts=$(date +%s)
time_dist=$(($now_ts - $last_modify_stamp))
echo "It's been $time_dist sec since last update."
interval_second=$(($update_interval_hours * 3600))
if (($time_dist < $interval_second)); then
echo -e "Last updated at $last_modify.\nNo need to update."
else
return 0
fi
fi
return 1
}
process_raw_trackerlist() {
python -c "
import sys
urls = (_.strip() for _ in sys.stdin if _.strip())
result = ','.join(urls)
print(result)
"
}
update_bt_tracker() {
echo "Feching tracker-list from $trackerlist_url ..."
echo "[use proxychains: ${use_pc}]"
if [[ "${use_pc}" == "true" ]]; then
trackers=$(proxychains curl $trackerlist_url | process_raw_trackerlist)
else
trackers=$(curl $trackerlist_url | process_raw_trackerlist)
fi
if (($? == 0)) && [[ -n $trackers ]]; then
echo -e '\nGot new tracker-list:'
echo $trackers | tee $save_trackerlist_path
else
echo -e '\nFailed to get new tracker-list'
fi
}
rotate_log() {
if (($save_history)) && [[ -e $log_path ]]; then
mkdir -p $history_dir
mv -v $log_path "${history_dir}/until_$(date +%s).log"
fi
}
update_config() {
if [[ "$1" == "-f" ]] || [[ -z ${trackers} ]] || internal_update_condition; then
update_bt_tracker
fi
# if [[ "$1" == "-f" ]] || ! [[ -e ${conffileworking} ]] ; then
[[ -e ${conffileworking} ]] && mv -v $conffileworking ${conffileworking}.backup
cp $conffile $conffileworking
if [[ -n ${trackers} ]]; then
sed -i 's/^bt-tracker=/# \0/' $conffileworking
echo "bt-tracker=$trackers" >>$conffileworking
echo "Using trackers: $trackers"
fi
echo "Recreated locked configuration file."
# fi
}
update() {
update_config -f
restart
}
is_running() {
systemctl --user is-active aria2cd
}
open_browser() {
try_exec $browser 'http://aria2c.com/'
}
# main
# --------------------------------------------
# clear
try_exec figlet aria2
init_files
case $1 in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status ;;
update) update ;;
log) log -f ;;
web) open_browser ;;
*) help ;;
esac
echo -e $cut_off
echo -e '\nDONE :)'