-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtorrent.go
229 lines (205 loc) · 9.1 KB
/
torrent.go
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
package main
import (
"net"
"strings"
"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
}
var torrentMap = make(map[string]TorrentInfoStruct)
var lastTorrentMap = make(map[string]TorrentInfoStruct)
var lastTorrentCleanTimestamp int64 = 0
func AddTorrentInfo(torrentInfoHash string, torrentTotalSize int64, cidr *net.IPNet, peerIP string, peerPort int, peerProgress float64, peerUploaded int64) {
if !((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded || config.SyncServerURL != "") {
return
}
var peers map[string]PeerInfoStruct
var peerPortMap map[int]bool
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
// 防止 Peer 在周期内以重新连接的方式清空实际上传量.
if peerInfo.Uploaded > peerUploaded {
peerUploaded += peerInfo.Uploaded
}
}
}
peerPortMap[peerPort] = true
peers[peerIP] = PeerInfoStruct{Net: cidr, Port: peerPortMap, Progress: peerProgress, Uploaded: peerUploaded}
torrentMap[torrentInfoHash] = TorrentInfoStruct{Size: torrentTotalSize, Peers: peers}
}
func IsProgressNotMatchUploaded(torrentTotalSize int64, clientProgress float64, clientUploaded int64) bool {
if config.BanByProgressUploaded && torrentTotalSize > 0 && clientProgress >= 0 && clientUploaded > 0 {
/*
条件 1. 若客户端对 Peer 上传已大于等于 Torrnet 大小的 2%;
条件 2. 但 Peer 报告进度乘以下载量再乘以一定防误判倍率, 却比客户端上传量还小;
若满足以上条件, 则认为 Peer 是有问题的.
e.g.:
若 torrentTotalSize: 100GB, clientProgress: 1% (0.01), clientUploaded: 6GB, config.BanByPUStartPrecent: 2 (0.02), config.BanByPUAntiErrorRatio: 5;
判断条件 1:
torrentTotalSize * config.BanByPUStartPrecent = 100GB * 0.02 = 2GB, clientUploaded = 6GB >= 2GB
满足此条件;
判断条件 2:
torrentTotalSize * clientProgress * config.BanByPUAntiErrorRatio = 100GB * 0.01 * 5 = 5GB, 5GB < clientUploaded = 6GB
满足此条件;
则该 Peer 将被封禁, 由于其报告进度为 1%, 算入 config.BanByPUAntiErrorRatio 滞后防误判倍率后为 5% (5GB), 但客户端实际却已上传 6GB.
*/
startUploaded := (float64(torrentTotalSize) * (config.BanByPUStartPrecent / 100))
peerReportDownloaded := (float64(torrentTotalSize) * clientProgress)
if (clientUploaded/1024/1024) >= int64(config.BanByPUStartMB) && float64(clientUploaded) >= startUploaded && (peerReportDownloaded*config.BanByPUAntiErrorRatio) < float64(clientUploaded) {
return true
}
}
return false
}
func IsProgressNotMatchUploaded_Relative(torrentTotalSize int64, peerInfo PeerInfoStruct, lastPeerInfo PeerInfoStruct) int64 {
// 若客户端对 Peer 上传已大于 0, 且相对上传量大于起始上传量, 则继续判断.
var relativeUploaded int64 = (peerInfo.Uploaded - lastPeerInfo.Uploaded)
if torrentTotalSize > 0 && peerInfo.Uploaded > 0 && (float64(relativeUploaded)/1024/1024) > float64(config.BanByRelativePUStartMB) {
relativeUploadedPrecent := (1 - (float64(lastPeerInfo.Uploaded) / float64(peerInfo.Uploaded)))
// 若相对上传百分比大于起始百分比, 则继续判断.
if relativeUploadedPrecent > (config.BanByRelativePUStartPrecent / 100) {
// 若相对上传百分比大于 Peer 报告进度乘以一定防误判倍率, 则认为 Peer 是有问题的.
var peerReportProgress float64 = 0
if peerInfo.Progress > 0 {
peerReportProgress = (1 - (lastPeerInfo.Progress / peerInfo.Progress))
}
if relativeUploadedPrecent > (peerReportProgress * config.BanByRelativePUAntiErrorRatio) {
return relativeUploaded
}
}
}
return 0
}
func CheckAllTorrent(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map[string]TorrentInfoStruct) (int, int) {
if ((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded) && len(lastTorrentMap) > 0 && currentTimestamp > (lastTorrentCleanTimestamp+int64(config.TorrentMapCleanInterval)) {
blockCount := 0
ipBlockCount := 0
for torrentInfoHash, torrentInfo := range torrentMap {
for peerIP, peerInfo := range torrentInfo.Peers {
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)
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)
AddBlockCIDR(peerIP, peerInfo.Net)
}
continue
}
}
}
}
}
lastTorrentCleanTimestamp = currentTimestamp
DeepCopyTorrentMap(torrentMap, lastTorrentMap)
return blockCount, ipBlockCount
}
return 0, 0
}
func CheckTorrent(torrentInfoHash string, torrentTracker string, torrentLeecherCount int64, torrentPeers interface{}) (int, interface{}) {
if torrentInfoHash == "" {
return -1, nil
}
if config.IgnorePTTorrent && torrentTracker != "" {
if torrentTracker == "Private" {
return -4, nil
}
lowerTorrentTracker := strings.ToLower(torrentTracker)
if strings.Contains(lowerTorrentTracker, "?passkey=") || strings.Contains(lowerTorrentTracker, "?authkey=") || strings.Contains(lowerTorrentTracker, "?secure=") {
return -4, nil
}
randomStrMatched, err := randomStrRegexp.MatchString(lowerTorrentTracker)
if err != nil {
Log("CheckTorrent_MatchTracker", GetLangText("Error-MatchRegexpErr"), true, err.Error())
} else if randomStrMatched {
return -4, nil
}
}
if config.IgnoreNoLeechersTorrent && torrentLeecherCount <= 0 {
return -2, nil
}
if torrentPeers != nil {
return 0, torrentPeers
}
torrentPeers = FetchTorrentPeers(torrentInfoHash)
if torrentPeers == nil {
return -3, nil
}
return 0, torrentPeers
}
func ProcessTorrent(torrentInfoHash string, torrentTracker string, torrentLeecherCount int64, torrentTotalSize int64, torrentPeers interface{}, emptyHashCount *int, noLeechersCount *int, badTorrentInfoCount *int, ptTorrentCount *int, blockCount *int, ipBlockCount *int, badPeersCount *int, emptyPeersCount *int) {
torrentInfoHash = strings.ToLower(torrentInfoHash)
torrentStatus, torrentPeersStruct := CheckTorrent(torrentInfoHash, torrentTracker, torrentLeecherCount, torrentPeers)
if config.Debug_CheckTorrent {
Log("Debug-CheckTorrent", "%s (Status: %d)", false, torrentInfoHash, 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:
switch currentClientType {
case "qBittorrent":
torrentPeers := torrentPeersStruct.(*qB_TorrentPeersStruct).Peers
for _, peer := range torrentPeers {
ProcessPeer(peer.IP, peer.Port, peer.PeerID, peer.Client, peer.DlSpeed, peer.UpSpeed, peer.Progress, peer.Downloaded, peer.Uploaded, torrentInfoHash, torrentTotalSize, blockCount, ipBlockCount, badPeersCount, emptyPeersCount)
}
case "Transmission":
torrentPeers := torrentPeersStruct.([]Tr_PeerStruct)
for _, peer := range torrentPeers {
// Transmission 目前似乎并不提供 Peer 的 PeerID 及 Downloaded/Uploaded, 因此使用无效值取代.
ProcessPeer(peer.IP, peer.Port, "", peer.Client, peer.DlSpeed, peer.UpSpeed, peer.Progress, -1, -1, torrentInfoHash, torrentTotalSize, blockCount, ipBlockCount, badPeersCount, emptyPeersCount)
}
case "BitComet":
torrentPeers := torrentPeersStruct.(*[]BC_PeerStruct)
for _, peer := range *torrentPeers {
// BitComet 目前不为其支持 PeerID, 因此使用无效值取代.
ProcessPeer(peer.IP, peer.Port, "", peer.Client, peer.DlSpeed, peer.UpSpeed, peer.Progress, peer.Downloaded, peer.Uploaded, torrentInfoHash, torrentTotalSize, blockCount, ipBlockCount, badPeersCount, emptyPeersCount)
}
}
}
if !skipSleep && config.SleepTime != 0 {
time.Sleep(time.Duration(config.SleepTime) * time.Millisecond)
}
}