public class DoSFilter extends Object implements javax.servlet.Filter
This filter is based on the QoSFilter
. it is useful for limiting
exposure to abuse from request flooding, whether malicious, or as a result of
a misconfigured client.
The filter keeps track of the number of requests from a connection per second. If a limit is exceeded, the request is either rejected, delayed, or throttled.
When a request is throttled, it is placed in a priority queue. Priority is given first to authenticated users and users with an HttpSession, then connections which can be identified by their IP addresses. Connections with no way to identify them are given lowest priority.
The extractUserId(ServletRequest request)
function should be
implemented, in order to uniquely identify authenticated users.
The following init parameters control the behavior of the filter:
ServletContext
attribute with the
filter name as the attribute name. This allows context external mechanism (eg JMX via ContextHandler.MANAGED_ATTRIBUTES
) to
manage the configuration of the filter.Modifier and Type | Field and Description |
---|---|
protected long |
_delayMs |
protected boolean |
_insertHeaders |
protected ContinuationListener[] |
_listener |
protected long |
_maxIdleTrackerMs |
protected long |
_maxRequestMs |
protected int |
_maxRequestsPerSec |
protected long |
_maxWaitMs |
protected String |
_name |
protected Semaphore |
_passes |
protected Queue<Continuation>[] |
_queue |
protected ConcurrentHashMap<String,org.eclipse.jetty.servlets.DoSFilter.RateTracker> |
_rateTrackers |
protected boolean |
_remotePort |
protected int |
_throttledRequests |
protected long |
_throttleMs |
protected boolean |
_trackSessions |
protected String |
_whitelistStr |
Constructor and Description |
---|
DoSFilter() |
Modifier and Type | Method and Description |
---|---|
protected void |
closeConnection(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Thread thread)
Takes drastic measures to return this response and stop this thread.
|
void |
destroy() |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain filterchain) |
protected void |
doFilterChain(javax.servlet.FilterChain chain,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
protected String |
extractUserId(javax.servlet.ServletRequest request)
Returns the user id, used to track this connection.
|
long |
getDelayMs()
Get delay (in milliseconds) that is applied to all requests
over the rate limit, before they are considered at all.
|
long |
getMaxIdleTrackerMs()
Get maximum amount of time (in milliseconds) to keep track
of request rates for a connection, before deciding that
the user has gone away, and discarding it.
|
protected int |
getMaxPriority() |
long |
getMaxRequestMs()
Get maximum amount of time (in milliseconds) to allow
the request to process.
|
int |
getMaxRequestsPerSec()
Get maximum number of requests from a connection per
second.
|
long |
getMaxWaitMs()
Get maximum amount of time (in milliseconds) the filter will
blocking wait for the throttle semaphore.
|
protected int |
getPriority(javax.servlet.ServletRequest request,
org.eclipse.jetty.servlets.DoSFilter.RateTracker tracker)
Get priority for this request, based on user type
|
org.eclipse.jetty.servlets.DoSFilter.RateTracker |
getRateTracker(javax.servlet.ServletRequest request)
Return a request rate tracker associated with this connection; keeps
track of this connection's request rate.
|
int |
getThrottledRequests()
Get number of requests over the rate limit able to be
considered at once.
|
long |
getThrottleMs()
Get amount of time (in milliseconds) to async wait for semaphore.
|
String |
getWhitelist()
Get a list of IP addresses that will not be rate limited.
|
void |
init(javax.servlet.FilterConfig filterConfig) |
protected void |
initWhitelist()
Initialize the IP address whitelist
|
boolean |
isInsertHeaders()
Check flag to insert the DoSFilter headers into the response.
|
boolean |
isRemotePort()
Get flag to have usage rate tracked by IP+port (effectively connection)
if session tracking is not used.
|
boolean |
isTrackSessions()
Get flag to have usage rate tracked by session if a session exists.
|
void |
setDelayMs(long value)
Set delay (in milliseconds) that is applied to all requests
over the rate limit, before they are considered at all.
|
void |
setInsertHeaders(boolean value)
Set flag to insert the DoSFilter headers into the response.
|
void |
setMaxIdleTrackerMs(long value)
Set maximum amount of time (in milliseconds) to keep track
of request rates for a connection, before deciding that
the user has gone away, and discarding it.
|
void |
setMaxRequestMs(long value)
Set maximum amount of time (in milliseconds) to allow
the request to process.
|
void |
setMaxRequestsPerSec(int value)
Get maximum number of requests from a connection per
second.
|
void |
setMaxWaitMs(long value)
Set maximum amount of time (in milliseconds) the filter will
blocking wait for the throttle semaphore.
|
void |
setRemotePort(boolean value)
Set flag to have usage rate tracked by IP+port (effectively connection)
if session tracking is not used.
|
void |
setThrottledRequests(int value)
Set number of requests over the rate limit able to be
considered at once.
|
void |
setThrottleMs(long value)
Set amount of time (in milliseconds) to async wait for semaphore.
|
void |
setTrackSessions(boolean value)
Set flag to have usage rate tracked by session if a session exists.
|
void |
setWhitelist(String value)
Set a list of IP addresses that will not be rate limited.
|
protected String _name
protected long _delayMs
protected long _throttleMs
protected long _maxWaitMs
protected long _maxRequestMs
protected long _maxIdleTrackerMs
protected boolean _insertHeaders
protected boolean _trackSessions
protected boolean _remotePort
protected int _throttledRequests
protected Semaphore _passes
protected Queue<Continuation>[] _queue
protected ContinuationListener[] _listener
protected int _maxRequestsPerSec
protected final ConcurrentHashMap<String,org.eclipse.jetty.servlets.DoSFilter.RateTracker> _rateTrackers
protected String _whitelistStr
public void init(javax.servlet.FilterConfig filterConfig)
init
in interface javax.servlet.Filter
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterchain) throws IOException, javax.servlet.ServletException
doFilter
in interface javax.servlet.Filter
IOException
javax.servlet.ServletException
protected void doFilterChain(javax.servlet.FilterChain chain, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException
chain
- request
- response
- IOException
javax.servlet.ServletException
protected void closeConnection(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Thread thread)
request
- current requestresponse
- current response, which must be stoppedthread
- the handling threadprotected int getPriority(javax.servlet.ServletRequest request, org.eclipse.jetty.servlets.DoSFilter.RateTracker tracker)
request
- tracker
- protected int getMaxPriority()
public org.eclipse.jetty.servlets.DoSFilter.RateTracker getRateTracker(javax.servlet.ServletRequest request)
request
- public void destroy()
destroy
in interface javax.servlet.Filter
protected String extractUserId(javax.servlet.ServletRequest request)
request
- protected void initWhitelist()
public int getMaxRequestsPerSec()
public void setMaxRequestsPerSec(int value)
value
- maximum number of requestspublic long getDelayMs()
public void setDelayMs(long value)
value
- delay (in milliseconds), 0 - no delay, -1 - reject requestpublic long getMaxWaitMs()
public void setMaxWaitMs(long value)
value
- maximum wait timepublic int getThrottledRequests()
public void setThrottledRequests(int value)
value
- number of requestspublic long getThrottleMs()
public void setThrottleMs(long value)
value
- wait timepublic long getMaxRequestMs()
public void setMaxRequestMs(long value)
value
- maximum processing timepublic long getMaxIdleTrackerMs()
public void setMaxIdleTrackerMs(long value)
value
- maximum tracking timepublic boolean isInsertHeaders()
public void setInsertHeaders(boolean value)
value
- value of the flagpublic boolean isTrackSessions()
public void setTrackSessions(boolean value)
value
- value of the flagpublic boolean isRemotePort()
public void setRemotePort(boolean value)
value
- value of the flagpublic String getWhitelist()
public void setWhitelist(String value)
value
- comma-separated whitelistCopyright © 1995-2013 Mort Bay Consulting. All Rights Reserved.