egg-full-ip

1.1.0 • Public • Published

说明

  • 插件会对IP进行判断,不符合判断的请求会优先跳转到redirectUrl指向的地址,未设置redirectUrl,则返回响应code, message
  • 插件允许开启白名单或者黑名单,只能开启其中一个
  • 不在白名单或者在黑名单中的都无法通过判断
  • 使用ctx.addIPs(ips: array<string>)批量添加IP名单,参数是字符串数组
  • 使用ctx.addIP(ip: string, expire: number)添加单个IPexpire单位为秒,不填则使用配置中的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.js
    exports.fullIP = {
      // true: 开启白名单,而黑名单会失效
      whiteOnly: false,
      // 当使用内置的判断黑白名单时,需要配置 redis
      redisName: null,
      // redis 键前缀,根据 whiteOnly 会在前缀尾部自行添加 ':w' 和 ':b'
      redisPrefix: 'full:ip',
      // redis IP 缓存时间,单位: 秒, -1 表示永久
      redisTTL: -1,
     
      // 无法通过判断时,跳转地址(优先极高),如 '/403'
      redirectUrl: null,
      // 无法通过判断时,返回值
      code: 403,
      message: 'REQUEST DISABLED',
    };
  • 配置 egg-redis(二选一)

    • 方法一: 没有指定redisName,直接使用app.redis

      // {app_root}/config/config.default.js
      config.redis = {
        client: {
          port: 6379,
          host: '127.0.0.1',
          password: '123456',
          db: 1, // 单独使用一个数据库,方便观察,默认 0
        },
      };
    • 方法二: 指定redisName,例如设置为fullIP

      // {app_root}/config/config.default.js
      config.redis = {
        clients: {
          fullIP: {
            port: 6379,
            host: '127.0.0.1',
            password: '123456',
            db: 1, // 单独使用一个数据库,方便观察,默认 0
          },
        },
      };

使用

  • 配置插件

    // config/plugin.js
    exports.fullIP = {
      enable: true,
      package: 'egg-full-ip',
    };
     
    exports.redis = {
      enable: true,
      package: 'egg-redis',
    };
  • 添加/移除IP,以下函数属于context扩展

    • 批量添加IP: addIPs(ips: array<string>)

      ctx.addIPs(['192.168.1.5', '192.168.1.12']);
    • 批量添加IP且都单独设置expire: addIPsExpire(ips: array<string, number>)expire设置为undefined表示为使用配置中的redisTTL

      ctx.addIPsExpire([
        ['192.168.1.5', 300],
        ['192.168.1.12', -1],
        ['192.168.1.33', undefined],
      ]);
    • 单个添加IP: addIP(ip: string, expire: number)

      ctx.addIP('192.168.1.5', 300);
      ctx.addIP('192.168.1.12');
    • 移除IP: delIP(ip: string)

      ctx.delIP('192.168.1.5');

License

MIT

Package Sidebar

Install

npm i egg-full-ip

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

11.1 kB

Total Files

8

Last publish

Collaborators

  • hi.alex.happiness