@@ -5,15 +5,21 @@ import { CouponService } from '../CouponService/couponService'
55
66export class RateLimiter {
77 PATH : string
8- type : 'ip' | 'wallet' | 'global'
8+ type : 'ip' | 'wallet' | 'global' | 'common_token_disbursal'
99
1010 constructor (
1111 app : any ,
1212 configs : RateLimiterConfig [ ] ,
13- type : 'ip' | 'wallet' | 'global' ,
13+ type : 'ip' | 'wallet' | 'global' | 'common_token_disbursal' ,
1414 couponService : CouponService ,
1515 keyGenerator ?: any ,
1616 ) {
17+ if ( configs . length === 0 ) {
18+ this . PATH = '/api/sendToken'
19+ this . type = 'common_token_disbursal'
20+ return
21+ }
22+
1723 this . PATH = configs [ 0 ] . RATELIMIT . PATH || '/api/sendToken'
1824 this . type = type
1925
@@ -27,7 +33,7 @@ export class RateLimiter {
2733 SKIP_FAILED_REQUESTS : RATELIMIT . SKIP_FAILED_REQUESTS || true ,
2834 }
2935
30- rateLimiters . set ( config . ID , this . getLimiter ( RL_CONFIG , keyGenerator ) )
36+ rateLimiters . set ( config . ID , this . getLimiter ( RL_CONFIG , config . ID , type , keyGenerator ) )
3137 } )
3238
3339 if ( configs [ 0 ] ?. RATELIMIT ?. REVERSE_PROXIES ) {
@@ -43,6 +49,8 @@ export class RateLimiter {
4349 return next ( )
4450 } else if ( this . type === 'wallet' && coupon && coupon . skipWalletRateLimit ) {
4551 return next ( )
52+ } else if ( this . type === 'common_token_disbursal' && coupon && coupon . skipCommonTokenDisbursalRateLimit ) {
53+ return next ( )
4654 }
4755 }
4856
@@ -54,15 +62,30 @@ export class RateLimiter {
5462 } )
5563 }
5664
57- getLimiter ( config : any , keyGenerator ?: any ) : RateLimitRequestHandler {
65+ getLimiter (
66+ config : any ,
67+ faucetConfigId : string ,
68+ type : 'ip' | 'wallet' | 'global' | 'common_token_disbursal' ,
69+ keyGenerator ?: any ,
70+ ) : RateLimitRequestHandler {
5871 const limiter = rateLimit ( {
5972 windowMs : config . WINDOW_SIZE * 60 * 1000 ,
6073 max : config . MAX_LIMIT ,
6174 standardHeaders : true ,
6275 legacyHeaders : false ,
6376 skipFailedRequests : config . SKIP_FAILED_REQUESTS ,
64- message : {
65- message : `Too many requests. Please try again after ${ config . WINDOW_SIZE } minutes`
77+ handler : ( req , res , next , options ) => {
78+ if ( type === 'common_token_disbursal' ) {
79+ console . log ( JSON . stringify ( {
80+ date : new Date ( ) ,
81+ type : "CommonTokenDisbursalRateLimit" ,
82+ faucetConfigId,
83+ ip : req . headers [ "cf-connecting-ip" ] || req . ip
84+ } ) )
85+ }
86+ res . status ( options . statusCode ) . send ( {
87+ message : `Too many requests. Please try again after ${ config . WINDOW_SIZE } minutes`
88+ } )
6689 } ,
6790 keyGenerator : keyGenerator ? keyGenerator : ( req , res ) => {
6891 const ip = this . getIP ( req )
0 commit comments