Skip to content

Commit

Permalink
more intelligent handling of ip whitelist check
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Feb 12, 2021
1 parent 69868db commit 6c1addf
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 6c1addf

Please sign in to comment.