robot-check-service.js 1.05 KB
'use strict';

const url = require('url');
const cache = global.yoho.cache.master;
const Promise = require('bluebird');
const config = global.yoho.config;
const _ = require('lodash');
const humanExpire = 3600;

const removeBlack = (remoteIp, apiLimitValidate, referer) => {
    let operations = [];

    if (referer) {
        let pid = _.get(url.parse(referer, true), 'query.pid');

        pid && operations.push(cache.delAsync(`${pid}:${remoteIp}`));
    }

    operations.push(cache.delAsync(`${config.app}:limiter:${remoteIp}`));

    // 验证码之后一小时之内不再限制qps
    if (apiLimitValidate) {
        operations.push(cache.setAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`, 1, humanExpire));
    } else {
        operations.push(cache.setAsync(`${config.app}:limiter:ishuman:${remoteIp}`, 1, humanExpire));
    }

    _.forEach(config.REQUEST_LIMIT, (val, key) => {
        operations.push(cache.delAsync(`${config.app}:limiter:${key}:max:${remoteIp}`));
    });

    return Promise.all(operations);
};

module.exports = {
    removeBlack
};