public class I2PTunnelHTTPServer extends I2PTunnelServer
Extends I2PTunnelServer to filter HTTP headers from client to server, replacing the Host header with configured value. Compresses HTTP message body and sets Content-Encoding: x-i2p-gzip when browser requests Accept-Encoding: x-i2p-gzip.
Features include POST/PUT rate limiting, referer and user-agent rejection, inproxy detection, HTTP blocklist support, keepalive management, and response header security filtering.
| Modifier and Type | Field and Description |
|---|---|
(package private) static int | CONN_CLOSEConnection: close or unknown value |
(package private) static int | CONN_KEEPALIVEConnection: keep-alive |
(package private) static int | CONN_NONEConnection header absent |
(package private) static int | CONN_UPGRADEConnection: upgrade (websocket) |
static int | DEFAULT_POST_BAN_TIMEPost throttle window (seconds). |
static int | DEFAULT_POST_MAXDefault max posts per window. |
static int | DEFAULT_POST_TOTAL_BAN_TIMEDefault total ban time for excessive posts. |
static int | DEFAULT_POST_TOTAL_MAXDefault max total posts before ban. |
static int | DEFAULT_POST_WINDOWDefault post rate-limit window (seconds). |
(package private) static String | ERR_FORBIDDEN403 Forbidden error response |
(package private) static String | ERR_NOT_FOUND404 Not Found error response |
protected static String | ERR_REQUEST_TIMEOUTHTTP 408 Request Timeout error response. |
static String | OPT_ADD_RESPONSE_HEADER_ALLOWConfig key to add Allow response header. |
static String | OPT_ADD_RESPONSE_HEADER_CACHE_CONTROLConfig key to add Cache-Control response header. |
static String | OPT_ADD_RESPONSE_HEADER_NOSNIFFConfig key to add X-Content-Type-Options: nosniff. |
static String | OPT_ADD_RESPONSE_HEADER_REFERRER_POLICYConfig key to add Referrer-Policy response header. |
static String | OPT_KEEPALIVEConfig key to enable keepalive. |
static String | OPT_POST_BAN_TIMEConfig key for post ban time. |
static String | OPT_POST_MAXConfig key for max posts. |
static String | OPT_POST_TOTAL_BAN_TIMEConfig key for post total ban time. |
static String | OPT_POST_TOTAL_MAXConfig key for max total posts. |
static String | OPT_POST_WINDOWConfig key for post check time window. |
static String | OPT_REJECT_INPROXYConfig key to reject requests from inproxy. |
static String | OPT_REJECT_REFERERConfig key to reject requests with Referer header. |
static String | OPT_REJECT_USER_AGENTSConfig key to reject specific User-Agents. |
static String | OPT_USER_AGENTSConfig key for User-Agent reject list. |
_clientExecutor, _log, bidir, i2pss, l, localPort, PROP_ALT_PKF, PROP_UNIQUE_LOCAL, PROP_USE_SSL, readTimeout, remoteHost, remotePort, serverId, slock, sockMgr, sslLock, taskopen, tunnel| Constructor and Description |
|---|
I2PTunnelHTTPServer(InetAddress host,
int port,
File privkey,
String privkeyname,
String spoofHost,
Logging l,
EventDispatcher notifyThis,
I2PTunnel tunnel)Create an HTTP tunnel server from a private key file. |
I2PTunnelHTTPServer(InetAddress host,
int port,
InputStream privData,
String privkeyname,
String spoofHost,
Logging l,
EventDispatcher notifyThis,
I2PTunnel tunnel)Create an HTTP tunnel server from a private key stream. |
I2PTunnelHTTPServer(InetAddress host,
int port,
String privData,
String spoofHost,
Logging l,
EventDispatcher notifyThis,
I2PTunnel tunnel)Create an HTTP tunnel server with a private key string. |
| Modifier and Type | Method and Description |
|---|---|
(package private) static void | addEntry(Map<String,List<String>> headers,
String key,
String value)Add an entry to the multimap. |
protected void | blockingHandle(I2PSocket socket)Called by the thread pool of I2PSocket handlers. |
boolean | close(boolean forced)Close the tunnel and stop the POST throttler. |
(package private) static int | getConnectionType(Map<String,List<String>> headers)Classify the request's Connection header value. |
(package private) static boolean | hasGzipEncoding(String enc)Whether the Accept-Encoding value includes x-i2p-gzip. |
(package private) static boolean | isGzipAllowed(Properties opts)Whether the tunnel allows gzip responses, from the tunnel options. |
(package private) boolean | isInproxyRejection(Map<String,List<String>> headers,
I2PSocket socket,
String peerB32,
Properties opts)Reject requests that contain forwarded-for headers (inproxy detection). |
(package private) static boolean | isKeepAliveRequest(String cmd)Whether the request can keep the connection alive: GET or HEAD with HTTP/1.1. |
(package private) boolean | isPostThrottled(StringBuilder command,
Hash peerHash,
I2PSocket socket,
String peerB32)Check POST/PUT throttling for this peer. |
(package private) boolean | isRefererRejection(Map<String,List<String>> headers,
I2PSocket socket,
String peerB32,
Properties opts)Reject requests with absolute Referer URIs. |
(package private) boolean | isUserAgentRejection(Map<String,List<String>> headers,
I2PSocket socket,
String peerB32,
Properties opts)Reject requests with blacklisted User-Agent strings. |
void | optionsUpdated(I2PTunnel tunnel)Called when tunnel options are updated. |
static Map<String,List<String>> | readHeaders(I2PSocket socket,
InputStream in,
StringBuilder command,
Set<String> skipHeaders,
I2PAppContext ctx,
long initialTimeout)From I2P to server: socket non-null, in null. |
static Map<String,List<String>> | readHeaders(I2PSocket socket,
InputStream in,
StringBuilder command,
String[] skipHeaders,
I2PAppContext ctx,
long initialTimeout)Read headers from the input stream, using an array of skip headers. |
(package private) static Map<String,List<String>> | readRequestHeaders(I2PSocket socket,
StringBuilder command,
int requestCount,
String peerB32,
I2PAppContext ctx,
Log log)Read the request headers, handling the specific errors that can occur
by sending the appropriate error response to the client. |
(package private) static void | setEntry(Map<String,List<String>> headers,
String key,
String value)Remove the other matching entries and set this entry as the only one. |
void | startRunning()Start the tunnel and initialize timing and throttling. |
destroy, getReadTimeout, getSocket, getSocket, getSocketManager, getSocketString, run, setReadTimeoutconnected, disconnected, errorOccurred, getBooleanOption, getId, getTunnel, isOpen, reportAbuse, routerDisconnected, setId, setName, setTunnel, toStringattachEventDispatcher, detachEventDispatcher, getEventDispatcher, getEvents, getEventValue, ignoreEvents, notifyEvent, unIgnoreEvents, waitEventValuestatic final int CONN_CLOSE
static final int CONN_KEEPALIVE
static final int CONN_NONE
static final int CONN_UPGRADE
public static final int DEFAULT_POST_BAN_TIME
public static final int DEFAULT_POST_MAX
public static final int DEFAULT_POST_TOTAL_BAN_TIME
public static final int DEFAULT_POST_TOTAL_MAX
public static final int DEFAULT_POST_WINDOW
static final String ERR_FORBIDDEN
static final String ERR_NOT_FOUND
protected static final String ERR_REQUEST_TIMEOUT
public static final String OPT_ADD_RESPONSE_HEADER_ALLOW
public static final String OPT_ADD_RESPONSE_HEADER_CACHE_CONTROL
public static final String OPT_ADD_RESPONSE_HEADER_NOSNIFF
public static final String OPT_ADD_RESPONSE_HEADER_REFERRER_POLICY
public static final String OPT_KEEPALIVE
public static final String OPT_POST_BAN_TIME
public static final String OPT_POST_MAX
public static final String OPT_POST_TOTAL_BAN_TIME
public static final String OPT_POST_TOTAL_MAX
public static final String OPT_POST_WINDOW
public static final String OPT_REJECT_INPROXY
public static final String OPT_REJECT_REFERER
public static final String OPT_REJECT_USER_AGENTS
public static final String OPT_USER_AGENTS
public I2PTunnelHTTPServer(InetAddress host, int port, File privkey, String privkeyname, String spoofHost, Logging l, EventDispatcher notifyThis, I2PTunnel tunnel)
host - the local address to bind toport - the local portprivkey - the private key fileprivkeyname - the name for the private keyspoofHost - the hostname to spoof in the Host header, or nulll - the logging instancenotifyThis - the event dispatcher for notificationstunnel - the parent tunnel configurationpublic I2PTunnelHTTPServer(InetAddress host, int port, InputStream privData, String privkeyname, String spoofHost, Logging l, EventDispatcher notifyThis, I2PTunnel tunnel)
host - the local address to bind toport - the local portprivData - the input stream containing the private key dataprivkeyname - the name for the private keyspoofHost - the hostname to spoof in the Host header, or nulll - the logging instancenotifyThis - the event dispatcher for notificationstunnel - the parent tunnel configurationpublic I2PTunnelHTTPServer(InetAddress host, int port, String privData, String spoofHost, Logging l, EventDispatcher notifyThis, I2PTunnel tunnel)
host - the local address to bind toport - the local portprivData - the private key data as a Base64 stringspoofHost - the hostname to spoof in the Host header, or nulll - the logging instancenotifyThis - the event dispatcher for notificationstunnel - the parent tunnel configurationstatic void addEntry(Map<String,List<String>> headers, String key, String value)
headers - the header multimap to modifykey - the header namevalue - the header value to addprotected void blockingHandle(I2PSocket socket)
blockingHandle in class I2PTunnelServersocket - the incoming I2P socket from the clientI2PTunnelServer.getSocket(Hash, int),
I2PTunnelRunnerpublic boolean close(boolean forced)
close in class I2PTunnelServerforced - true to force closestatic int getConnectionType(Map<String,List<String>> headers)
headers - request headersstatic boolean hasGzipEncoding(String enc)
enc - an Accept-Encoding header value, or nullstatic boolean isGzipAllowed(Properties opts)
opts - the client optionsboolean isInproxyRejection(Map<String,List<String>> headers, I2PSocket socket, String peerB32, Properties opts)
headers - the request headerssocket - the incoming I2P socketpeerB32 - the peer's base32 address for loggingopts - the tunnel client optionsstatic boolean isKeepAliveRequest(String cmd)
cmd - the trimmed request lineboolean isPostThrottled(StringBuilder command, Hash peerHash, I2PSocket socket, String peerB32)
command - the request command string (first line)peerHash - the peer's hash for throttling lookupsocket - the incoming I2P socketpeerB32 - the peer's base32 address for loggingboolean isRefererRejection(Map<String,List<String>> headers, I2PSocket socket, String peerB32, Properties opts)
headers - the request headerssocket - the incoming I2P socketpeerB32 - the peer's base32 address for loggingopts - the tunnel client optionsboolean isUserAgentRejection(Map<String,List<String>> headers, I2PSocket socket, String peerB32, Properties opts)
headers - the request headerssocket - the incoming I2P socketpeerB32 - the peer's base32 address for loggingopts - the tunnel client optionspublic void optionsUpdated(I2PTunnel tunnel)
optionsUpdated in class I2PTunnelServertunnel - the tunnelpublic static Map<String,List<String>> readHeaders(I2PSocket socket, InputStream in, StringBuilder command, Set<String> skipHeaders, I2PAppContext ctx, long initialTimeout) throws IOException
socket - if null, use in as InputStreamin - if null, use socket.getInputStream() as InputStreamcommand - out parameter, first lineskipHeaders - MUST be lower caseSocketTimeoutException - if timeout is reached before newlineEOFException - if EOF is reached before newlineLineTooLongException - if one header too long, or too many headers, or total size too bigRequestTooLongException - if too longBadRequestException - on bad headersIOException - on other errors in the underlying streampublic static Map<String,List<String>> readHeaders(I2PSocket socket, InputStream in, StringBuilder command, String[] skipHeaders, I2PAppContext ctx, long initialTimeout) throws IOException
socket - if non-null, read from socket; otherwise read from inin - if non-null and socket is null, read from this streamcommand - output buffer for the first request lineskipHeaders - array of lower-case header names to filter outctx - I2P app context for clock and timeoutinitialTimeout - timeout for the first line readIOException - on I/O errorsstatic Map<String,List<String>> readRequestHeaders(I2PSocket socket, StringBuilder command, int requestCount, String peerB32, I2PAppContext ctx, Log log) throws IOException
socket - the client socketcommand - buffer for the request linerequestCount - the number of requests already handled on this connection (0 = first)peerB32 - the client's base32 for loggingctx - the I2P app contextlog - the logging instanceIOException - on other I/O errors, propagated to the callerstatic void setEntry(Map<String,List<String>> headers, String key, String value)
headers - the header multimap to modifykey - the header namevalue - the header value to set (replaces all existing values for this key)public void startRunning()
startRunning in class I2PTunnelServer