-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtorrent.go
More file actions
257 lines (226 loc) · 8.47 KB
/
Copy pathtorrent.go
File metadata and controls
257 lines (226 loc) · 8.47 KB
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
package main
import (
"net"
"net/url"
"strings"
"sync"
"time"
)
type TorrentInfoStruct struct {
Size int64
Peers map[string]PeerInfoStruct
}
type PeerInfoStruct struct {
Net *net.IPNet
Port map[int]bool
Progress float64
Downloaded int64
Uploaded int64
ID string
Client string
}
// ExtractTrackerURI 从 Tracker URL 中提取路径和查询参数部分, 剥离 scheme 和域名.
// 用于避免域名中的长字符串被误匹配为 PT Token.
func ExtractTrackerURI(trackerURL string) string {
u, err := url.Parse(trackerURL)
if err != nil {
return trackerURL
}
uri := u.Path
if u.RawQuery != "" {
uri += "?" + u.RawQuery
}
return uri
}
var torrentMap = make(map[string]TorrentInfoStruct)
var lastTorrentMap = make(map[string]TorrentInfoStruct)
var torrentMapMutex sync.RWMutex
var lastTorrentMapMutex sync.RWMutex
var lastTorrentCleanTimestamp int64 = 0
// AddTorrentInfo 添加种子信息, 以便后续进行上传进度比分析.
func AddTorrentInfo(torrentInfoHash string, torrentTotalSize int64, cidr *net.IPNet, peerIP string, peerPort int, peerProgress float64, peerDownloaded int64, peerUploaded int64, peerID string, peerClient string) {
if !((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded || config.SyncServerURL != "") {
return
}
var peers map[string]PeerInfoStruct
var peerPortMap map[int]bool
torrentMapMutex.Lock()
if torrentInfo, exist := torrentMap[torrentInfoHash]; !exist {
peers = make(map[string]PeerInfoStruct)
peerPortMap = make(map[int]bool)
} else {
peers = torrentInfo.Peers
if peerInfo, exist := peers[peerIP]; !exist {
peerPortMap = make(map[int]bool)
} else {
peerPortMap = peerInfo.Port
}
}
peerPortMap[peerPort] = true
peers[peerIP] = PeerInfoStruct{Net: cidr, Port: peerPortMap, Progress: peerProgress, Downloaded: peerDownloaded, Uploaded: peerUploaded, ID: peerID, Client: peerClient}
torrentMap[torrentInfoHash] = TorrentInfoStruct{Size: torrentTotalSize, Peers: peers}
torrentMapMutex.Unlock()
}
// IsProgressNotMatchUploaded 判断 Peer 报告进度是否与已上传量不匹配.
func IsProgressNotMatchUploaded(torrentTotalSize int64, clientProgress float64, clientUploaded int64) bool {
if config.BanByProgressUploaded && torrentTotalSize > 0 && clientProgress >= 0 && clientUploaded > 0 {
startUploaded := (float64(torrentTotalSize) * (config.BanByPUStartPercent / 100))
peerReportDownloaded := (float64(torrentTotalSize) * clientProgress)
if (clientUploaded/1024/1024) >= int64(config.BanByPUStartMB) && float64(clientUploaded) >= startUploaded && (peerReportDownloaded*config.BanByPUAntiErrorRatio) < float64(clientUploaded) {
return true
}
}
return false
}
// IsProgressNotMatchUploaded_Relative 判断 Peer 在两个周期之间的相对上传进度是否不匹配.
func IsProgressNotMatchUploaded_Relative(torrentTotalSize int64, peerInfo PeerInfoStruct, lastPeerInfo PeerInfoStruct) int64 {
var relativeUploaded int64 = 0
if peerInfo.Uploaded < lastPeerInfo.Uploaded {
relativeUploaded = peerInfo.Uploaded
} else {
relativeUploaded = (peerInfo.Uploaded - lastPeerInfo.Uploaded)
}
if torrentTotalSize > 0 && peerInfo.Uploaded > 0 && (float64(relativeUploaded)/1024/1024) > float64(config.BanByRelativePUStartMB) {
var relativeUploadedPercent float64 = 0
if peerInfo.Uploaded > 0 {
if peerInfo.Uploaded < lastPeerInfo.Uploaded {
relativeUploadedPercent = 1
} else {
relativeUploadedPercent = (1 - (float64(lastPeerInfo.Uploaded) / float64(peerInfo.Uploaded)))
}
}
if relativeUploadedPercent > (config.BanByRelativePUStartPercent / 100) {
var peerReportProgress float64 = 0
if peerInfo.Progress > 0 {
if peerInfo.Progress < lastPeerInfo.Progress {
peerReportProgress = 1
} else {
peerReportProgress = (1 - (lastPeerInfo.Progress / peerInfo.Progress))
}
}
if relativeUploadedPercent > (peerReportProgress * config.BanByRelativePUAntiErrorRatio) {
return relativeUploaded
}
}
}
return 0
}
// CheckAllTorrent 对所有种子和 Peer 进行分析.
func CheckAllTorrent(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map[string]TorrentInfoStruct) (int, int) {
if ((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded || config.BTNSubmitHistories) && (currentTimestamp > (lastTorrentCleanTimestamp + int64(config.TorrentMapCleanInterval))) {
blockCount := 0
ipBlockCount := 0
torrentMapMutex.Lock()
lastTorrentMapMutex.Lock()
defer torrentMapMutex.Unlock()
defer lastTorrentMapMutex.Unlock()
for torrentInfoHash, torrentInfo := range torrentMap {
for peerIP, peerInfo := range torrentInfo.Peers {
lastTorrentInfo, exist := lastTorrentMap[torrentInfoHash]
if exist {
if lastPeerInfo, exist := lastTorrentInfo.Peers[peerIP]; exist {
if lastPeerInfo.Uploaded == peerInfo.Uploaded {
continue
}
}
}
if IsBlockedPeer(peerIP, -1, true) {
continue
}
if config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0 {
if float64(peerInfo.Uploaded) > (float64(torrentInfo.Size) * peerInfo.Progress * config.IPUpCheckPerTorrentRatio) {
Log("CheckAllTorrent_AddBlockPeer (Torrent-Too high uploaded)", "%s (Uploaded: %.2f MB)", true, peerIP, (float64(peerInfo.Uploaded) / 1024 / 1024))
ipBlockCount++
AddBlockPeer("CheckAllTorrent", "Torrent-Too high uploaded", peerIP, -1, torrentInfoHash, peerInfo.ID, peerInfo.Client, 0, peerInfo.Uploaded)
AddBlockCIDR(peerIP, peerInfo.Net)
continue
}
}
if config.BanByRelativeProgressUploaded {
if lastPeerInfo, exist := lastTorrentMap[torrentInfoHash].Peers[peerIP]; exist {
if uploadDuring := IsProgressNotMatchUploaded_Relative(torrentInfo.Size, peerInfo, lastPeerInfo); uploadDuring > 0 {
for port := range peerInfo.Port {
if IsBlockedPeer(peerIP, port, true) {
continue
}
Log("CheckAllTorrent_AddBlockPeer (Bad-Relative_Progress_Uploaded)", "%s:%d (UploadDuring: %.2f MB)", true, peerIP, port, uploadDuring)
blockCount++
AddBlockPeer("CheckAllTorrent", "Bad-Relative_Progress_Uploaded", peerIP, port, torrentInfoHash, peerInfo.ID, peerInfo.Client, 0, peerInfo.Uploaded)
AddBlockCIDR(peerIP, peerInfo.Net)
}
continue
}
}
}
}
}
lastTorrentCleanTimestamp = currentTimestamp
DeepCopyTorrentMap(torrentMap, lastTorrentMap)
return blockCount, ipBlockCount
}
return 0, 0
}
// CheckTorrent 检查单个种子的状态.
func CheckTorrent(torrent *Torrent) (int, []*Peer) {
if torrent.Hash == "" {
return -1, nil
}
if config.IgnorePTTorrent && torrent.Tracker != "" {
if torrent.Tracker == "Private" {
return -4, nil
}
lowerTorrentTracker := strings.ToLower(torrent.Tracker)
if strings.Contains(lowerTorrentTracker, "?passkey=") || strings.Contains(lowerTorrentTracker, "?authkey=") || strings.Contains(lowerTorrentTracker, "?secure=") {
return -4, nil
}
if config.IgnorePTTorrentByRandomStr {
trackerURI := ExtractTrackerURI(lowerTorrentTracker)
randomStrMatched, err := randomStrRegexp.MatchString(trackerURI)
if err != nil {
Log("CheckTorrent_MatchTracker", GetLangText("Error-MatchRegexpErr"), true, err.Error())
} else if randomStrMatched {
return -4, nil
}
}
}
if config.IgnoreNoLeechersTorrent && torrent.LeechCount <= 0 {
return -2, nil
}
if torrent.Peers != nil {
return 0, torrent.Peers
}
peers, err := FetchTorrentPeers(torrent)
if err != nil || peers == nil {
return -3, nil
}
return 0, peers
}
// ProcessTorrent 处理单个种子的 Peer 分析任务.
func ProcessTorrent(torrent *Torrent, emptyHashCount *int, noLeechersCount *int, badTorrentInfoCount *int, ptTorrentCount *int, blockCount *int, ipBlockCount *int, badPeersCount *int, emptyPeersCount *int) {
torrent.Hash = strings.ToLower(torrent.Hash)
torrentStatus, peers := CheckTorrent(torrent)
if config.Debug_CheckTorrent {
Log("Debug-CheckTorrent", "%s (Status: %d)", false, torrent.Hash, torrentStatus)
}
skipSleep := false
switch torrentStatus {
case -1:
skipSleep = true
*emptyHashCount++
case -2:
skipSleep = true
*noLeechersCount++
case -3:
*badTorrentInfoCount++
case -4:
skipSleep = true
*ptTorrentCount++
case 0:
for _, peer := range peers {
ProcessPeer(peer, torrent.Hash, torrent.TotalSize, blockCount, ipBlockCount, badPeersCount, emptyPeersCount)
}
}
if !skipSleep && config.SleepTime != 0 {
time.Sleep(time.Duration(config.SleepTime) * time.Millisecond)
}
}