java.lang.Object
net.i2p.i2ptunnel.BlocklistManager

public class BlocklistManagerextends Object
Manages HTTP blocklists for request filtering.

This class handles two types of blocklists:

  • URL Blocklist (http_blocklist.txt) - Contains patterns to match against request URLs. Requests matching these patterns are blocked.
  • Client Blocklist (http_blocklist_clients.txt) - Tracks destinations that have been blocked, to prevent repeated abuse.

The blocklist files are located in the I2P config directory and are monitored for changes. The URL blocklist is compiled into a regex pattern for efficient matching.

  • Field Details

    • HTTP_BLOCKLIST

      public static final String HTTP_BLOCKLIST
      Default filename for the URL blocklist.
      See Also:
    • HTTP_BLOCKLIST_CLIENTS

      public static final String HTTP_BLOCKLIST_CLIENTS
      Default filename for the client blocklist.
      See Also:
    • DEFAULT_CLIENT_LIMIT

      public static final int DEFAULT_CLIENT_LIMIT
      Default maximum number of entries in the client blocklist.
      See Also:
    • _log

      private final Log _log
      Logger for this class.
    • _configDir

      private final File _configDir
      Directory the blocklist file is read from.
    • _regexPattern

      private Pattern _regexPattern
      Compiled pattern matching blocked addresses.
    • _blocklistLastModified

      private long _blocklistLastModified
      File timestamp the pattern was built at.
    • _clientBlockList

      private List<String> _clientBlockList
      The blocked addresses read from the file.
    • blocklistClientsLastModified

      private static long blocklistClientsLastModified
      File timestamp the shared list was read at.
    • cachedClientBlockListSize

      private static int cachedClientBlockListSize
      Size of the shared list, -1 before first read.
    • _clientLimit

      private int _clientLimit
      Most clients this tunnel allows, from the blocklist.
  • Constructor Details

    • BlocklistManager

      public BlocklistManager(Log log, int clientLimit)
      Creates a new BlocklistManager.
      Parameters:
      log - the Log instance for logging blocklist events
      clientLimit - the maximum number of entries to keep in the client blocklist; if 0 or negative, uses DEFAULT_CLIENT_LIMIT
  • Method Details

    • shouldBlockRequest

      public boolean shouldBlockRequest(StringBuilder command)
      Checks if a request should be blocked based on the URL blocklist.

      This method checks if the request URL matches any pattern in the http_blocklist.txt file. The blocklist file is monitored for changes and will be reloaded if modified.

      Each line in the blocklist file is treated as a literal string (not regex), case-insensitive matching is applied.

      Parameters:
      command - the HTTP request command containing the URL
      Returns:
      true if the request should be blocked, false otherwise
    • getMatchedBlocklistString

      public String getMatchedBlocklistString(StringBuilder command)
      Gets the matched string from the blocklist for a request.

      If shouldBlockRequest(StringBuilder) returns true, this method can be called to retrieve the specific pattern that matched.

      Parameters:
      command - the HTTP request command containing the URL
      Returns:
      the matched blocklist string, or null if no match
    • compileRegexPattern

      private Pattern compileRegexPattern(File blocklistFile)
      Reads and compiles the blocklist file into a regex pattern.

      Each non-empty, non-comment line is treated as a literal string that will be matched against request URLs. The resulting pattern is case-insensitive.

      Parameters:
      blocklistFile - the blocklist file to read
      Returns:
      compiled Pattern, or null on error
    • logBlockedDestination

      public void logBlockedDestination(String destination) throws IOException
      Logs a blocked destination to the client blocklist.

      This method adds the destination to the http_blocklist_clients.txt file if not already present. The client blocklist is used to track destinations that have been blocked, to prevent repeated abuse attempts.

      The client blocklist has a maximum size limit. When the limit is reached, the oldest entry is removed before adding a new one.

      Parameters:
      destination - the Base32-encoded destination to log
      Throws:
      IOException - if an I/O error occurs
    • refreshClientBlocklist

      private void refreshClientBlocklist(File blocklistClients) throws IOException
      Refreshes the in-memory client blocklist from disk if the file has changed.
      Parameters:
      blocklistClients - the client blocklist file
      Throws:
      IOException - if an I/O error occurs
    • existsInClientBlocklist

      public boolean existsInClientBlocklist(String destination) throws IOException
      Checks if a destination exists in the client blocklist.

      This method checks if the given destination has been previously blocked. The blocklist file is monitored for changes and will be reloaded if modified.

      Parameters:
      destination - the Base32-encoded destination to check
      Returns:
      true if the destination is in the blocklist, false otherwise
      Throws:
      IOException - if an I/O error occurs
    • getClientBlocklistSize

      public int getClientBlocklistSize()
      The number of entries in the client blocklist.
      Returns:
      the number of entries in the client blocklist, or -1 if not initialized