Rate Limiting on Firebase Hosting

前端 未结 2 1652
我在风中等你
我在风中等你 2021-02-14 03:25

I\'ve been searching for ways to rate limit requests by IP, but was not able to find any resources. Basically what I\'m looking is a way to implement firewall logic. I know that

2条回答
  •  滥情空心
    2021-02-14 04:12

    It seems to be the current rate limit is to use some middleware like express-rate-limiter. Then in your server.ts (or .js if JavaScript) file you can do as follows:

    import * as express from 'express';
    import * as rateLimit from 'express-rate-limit';
    
    const server: Express = express(); 
    
    server.set('trust proxy', 1); // Enable because the application is behind reverse proxy (Firebase).
    server.use(
      rateLimit({
        max: 100, // Max 100 connections per windowMs can be done before sending HTTP 429 (Too Many Requests) response code. After 100 requests within 15 minutes block the IP.
        message:
          'This IP has been temporarily blocked due to too many requests, please try again later.',
        windowMs: 15 * 60 * 1000 // In milliseconds, keep records of requests in memory for 15 minutes.
      })
    );
    

    Alternatively, if you don't want to block the IP, rather slow it down use express-slow-down.

提交回复
热议问题