forked from linux4ever07/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_list.sh
executable file
·127 lines (98 loc) · 2.92 KB
/
tracker_list.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
#!/bin/bash
# This script parses a BitTorrent tracker list text file, sorts, removes
# duplicates, checks online status of each URL, and prints the list to
# STDOUT in the correct format.
# Only the trackers that are still online will be printed. This is
# useful to clean up old lists of public trackers that can be found
# online, as an example. Though, it might be a good idea to run the
# script a couple of times, waiting a few hours or days inbetween, since
# a tracker could be only temporarily offline.
# Any tracker URL using protocols besides HTTP, HTTPS and UDP will be
# ignored / skipped by this script, when checking online status.
# The second argument to the script (-nocheck), is optional. If used,
# the online status of trackers will not be checked, but the list will
# only get sorted and rid of duplicates.
# If you want to save the list in a text file, you can just do a
# redirection like so:
# tracker_list.sh 'trackers.txt' | tee 'trackers_checked.txt'
usage () {
printf '\n%s\n\n' "Usage: $(basename "$0") [tracker txt] [-nocheck]"
exit
}
if [[ ! -f $1 ]]; then
usage
elif [[ -n $2 && $2 != '-nocheck' ]]; then
usage
fi
nocheck=0
if [[ $2 == '-nocheck' ]]; then
nocheck=1
fi
if=$(readlink -f "$1")
switch=0
regex1='^([[:alpha:]]+)://'
regex2=':([0-9]+)'
regex3='/.*$'
regex4='/announce(\.[^.]*){0,1}$'
regex5='/$'
declare -a trackers
mapfile -t lines < <(tr -d '\r' <"$if" | tr -d '[:blank:]' | tr '[:upper:]' '[:lower:]' | sort --unique)
for (( i = 0; i < ${#lines[@]}; i++ )); do
line="${lines[${i}]}"
switch=0
if [[ -n $line ]]; then
for (( j = 0; j < ${#trackers[@]}; j++ )); do
line_tmp=$(sed -E -e "s_${regex4}__" -e "s_${regex5}__" <<<"$line")
if [[ ${trackers[${j}]} =~ $line_tmp ]]; then
switch=1
array_l="${#trackers[${j}]}"
line_l="${#line}"
if [[ $line_l > $array_l && $line =~ $regex4 ]]; then
trackers["${j}"]="$line"
fi
fi
done
if [[ $switch -eq 0 ]]; then
trackers+=("$line")
fi
fi
done
declare -A md5h
for (( i = 0; i < ${#trackers[@]}; i++ )); do
tracker="${trackers[${i}]}"
md5=$(md5sum -b <<<"$tracker")
md5="${md5%% *}"
if [[ ${md5h[${md5}]} -eq 1 ]]; then
continue
else
md5h["${md5}"]=1
fi
if [[ $nocheck -eq 1 ]]; then
printf '%s\n\n' "$tracker"
continue
fi
address=$(sed -E -e "s_${regex1}__" -e "s_${regex2}__" -e "s_${regex3}__" <<<"$tracker")
protocol=$(grep -Eo "$regex1" <<<"$tracker" | sed -E "s_${regex1}_\1_")
port=$(grep -Eo "$regex2" <<<"$tracker" | sed -E "s_${regex2}_\1_")
case $protocol in
http*)
curl --retry 10 --retry-delay 10 --connect-timeout 10 --silent --output /dev/null "$tracker"
;;
udp)
for n in {1..10}; do
timeout 10 nc --udp -z "$address" "$port" 1>&- 2>&-
done
;;
*)
continue
;;
esac
if [[ $? -ne 0 ]]; then
ping -c 10 -W 10 "$address" &>/dev/null
if [[ $? -eq 0 ]]; then
printf '%s\n\n' "$tracker"
fi
elif [[ $? -eq 0 ]]; then
printf '%s\n\n' "$tracker"
fi
done