说明
- 插件会对
IP
进行判断,不符合判断的请求会优先跳转到redirectUrl
指向的地址,未设置redirectUrl
,则返回响应code
,message
- 插件允许开启白名单或者黑名单,只能开启其中一个
- 不在白名单或者在黑名单中的都无法通过判断
- 使用
ctx.addIPs(ips: array<string>)
批量添加IP
名单,参数是字符串数组 - 使用
ctx.addIP(ip: string, expire: number)
添加单个IP
,expire
单位为秒,不填则使用配置中的redisTTL
- 使用
ctx.addIPsExpire(ips: array<string, number>)
批量添加IP
名单,每个IP
都单独设置expire
- 使用
ctx.delIP(ip: string)
移除IP
名单 - 插件使用
redis
来存储数据,因此需要安装egg-redis
版本更新
安装
$ npm install egg-full-ip egg-redis --save
配置
-
配置
egg-full-ip
// {app_root}/config/config.default.jsexportsfullIP =// true: 开启白名单,而黑名单会失效whiteOnly: false// 当使用内置的判断黑白名单时,需要配置 redisredisName: null// redis 键前缀,根据 whiteOnly 会在前缀尾部自行添加 ':w' 和 ':b'redisPrefix: 'full:ip'// redis IP 缓存时间,单位: 秒, -1 表示永久redisTTL: -1// 无法通过判断时,跳转地址(优先极高),如 '/403'redirectUrl: null// 无法通过判断时,返回值code: 403message: 'REQUEST DISABLED'; -
配置
egg-redis
(二选一)-
方法一: 没有指定
redisName
,直接使用app.redis
// {app_root}/config/config.default.jsconfigredis =client:port: 6379host: '127.0.0.1'password: '123456'db: 1 // 单独使用一个数据库,方便观察,默认 0; -
方法二: 指定
redisName
,例如设置为fullIP
// {app_root}/config/config.default.jsconfigredis =clients:fullIP:port: 6379host: '127.0.0.1'password: '123456'db: 1 // 单独使用一个数据库,方便观察,默认 0;
-
使用
-
配置插件
// config/plugin.jsexportsfullIP =enable: truepackage: 'egg-full-ip';exportsredis =enable: truepackage: 'egg-redis'; -
添加/移除
IP
,以下函数属于context
扩展-
批量添加
IP
:addIPs(ips: array<string>)
ctx; -
批量添加
IP
且都单独设置expire
:addIPsExpire(ips: array<string, number>)
,expire
设置为undefined
表示为使用配置中的redisTTL
ctx; -
单个添加
IP
:addIP(ip: string, expire: number)
ctx;ctx; -
移除
IP
:delIP(ip: string)
ctx;
-