Nodejs 如何设计一个限频接口来防止攻击

做过后端研发的基本对接口限频完全不陌生,特别是针对一些核心接口受到攻击的时候,比如 Jmeter 来通过一些用户填写接入恶意灌入脏数据。

那在 nodejs 这边如何设计限频接口呢?

基于 express 的 express-rate-limit

 

源码地址:

https://github.com/nfriedly/express-rate-limit

 Basic rate-limiting middleware for expressUse to limit repeated requests to public APIs and/or endpoints such as password reset.

 

 

 

  

其实这个包不老,都是 typescript 重写的。

这里面有几个问题,因为要限制:

比如同一个 IP 在一定时间内访问的次数不能超过一定的值。

那就需要通过一个 Store 来记录,支持:

 1、memory-store - 比较简单的 in-memory

然后就是依托 redis 或者 mongo,还有估计很多前端不知道的 Memcached:

 

2、rate-limit-redis

3、rate-limit-memcached

4、rate-limit-mongo

这个包的作者还专门写了一篇文章来描述如何自定义一个 Store,提供 typescript 和 js 的版本 

https://github.com/nfriedly/express-rate-limit/wiki/Creating-Your-Own-Store

自定义存储将在内部跟踪每个标识符(如 IP地址)收到的访问次数,并随着时间的推移自动减少次数。

 

一个 Store 必须包含如下方法:

 

定义一个 Store 的 ts 类型:

export interface Store {
  init?: (options: Options) => void
  increment(key: string) => Promise<IncrementResponse> | IncrementResponse
  decrement: (key: string) => Promise<void> | void
  resetKey: (key: string) => Promise<void> | void
  resetAll?: () => Promise<void> | void  
}

 

这里的 IncrementResponse 类型:

export type IncrementResponse = {
  totalHits: number
  resetTime: Date | undefined
}

 

 

然后在 express-rate-limit 的 memory-store 设计中:

 

源码地址如下:

https://github.com/nfriedly/express-rate-limit/blob/master/source/memory-store.ts

 

源码设计:

export default class MemoryStore implements Store {
  windowMs!: number
  hits!: {
    [key: string]: number | undefined
  }
  resetTime!: Date
}

 

这里的 windowMs:

The duration of time before which all hit counts 
are reset (in milliseconds)

 

*、increment 方法

It adds 1 to the internal count for a key 
and returns an object consisting of the new internal count (totalHits) 
and the time that the count will reach 0 (resetTime).

 

源码设计:接受一个参数 key,进行加一

async increment(key: string): Promise<IncrementResponse> {
    const totalHits = (this.hits[key] ?? 0) + 1
    this.hits[key] = totalHits

    return {
        totalHits,
        resetTime: this.resetTime,
    }
}

 

 

*、decrement 方法

is used only to 'uncount' requests 
when one or both of the skipSuccessfulRequests 
or skipFailedRequests options are enabled.

 

源码设计:接受一个参数 key,进行减一

async decrement(key: string): Promise<void> {
    const current = this.hits[key]
    if (current) {
      this.hits[key] = current - 1
    }
}

 

*、init 方法

allows the store to set itself up 
using the options passed to the middleware.

允许传一些配置对象给 middleware

 

源码设计:

init(options: Options): void {
    // Get the duration of a window from the options
    this.windowMs = options.windowMs
    // Then calculate the reset time using that
    this.resetTime = calculateNextResetTime(this.windowMs)

    // Initialise the hit counter map
    this.hits = {}

    // Reset hit counts for ALL clients every `windowMs` - this will also
    // re-calculate the `resetTime`
    const interval = setInterval(async () => {
        await this.resetAll()
    }, this.windowMs)
    if (interval.unref) {
        interval.unref()
    }
}

 

看下代码设计:

 

*、resetKey 方法

Resets the rate limiting for a given key.

 

源码设计:接受一个参数 key

async resetKey(key: string): Promise<void> {
  delete this.hits[key]
}

 

 

*、resetAll 方法

 

reset everyone's hit counter

 

源码设计:

async resetAll(): Promise<void> {
  this.hits = {}
  this.resetTime = calculateNextResetTime(this.windowMs)
}

 

这里核心依赖一个方法:calculateNextResetTime

 

const calculateNextResetTime = (windowMs: number): Date => {
  const resetTime = new Date()
  resetTime.setMilliseconds(resetTime.getMilliseconds() + windowMs)
  return resetTime
}

 

本篇核心从源码角度接受了如何设计限频依赖的 Store 的设计,下一篇我们继续剖析

 

喜欢这篇文章, 觉得有收获?欢迎打赏~~

想要内推字节的同学也可以联系我,或者直接使用以下链接进行投递:

社招:https://job.toutiao.com/s/d735st8

校招:字节跳动校招内推码: WSRYCQU 投递链接: https://jobs.toutiao.com/s/d739WNP

posted @   苍青浪  阅读(1040)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2021-07-21 MySQL 更新不成功,事务问题搞清楚了吗?
2021-07-21 mysql数据库时间类型datetime、bigint、timestamp的查询效率比较
点击右上角即可分享
微信分享提示
哥伦布
03:14发布
哥伦布
03:14发布
7°
多云
西南风
3级
空气质量
相对湿度
64%
今天
多云
-1°/10°
周三
小雨
2°/12°
周四
中雨
2°/14°