net.i2p.i2ptunnel

## Class BlocklistManager

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

-     public class BlocklistManager
      extends 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 Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_CLIENT_LIMIT` Default maximum number of entries in the client blocklist
    `static String` | `HTTP_BLOCKLIST` Default filename for the URL blocklist
    `static String` | `HTTP_BLOCKLIST_CLIENTS` Default filename for the client blocklist

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BlocklistManager(Log log, int clientLimit)` Creates a new BlocklistManager.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `existsInClientBlocklist(String destination)` Checks if a destination exists in the client blocklist.
    `int` | `getClientBlocklistSize()` Gets the current size of the client blocklist.
    `String` | `getMatchedBlocklistString(StringBuilder command)` Gets the matched string from the blocklist for a request.
    `void` | `logBlockedDestination(String destination)` Logs a blocked destination to the client blocklist.
    `boolean` | `shouldBlockRequest(StringBuilder command)` Checks if a request should be blocked based on the URL blocklist.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_CLIENT_LIMIT

          public static final int DEFAULT_CLIENT_LIMIT

      Default maximum number of entries in the client blocklist
      See Also:
        Constant Field Values

    - #### HTTP_BLOCKLIST

          public static final String HTTP_BLOCKLIST

      Default filename for the URL blocklist
      See Also:
        Constant Field Values

    - #### HTTP_BLOCKLIST_CLIENTS

          public static final String HTTP_BLOCKLIST_CLIENTS

      Default filename for the client blocklist
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### 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 Detail

    - #### 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()

      Gets the current size of the client blocklist.
      Returns:
        the number of entries in the client blocklist, or -1 if not initialized

    - #### getMatchedBlocklistString

          public String getMatchedBlocklistString(StringBuilder command)

      Gets the matched string from the blocklist for a request.

      If shouldBlockRequest() 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

    - #### 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

    - #### 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