1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.upload;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37
38 import java.io.IOException;
39
40 import java.util.HashMap;
41 import java.util.LinkedList;
42 import java.util.Map;
43
44 import javax.servlet.Filter;
45 import javax.servlet.FilterChain;
46 import javax.servlet.FilterConfig;
47 import javax.servlet.ServletException;
48 import javax.servlet.ServletRequest;
49 import javax.servlet.ServletResponse;
50
51 import org.apache.commons.collections.CollectionUtils;
52
53
54
55
56
57 public class DosGuardFilter implements Filter
58 {
59
60 private static final int INITIAL_CAPACITY = 100;
61
62
63 private int _nMinContentLength;
64
65
66 private int _nMinInterval;
67
68
69 private Map<String, Long> _mapLastRequestTimes;
70
71
72
73 private LinkedList<Entry> _listOrderedRequests;
74
75
76
77
78 @Override
79 public void init( FilterConfig config ) throws ServletException
80 {
81 _mapLastRequestTimes = new HashMap<>( INITIAL_CAPACITY );
82 _listOrderedRequests = new LinkedList<>( );
83
84 try
85 {
86 String paramValue = config.getInitParameter( "minContentLength" );
87
88 if ( paramValue != null )
89 {
90 _nMinContentLength = Integer.parseInt( paramValue );
91 }
92
93 paramValue = config.getInitParameter( "minInterval" );
94
95 if ( paramValue != null )
96 {
97 _nMinInterval = Integer.parseInt( paramValue );
98 }
99 }
100 catch( NumberFormatException ex )
101 {
102 ServletException servletEx = new ServletException( ex.getMessage( ) );
103 servletEx.initCause( ex );
104 throw servletEx;
105 }
106 }
107
108
109
110
111 @Override
112 public void destroy( )
113 {
114
115 }
116
117
118
119
120 @Override
121 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
122 {
123
124 if ( this.isAllowed( request.getRemoteAddr( ), request.getContentLength( ) ) )
125 {
126 chain.doFilter( request, response );
127 }
128 else
129 {
130 throw new ServletException( "DOS Guard : Too many upload from the same IP !" );
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143 public synchronized boolean isAllowed( String strRemoteAddr, int iContentLength )
144 {
145 AppLogService.debug( "DosGuard : isAllowed({}, {})", strRemoteAddr, iContentLength );
146
147
148 if ( _nMinInterval < 0 )
149 {
150 AppLogService.debug( "minInterval is below minimum, ignored" );
151
152 return true;
153 }
154
155
156 if ( iContentLength < _nMinContentLength )
157 {
158 AppLogService.debug( "ContentLength is below minimum, ignored" );
159
160 return true;
161 }
162
163
164 long lRequestTime = System.currentTimeMillis( );
165 AppLogService.debug( "Request time : {}", lRequestTime );
166
167
168 Long previousRequestTime = _mapLastRequestTimes.get( strRemoteAddr );
169 AppLogService.debug( "Previous request time : {}", previousRequestTime );
170
171 if ( previousRequestTime != null )
172 {
173 AppLogService.debug( "IP is in the map" );
174
175
176 if ( lRequestTime > ( previousRequestTime.longValue( ) + _nMinInterval ) )
177 {
178 AppLogService.debug( "IP is allowed to make a new request" );
179
180
181 this.cleanExpiredEntries( );
182
183
184 _mapLastRequestTimes.put( strRemoteAddr, Long.valueOf( lRequestTime ) );
185
186
187 _listOrderedRequests.addFirst( new Entry( strRemoteAddr, lRequestTime ) );
188
189 return true;
190 }
191
192 AppLogService.debug( "IP is not allowed to make a new request" );
193
194 return false;
195 }
196
197 AppLogService.debug( "IP is not in the map" );
198
199
200 this.cleanExpiredEntries( );
201
202
203 _mapLastRequestTimes.put( strRemoteAddr, Long.valueOf( lRequestTime ) );
204
205
206 _listOrderedRequests.addFirst( new Entry( strRemoteAddr, lRequestTime ) );
207
208 return true;
209 }
210
211
212
213
214 private void cleanExpiredEntries( )
215 {
216 AppLogService.debug( "DosGuard.class : cleanExpiredEntries()" );
217
218 if ( CollectionUtils.isNotEmpty( _listOrderedRequests ) )
219 {
220
221 long lMinTime = System.currentTimeMillis( ) - _nMinInterval;
222
223 AppLogService.debug( "Min time : {}", lMinTime );
224
225
226 boolean bDone = false;
227
228 while ( !bDone && CollectionUtils.isNotEmpty( _listOrderedRequests ) )
229 {
230
231
232 Entry lastEntry = _listOrderedRequests.getLast( );
233
234 if ( lastEntry.getRequestTime( ) < lMinTime )
235 {
236
237 _mapLastRequestTimes.remove( lastEntry.getRemoteAddr( ) );
238 _listOrderedRequests.removeLast( );
239
240 AppLogService.debug( "Removing [{}, {}]", lastEntry.getRemoteAddr( ), lastEntry.getRequestTime( ) );
241 }
242 else
243 {
244 bDone = true;
245 }
246 }
247 }
248 }
249
250
251
252
253 private static class Entry
254 {
255 private String _strRemoteAddr;
256 private long _lRequestTime;
257
258
259
260
261
262
263
264
265
266 public Entry( String strRemoteAddr, long lRequestTime )
267 {
268 this._strRemoteAddr = strRemoteAddr;
269 this._lRequestTime = lRequestTime;
270 }
271
272
273
274
275
276
277 public String getRemoteAddr( )
278 {
279 return _strRemoteAddr;
280 }
281
282
283
284
285
286
287 public long getRequestTime( )
288 {
289 return _lRequestTime;
290 }
291 }
292 }