From 6c1addf493ebb942548dbe027fd1fef2e1f8391d Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 12 Feb 2021 20:16:19 +0200 Subject: [PATCH] more intelligent handling of ip whitelist check --- score.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/score.go b/score.go index 8fdb66d9..75f9e27f 100644 --- a/score.go +++ b/score.go @@ -27,6 +27,9 @@ type peerStats struct { // IP tracking; store as string for easy processing ips []string + // IP whitelisting cache + ipWhitelist map[string]bool + // behavioural pattern penalties (applied by the router) behaviourPenalty float64 } @@ -339,11 +342,25 @@ func (ps *peerScore) ipColocationFactor(p peer.ID) float64 { loop: for _, ip := range pstats.ips { if len(ps.params.IPColocationFactorWhitelist) > 0 { - ipObj := net.ParseIP(ip) - for _, ipNet := range ps.params.IPColocationFactorWhitelist { - if ipNet.Contains(ipObj) { - continue loop + if pstats.ipWhitelist == nil { + pstats.ipWhitelist = make(map[string]bool) + } + + whitelisted, ok := pstats.ipWhitelist[ip] + if !ok { + ipObj := net.ParseIP(ip) + for _, ipNet := range ps.params.IPColocationFactorWhitelist { + if ipNet.Contains(ipObj) { + pstats.ipWhitelist[ip] = true + continue loop + } } + + pstats.ipWhitelist[ip] = false + } + + if whitelisted { + continue loop } }