0.0.3 • Published 9 years ago

ipblock v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

ipblock

koa middleware 限制ip访问次数

Installation

npm install ipblock	

Example

code

var koa = require("koa");
var app = koa(), ipblock = require("ipblock");

// block router
// test: 100000 ms / 10 times

ipblock(app, {method: "get", path:"/block", times: 10, range: 10000});
ipblock(app, {method: "post", path:"/block", times: 10, range: 10000});
ipblock(app, {method: "get", path:"/block/:id", times: 10, range: 10000});


app.use(function*(){

	this.body = "Hello World";
});

app.listen(4000);

Test

bash

	sh testes/test.sh

output

200
200
200
200
200
200
200
200
200
200
403

策略 (假设30s 限制10次访问

  • 时间节点A,开始访问
  • 则设A为cursor。开始计数,达到10次,则 开始触发block算法。
  • 如果当前时间戳 - A > 30s,则 A的节点往前移一位,且把当前时间戳压入栈。
  • 如果当前时间戳 - A < 30s,则 返回403,不做任何操作。