-
Notifications
You must be signed in to change notification settings - Fork 737
/
tracker.sh
executable file
·154 lines (140 loc) · 5.38 KB
/
tracker.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
#!/usr/bin/env bash
#
# https://github.com/P3TERX/aria2.conf
# File name:tracker.sh
# Description: Get BT trackers and add to Aria2
# Version: 3.1
#
# Copyright (c) 2018-2021 P3TERX <https://p3terx.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# BT tracker is provided by the following project.
# https://github.com/XIU2/TrackersListCollection
#
RED_FONT_PREFIX="\033[31m"
GREEN_FONT_PREFIX="\033[32m"
YELLOW_FONT_PREFIX="\033[1;33m"
LIGHT_PURPLE_FONT_PREFIX="\033[1;35m"
FONT_COLOR_SUFFIX="\033[0m"
INFO="[${GREEN_FONT_PREFIX}INFO${FONT_COLOR_SUFFIX}]"
ERROR="[${RED_FONT_PREFIX}ERROR${FONT_COLOR_SUFFIX}]"
ARIA2_CONF=${1:-aria2.conf}
DOWNLOADER="curl -fsSL --connect-timeout 3 --max-time 3 --retry 2"
NL=$'\n'
DATE_TIME() {
date +"%m/%d %H:%M:%S"
}
GET_TRACKERS() {
if [[ -z "${CUSTOM_TRACKER_URL}" ]]; then
echo && echo -e "$(DATE_TIME) ${INFO} Get BT trackers..."
TRACKER=$(
${DOWNLOADER} https://trackerslist.com/all_aria2.txt ||
${DOWNLOADER} https://cdn.statically.io/gh/XIU2/TrackersListCollection/master/all_aria2.txt ||
${DOWNLOADER} https://trackers.p3terx.com/all_aria2.txt
)
else
echo && echo -e "$(DATE_TIME) ${INFO} Get BT trackers from url(s):${CUSTOM_TRACKER_URL} ..."
URLS=$(echo ${CUSTOM_TRACKER_URL} | tr "," "$NL")
for URL in $URLS; do
TRACKER+="$(${DOWNLOADER} ${URL} | tr "," "\n")$NL"
done
TRACKER="$(echo "$TRACKER" | awk NF | sort -u | sed 'H;1h;$!d;x;y/\n/,/' )"
fi
[[ -z "${TRACKER}" ]] && {
echo
echo -e "$(DATE_TIME) ${ERROR} Unable to get trackers, network failure or invalid links." && exit 1
}
}
ECHO_TRACKERS() {
echo -e "
--------------------[BitTorrent Trackers]--------------------
${TRACKER}
--------------------[BitTorrent Trackers]--------------------
"
}
ADD_TRACKERS() {
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to Aria2 configuration file ${LIGHT_PURPLE_FONT_PREFIX}${ARIA2_CONF}${FONT_COLOR_SUFFIX} ..." && echo
if [ ! -f ${ARIA2_CONF} ]; then
echo -e "$(DATE_TIME) ${ERROR} '${ARIA2_CONF}' does not exist."
exit 1
else
[ -z $(grep "bt-tracker=" ${ARIA2_CONF}) ] && echo "bt-tracker=" >>${ARIA2_CONF}
sed -i "s@^\(bt-tracker=\).*@\1${TRACKER}@" ${ARIA2_CONF} && echo -e "$(DATE_TIME) ${INFO} BT trackers successfully added to Aria2 configuration file !"
fi
}
ADD_TRACKERS_RPC() {
if [[ "${RPC_SECRET}" ]]; then
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.changeGlobalOption","id":"P3TERX","params":["token:'${RPC_SECRET}'",{"bt-tracker":"'${TRACKER}'"}]}'
else
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.changeGlobalOption","id":"P3TERX","params":[{"bt-tracker":"'${TRACKER}'"}]}'
fi
curl "${RPC_ADDRESS}" -fsSd "${RPC_PAYLOAD}" || curl "https://${RPC_ADDRESS}" -kfsSd "${RPC_PAYLOAD}"
}
ADD_TRACKERS_RPC_STATUS() {
RPC_RESULT=$(ADD_TRACKERS_RPC)
[[ $(echo ${RPC_RESULT} | grep OK) ]] &&
echo -e "$(DATE_TIME) ${INFO} BT trackers successfully added to Aria2 !" ||
echo -e "$(DATE_TIME) ${ERROR} Network failure or Aria2 RPC interface error!"
}
ADD_TRACKERS_REMOTE_RPC() {
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to remote Aria2: ${LIGHT_PURPLE_FONT_PREFIX}${RPC_ADDRESS%/*}${FONT_COLOR_SUFFIX} ..." && echo
ADD_TRACKERS_RPC_STATUS
}
ADD_TRACKERS_LOCAL_RPC() {
if [ ! -f ${ARIA2_CONF} ]; then
echo -e "$(DATE_TIME) ${ERROR} '${ARIA2_CONF}' does not exist."
exit 1
else
RPC_PORT=$(grep ^rpc-listen-port ${ARIA2_CONF} | cut -d= -f2-)
RPC_SECRET=$(grep ^rpc-secret ${ARIA2_CONF} | cut -d= -f2-)
[[ ${RPC_PORT} ]] || {
echo -e "$(DATE_TIME) ${ERROR} Aria2 configuration file incomplete."
exit 1
}
RPC_ADDRESS="localhost:${RPC_PORT}/jsonrpc"
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to Aria2 ..." && echo
ADD_TRACKERS_RPC_STATUS
fi
}
[ $(command -v curl) ] || {
echo -e "$(DATE_TIME) ${ERROR} curl is not installed."
exit 1
}
if [ "$1" = "cat" ]; then
GET_TRACKERS
ECHO_TRACKERS
elif [ "$1" = "RPC" ]; then
RPC_ADDRESS="$2/jsonrpc"
RPC_SECRET="$3"
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS_REMOTE_RPC
elif [ "$2" = "RPC" ]; then
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS
echo
ADD_TRACKERS_LOCAL_RPC
else
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS
fi
exit 0