java.lang.Object
net.i2p.router.networkdb.kademlia.LookupBanHammer
Tracks lookup frequency per (peer, tunnel) pair to provide DOS protection via temporary bans on excessive requesters. Uses burst detection over a 1-second window and sustained-rate threshold over a 30-second sliding window. Maps are size-bounded to prevent memory exhaustion.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classPeriodic cleanup: remove expired bans, prune idle burst trackers, size-bound.private static class(peer, tunnel) key with cached hash.Field Summary
FieldsModifier and TypeFieldDescriptionprivate static intLookups per window counted as a burst.private final LookupBanHammer.CleanerTimer cleaning the counters and bans.(package private) static longInterval between cleanup passes in milliseconds.private static intMost counted tunnels held at once.private static final longTime a banned peer stays banned.private final ConcurrentHashMap<LookupBanHammer.ReplyTunnel, Long> The banned reply tunnels, by expiry time.private static final longLength of the lookup counting window.private final ConcurrentHashMap<LookupBanHammer.ReplyTunnel, ConcurrentLinkedDeque<Long>> The lookup counts, by reply tunnel.private static final longInterval of the counter cleanup.private static final TunnelIdTunnel id standing in for an unknown id.private static final intLookups per window before a peer is banned.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static intburstCount(ConcurrentLinkedDeque<Long> deque, long now) Count entries in the last 1-second window (newest-first iteration).(package private) voidcancel()Stop the periodic cleanup.private voidEvict the first available ban entry (the Cleaner handles bulk expiry).(package private) static intRead the cap last set bysetMaxEntries(int).private booleanimposeBan(LookupBanHammer.ReplyTunnel rt, long now) Record a ban, evicting the oldest if at capacity.private booleanisBanned(LookupBanHammer.ReplyTunnel rt, long now) Check whether the requester has an active ban.(package private) static voidsetBurstThreshold(int t) Set the burst ban threshold, clamped to 2..100.(package private) static voidsetCleanTimeMs(long ms) Set the cleanup interval, clamped to 5000..120000 ms.(package private) static voidsetMaxEntries(int max) Set the cap on tracked (peer, tunnel) pairs, clamped to 1000..200000.(package private) booleanRecord a lookup and check if the requester should be banned.private static voidslideWindow(ConcurrentLinkedDeque<Long> deque, long now) Remove entries older than 30s from the deque.
Field Details
burstTimestamps
private final ConcurrentHashMap<LookupBanHammer.ReplyTunnel, ConcurrentLinkedDeque<Long>> burstTimestampsThe lookup counts, by reply tunnel.banExpiration
The banned reply tunnels, by expiry time.DUMMY_ID
Tunnel id standing in for an unknown id.MAX_LOOKUPS
private static final int MAX_LOOKUPSLookups per window before a peer is banned.- See Also:
CLEAN_TIME
_burstThreshold
private static volatile int _burstThresholdLookups per window counted as a burst.BURST_WINDOW_MS
private static final long BURST_WINDOW_MSLength of the lookup counting window.- See Also:
BAN_DURATION_MS
private static final long BAN_DURATION_MSTime a banned peer stays banned.- See Also:
_maxEntries
private static volatile int _maxEntriesMost counted tunnels held at once._cleanTimeMs
static volatile long _cleanTimeMsInterval between cleanup passes in milliseconds. The first pass is scheduled with CLEAN_TIME instead, so this only governs rescheduling and can be retuned at runtime._cleaner
Timer cleaning the counters and bans.
Constructor Details
LookupBanHammer
LookupBanHammer()Create the tracker maps and schedule the first cleanup pass.
Method Details
cancel
void cancel()Stop the periodic cleanup.setMaxEntries
static void setMaxEntries(int max) Set the cap on tracked (peer, tunnel) pairs, clamped to 1000..200000.- Parameters:
max- requested cap on the number of tracked pairs
getMaxEntries
static int getMaxEntries()Read the cap last set bysetMaxEntries(int).- Returns:
- cap on the number of tracked (peer, tunnel) pairs
setCleanTimeMs
static void setCleanTimeMs(long ms) Set the cleanup interval, clamped to 5000..120000 ms.- Parameters:
ms- requested interval between cleanup passes, in milliseconds
setBurstThreshold
static void setBurstThreshold(int t) Set the burst ban threshold, clamped to 2..100.- Parameters:
t- lookups allowed within the 1-second burst window before a ban is imposed
shouldBan
isBanned
Check whether the requester has an active ban. Removes expired entries inline.- Parameters:
rt- the reply tunnel whose ban is checkednow- the current time in ms, compared against the ban expiry- Returns:
- true if the ban is still active
slideWindow
Remove entries older than 30s from the deque.- Parameters:
deque- lookup timestamps in ms, oldest firstnow- the current time in ms
burstCount
Count entries in the last 1-second window (newest-first iteration).- Parameters:
deque- lookup timestamps in ms, counted newest firstnow- the current time in ms- Returns:
- the number of lookups within the last BURST_WINDOW_MS
imposeBan
Record a ban, evicting the oldest if at capacity. Returns true.- Parameters:
rt- the reply tunnel to bannow- the current time in ms, the base of the ban expiry- Returns:
- always true, so callers can return the call directly
evictOneBan
private void evictOneBan()Evict the first available ban entry (the Cleaner handles bulk expiry).