more intelligent handling of ip whitelist check

This commit is contained in:
vyzo 2021-02-12 20:16:19 +02:00
parent 69868db8cf
commit 6c1addf493

View File

@ -27,6 +27,9 @@ type peerStats struct {
// IP tracking; store as string for easy processing // IP tracking; store as string for easy processing
ips []string ips []string
// IP whitelisting cache
ipWhitelist map[string]bool
// behavioural pattern penalties (applied by the router) // behavioural pattern penalties (applied by the router)
behaviourPenalty float64 behaviourPenalty float64
} }
@ -339,12 +342,26 @@ func (ps *peerScore) ipColocationFactor(p peer.ID) float64 {
loop: loop:
for _, ip := range pstats.ips { for _, ip := range pstats.ips {
if len(ps.params.IPColocationFactorWhitelist) > 0 { if len(ps.params.IPColocationFactorWhitelist) > 0 {
if pstats.ipWhitelist == nil {
pstats.ipWhitelist = make(map[string]bool)
}
whitelisted, ok := pstats.ipWhitelist[ip]
if !ok {
ipObj := net.ParseIP(ip) ipObj := net.ParseIP(ip)
for _, ipNet := range ps.params.IPColocationFactorWhitelist { for _, ipNet := range ps.params.IPColocationFactorWhitelist {
if ipNet.Contains(ipObj) { if ipNet.Contains(ipObj) {
pstats.ipWhitelist[ip] = true
continue loop continue loop
} }
} }
pstats.ipWhitelist[ip] = false
}
if whitelisted {
continue loop
}
} }
// P6 has a cliff (IPColocationFactorThreshold); it's only applied iff // P6 has a cliff (IPColocationFactorThreshold); it's only applied iff