File tree 1 file changed +6
-3
lines changed
src/main/java/org/javawebstack/webutils/middlewares
1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -24,15 +24,18 @@ public RateLimitMiddleware(long millis){
24
24
}
25
25
26
26
public Object handle (Exchange exchange ) {
27
+ String ip = exchange .rawRequest ().getRemoteAddr ();
28
+ if (exchange .header ("X-Forwarded-For" ) != null )
29
+ ip = exchange .header ("X-Forwarded-For" );
27
30
RateLimit rateLimit = null ;
28
- if (rateLimits .containsKey (exchange . rawRequest (). getRemoteAddr () )) {
29
- rateLimit = rateLimits .get (exchange . rawRequest (). getRemoteAddr () );
31
+ if (rateLimits .containsKey (ip )) {
32
+ rateLimit = rateLimits .get (ip );
30
33
if (rateLimit .stillAlive () && rateLimit .getAndDecrease () <= 1 )
31
34
throw new RateLimitException ();
32
35
}
33
36
if (rateLimit == null || !rateLimit .stillAlive ())
34
37
rateLimit = new RateLimit (System .currentTimeMillis () + millis , this .rateLimit );
35
- rateLimits .put (exchange . rawRequest (). getRemoteAddr () , rateLimit );
38
+ rateLimits .put (ip , rateLimit );
36
39
exchange .header ("X-Rate-Limit-Limit" , String .valueOf (this .rateLimit ));
37
40
exchange .header ("X-Rate-Limit-Remaining" , String .valueOf (rateLimit .getRateLimitLeft ()));
38
41
exchange .header ("X-Rate-Limit-Reset" , String .valueOf (rateLimit .getTimeMillis ()/1000 ));
You can’t perform that action at this time.
0 commit comments