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
Modifier and TypeFieldDescriptionprivate longFile timestamp the pattern was built at.The blocked addresses read from the file.private intMost clients this tunnel allows, from the blocklist.private final FileDirectory the blocklist file is read from.private final LogLogger for this class.private PatternCompiled pattern matching blocked addresses.private static longFile timestamp the shared list was read at.private static intSize of the shared list, -1 before first read.static final intDefault maximum number of entries in the client blocklist.static final StringDefault filename for the URL blocklist.static final StringDefault filename for the client blocklist.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate PatterncompileRegexPattern(File blocklistFile) Reads and compiles the blocklist file into a regex pattern.booleanexistsInClientBlocklist(String destination) Checks if a destination exists in the client blocklist.intThe number of entries in the client blocklist.getMatchedBlocklistString(StringBuilder command) Gets the matched string from the blocklist for a request.voidlogBlockedDestination(String destination) Logs a blocked destination to the client blocklist.private voidrefreshClientBlocklist(File blocklistClients) Refreshes the in-memory client blocklist from disk if the file has changed.booleanshouldBlockRequest(StringBuilder command) Checks if a request should be blocked based on the URL blocklist.
Field Details
HTTP_BLOCKLIST
Default filename for the URL blocklist.- See Also:
HTTP_BLOCKLIST_CLIENTS
Default filename for the client blocklist.- See Also:
DEFAULT_CLIENT_LIMIT
public static final int DEFAULT_CLIENT_LIMITDefault maximum number of entries in the client blocklist.- See Also:
_log
Logger for this class._configDir
Directory the blocklist file is read from._regexPattern
Compiled pattern matching blocked addresses._blocklistLastModified
private long _blocklistLastModifiedFile timestamp the pattern was built at._clientBlockList
blocklistClientsLastModified
private static long blocklistClientsLastModifiedFile timestamp the shared list was read at.cachedClientBlockListSize
private static int cachedClientBlockListSizeSize of the shared list, -1 before first read._clientLimit
private int _clientLimitMost clients this tunnel allows, from the blocklist.
Constructor Details
BlocklistManager
Creates a new BlocklistManager.- Parameters:
log- the Log instance for logging blocklist eventsclientLimit- the maximum number of entries to keep in the client blocklist; if 0 or negative, uses DEFAULT_CLIENT_LIMIT
Method Details
shouldBlockRequest
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
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
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
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
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
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