-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathip.go
More file actions
145 lines (125 loc) · 4.17 KB
/
Copy pathip.go
File metadata and controls
145 lines (125 loc) · 4.17 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
package main
import (
"net"
"sync"
)
type IPInfoStruct struct {
Net *net.IPNet
Port map[int]bool
TorrentDownloaded map[string]int64
TorrentUploaded map[string]int64
}
var ipMap = make(map[string]IPInfoStruct)
var lastIPMap = make(map[string]IPInfoStruct)
var ipMapMutex sync.RWMutex
var lastIPMapMutex sync.RWMutex
var lastIPCleanTimestamp int64 = 0
func AddIPInfo(cidr *net.IPNet, peerIP string, peerPort int, torrentInfoHash string, peerDownloaded int64, peerUploaded int64) {
if !(config.MaxIPPortCount > 0 || (config.IPUploadedCheck && config.IPUpCheckIncrementMB > 0)) {
return
}
var clientPortMap map[int]bool
var clientTorrentDownloadedMap map[string]int64
var clientTorrentUploadedMap map[string]int64
ipMapMutex.Lock()
if info, exist := ipMap[peerIP]; !exist {
clientPortMap = make(map[int]bool)
clientTorrentDownloadedMap = make(map[string]int64)
clientTorrentUploadedMap = make(map[string]int64)
} else {
clientPortMap = info.Port
clientTorrentDownloadedMap = info.TorrentDownloaded
clientTorrentUploadedMap = info.TorrentUploaded
if clientTorrentDownloadedMap == nil {
clientTorrentDownloadedMap = make(map[string]int64)
}
}
clientPortMap[peerPort] = true
clientTorrentDownloadedMap[torrentInfoHash] = peerDownloaded
clientTorrentUploadedMap[torrentInfoHash] = peerUploaded
ipMap[peerIP] = IPInfoStruct{Net: cidr, Port: clientPortMap, TorrentDownloaded: clientTorrentDownloadedMap, TorrentUploaded: clientTorrentUploadedMap}
ipMapMutex.Unlock()
}
func IsIPTooHighUploaded(ipInfo IPInfoStruct, lastIPInfo IPInfoStruct) int64 {
var totalUploaded int64 = 0
for torrentInfoHash, torrentUploaded := range ipInfo.TorrentUploaded {
if config.IPUpCheckIncrementMB > 0 {
if lastTorrentUploaded, exist := lastIPInfo.TorrentUploaded[torrentInfoHash]; !exist {
totalUploaded += torrentUploaded
} else {
if torrentUploaded < lastTorrentUploaded {
totalUploaded += torrentUploaded
} else {
totalUploaded += (torrentUploaded - lastTorrentUploaded)
}
}
}
}
if config.IPUpCheckIncrementMB > 0 {
var totalUploadedMB int64 = (totalUploaded / 1024 / 1024)
if totalUploadedMB > int64(config.IPUpCheckIncrementMB) {
return totalUploadedMB
}
}
return 0
}
func IsMatchCIDR(peerNet *net.IPNet) bool {
if peerNet != nil {
blockCIDRMapMutex.RLock()
_, exist := blockCIDRMap[peerNet.String()]
blockCIDRMapMutex.RUnlock()
if exist {
return true
}
}
return false
}
func CheckAllIP(ipMap map[string]IPInfoStruct, lastIPMap map[string]IPInfoStruct) int {
if (config.MaxIPPortCount > 0 || (config.IPUploadedCheck && config.IPUpCheckIncrementMB > 0)) && len(lastIPMap) > 0 && currentTimestamp > (lastIPCleanTimestamp+int64(config.IPUpCheckInterval)) {
ipBlockCount := 0
ipMapMutex.Lock()
lastIPMapMutex.Lock()
defer ipMapMutex.Unlock()
defer lastIPMapMutex.Unlock()
ipMapLoop:
for ip, ipInfo := range ipMap {
if IsBlockedPeer(ip, -1, true) || len(ipInfo.Port) <= 0 {
continue
}
for port := range ipInfo.Port {
if IsBlockedPeer(ip, port, true) {
continue ipMapLoop
}
}
if config.MaxIPPortCount > 0 {
if len(ipInfo.Port) > int(config.MaxIPPortCount) {
Log("CheckAllIP_AddBlockPeer (Too many ports)", "%s:%d", true, ip, -1)
ipBlockCount++
AddBlockPeer("CheckAllIP", "Too many ports", ip, -1, "", "", "", 0, 0)
AddBlockCIDR(ip, ipInfo.Net)
continue
}
}
if lastIPInfo, exist := lastIPMap[ip]; exist {
if uploadDuring := IsIPTooHighUploaded(ipInfo, lastIPInfo); uploadDuring > 0 {
Log("CheckAllIP_AddBlockPeer (Global-Too high uploaded)", "%s:%d (UploadDuring: %.2f MB)", true, ip, -1, uploadDuring)
ipBlockCount++
var totalDownloaded int64 = 0
var totalUploaded int64 = 0
for _, v := range ipInfo.TorrentDownloaded {
totalDownloaded += v
}
for _, v := range ipInfo.TorrentUploaded {
totalUploaded += v
}
AddBlockPeer("CheckAllIP", "Global-Too high uploaded", ip, -1, "", "", "", totalDownloaded, totalUploaded)
AddBlockCIDR(ip, ipInfo.Net)
}
}
}
lastIPCleanTimestamp = currentTimestamp
DeepCopyIPMap(ipMap, lastIPMap)
return ipBlockCount
}
return 0
}