java.lang.Object
net.i2p.router.networkdb.kademlia.LookupBanHammer

class LookupBanHammerextends Object
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.
  • Field Details

    • burstTimestamps

      The lookup counts, by reply tunnel.
    • banExpiration

      private final ConcurrentHashMap<LookupBanHammer.ReplyTunnel, Long> banExpiration
      The banned reply tunnels, by expiry time.
    • DUMMY_ID

      private static final TunnelId DUMMY_ID
      Tunnel id standing in for an unknown id.
    • MAX_LOOKUPS

      private static final int MAX_LOOKUPS
      Lookups per window before a peer is banned.
      See Also:
    • CLEAN_TIME

      private static final long CLEAN_TIME
      Interval of the counter cleanup.
      See Also:
    • _burstThreshold

      private static volatile int _burstThreshold
      Lookups per window counted as a burst.
    • BURST_WINDOW_MS

      private static final long BURST_WINDOW_MS
      Length of the lookup counting window.
      See Also:
    • BAN_DURATION_MS

      private static final long BAN_DURATION_MS
      Time a banned peer stays banned.
      See Also:
    • _maxEntries

      private static volatile int _maxEntries
      Most counted tunnels held at once.
    • _cleanTimeMs

      static volatile long _cleanTimeMs
      Interval 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

      private final LookupBanHammer.Cleaner _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 by setMaxEntries(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

      boolean shouldBan(Hash key, TunnelId id)
      Record a lookup and check if the requester should be banned.
      Parameters:
      key - the requesting peer's router hash, one half of the identity a ban is recorded against
      id - reply tunnel, or null for direct
      Returns:
      true if currently banned
    • isBanned

      private boolean isBanned(LookupBanHammer.ReplyTunnel rt, long now)
      Check whether the requester has an active ban. Removes expired entries inline.
      Parameters:
      rt - the reply tunnel whose ban is checked
      now - the current time in ms, compared against the ban expiry
      Returns:
      true if the ban is still active
    • slideWindow

      private static void slideWindow(ConcurrentLinkedDeque<Long> deque, long now)
      Remove entries older than 30s from the deque.
      Parameters:
      deque - lookup timestamps in ms, oldest first
      now - the current time in ms
    • burstCount

      private static int burstCount(ConcurrentLinkedDeque<Long> deque, long now)
      Count entries in the last 1-second window (newest-first iteration).
      Parameters:
      deque - lookup timestamps in ms, counted newest first
      now - the current time in ms
      Returns:
      the number of lookups within the last BURST_WINDOW_MS
    • imposeBan

      private boolean imposeBan(LookupBanHammer.ReplyTunnel rt, long now)
      Record a ban, evicting the oldest if at capacity. Returns true.
      Parameters:
      rt - the reply tunnel to ban
      now - 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).