{"size": 1223, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "s249359986/koa-ip", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "s249359986/koa-ip", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "s249359986/koa-ip", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.4418604651, "max_line_length": 93, "alphanum_fraction": 0.4701553557}
/**
 * Created by songdonghong on 2016/12/9.
 */

function ip(conf) {
    var MSG="非法ip";
    if (typeof conf !== 'object') {
        if (typeof conf === 'string') {
            conf = {whiteList: [conf]};
        } else {
            conf = {
            };
        }
    }
    if (Array.isArray(conf)) {
        conf = {whiteList: conf};
    }
    return function* (next) {
        var _this=this;
        var _ip = typeof conf.proxy === 'function' ? conf.proxy.call(_this, _this) : this.ip;
        var pass = false;
        if (conf.whiteList && Array.isArray(conf.whiteList)) {
            pass = conf.whiteList.some(function (item) {
                return RegExp(item).test(_ip);
            });
        }
        if (conf.blackList && Array.isArray(conf.blackList)) {
            pass = !conf.blackList.some(function (item) {
                return RegExp(item).test(_ip);
            });
        }
        if (pass) {
            console.log((new Date).toUTCString() + ' ' + _ip + ' -> ok');
            yield next;
        } else {
            console.log((new Date).toUTCString() + ' ' + _ip + ' -> no');
            _this.status=403;
            _this.body=conf.msg||MSG;
        }
    }
}

module.exports = ip;