-
Notifications
You must be signed in to change notification settings - Fork 1
/
trans-block.sh
executable file
·130 lines (111 loc) · 3.22 KB
/
trans-block.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
#!/usr/bin/env sh
HOST="localhost:9091"
AUTH="username:password"
CLIENTS="xunlei thunder gt[[:digit:]]\{4\} xl0012 xf dandanplay dl3760 qq libtorrent"
LIST="$HOME/.config/transmission-daemon/blocklists/leechers.txt"
BIN="$LIST.bin"
# Clear blocklist
# 0=disable
# TIMEOUT_SECONDS=$((60 * 60 * 24)) # 24 hours
TIMEOUT_SECONDS=0
# Restart related torrents immediately if leechers detected
RESTART_TORRENT=true
error() { echo "$@" >&2; }
HASH_SHORT=
error_with_hash_tag() { error "[$HASH_SHORT]" "$@"; }
pattern=$(echo "$CLIENTS" | xargs -0 | sed 's/ /\\)\\|\\(/g')
pattern="\(\($pattern\)\)"
trans_reload() {
error "Reloading"
# reload: https://github.com/transmission/transmission/blob/main/docs/Editing-Configuration-Files.md#reload-settings
killall -HUP transmission-daemon
}
block_leechers() {
peers=$(transmission-remote "$HOST" --auth "$AUTH" --torrent "$1" --info-peers)
leechers=$(echo "$peers" | grep -i "$pattern")
result=1
while IFS= read -r leecher; do
[ -z "$leecher" ] && continue
# https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format
client=$(echo "$leecher" | awk '{ print $6 }')
client=$(echo "$leecher" | grep -o -- "$client.*$")
ip=$(echo "$leecher" | awk '{ print $1 }')
line="$client:$ip-$ip"
error_with_hash_tag "Blocking leecher"
if grep -qs -- "$line" "$LIST"; then
error_with_hash_tag "Duplicate: $line"
else
echo "$line"
echo "$line" >>"$LIST"
fi
result=0
done <<EOF
$leechers
EOF
return $result
}
trans_restart_torrent() {
error_with_hash_tag "Restarting"
retry_max=5
for _ in $(seq 0 "$retry_max"); do
if transmission-remote "$HOST" --auth "$AUTH" --torrent "$1" --stop | grep -qs success; then
break
fi
sleep 1
done
stopped=false
for _ in $(seq 0 "$retry_max"); do
if transmission-remote "$HOST" --auth "$AUTH" --torrent "$1" --info | grep -qs 'State: Stopped'; then
error_with_hash_tag "Stopped"
stopped=true
break
fi
sleep 1
done
if [ "$stopped" = false ]; then
error_with_hash_tag "Unable to stop, skipping"
return 1
fi
for _ in $(seq 0 "$retry_max"); do
if transmission-remote "$HOST" --auth "$AUTH" --torrent "$1" --start | grep -qs success; then
break
fi
sleep 1
done
for _ in $(seq 0 "$retry_max"); do
if transmission-remote "$HOST" --auth "$AUTH" --torrent "$1" --info | grep -qs 'State: Stopped'; then
sleep 1
continue
fi
error_with_hash_tag "Started"
stopped=false
break
done
if [ "$stopped" = true ]; then
error_with_hash_tag "Unable to start"
error_with_hash_tag "You may have to start manually"
return 1
fi
}
start=$(date +%s)
while true; do
diff=$(($(date +%s) - start))
if [ $TIMEOUT_SECONDS -ne 0 ] && [ $diff -ge $TIMEOUT_SECONDS ]; then
error "Clearing blocklist"
rm -f "$LIST"
rm -f "$BIN"
start=$(date +%s)
trans_reload
fi
hashes=$(transmission-remote "$HOST" --auth "$AUTH" --torrent all --info | grep Hash | awk '{ print $2 }')
for h in $hashes; do
HASH_SHORT="$(echo "$h" | cut -c -8)"
if block_leechers "$h"; then
trans_reload
if [ $RESTART_TORRENT = true ]; then
trans_restart_torrent "$h"
fi
fi
done
sleep 30
done