local host = "192.168.16.2"
local port = 6379

function getClientIp()
    local headers = ngx.req.get_headers()
    local ip = headers["X-Real-IP"]
    if ip == nil then
        ip = headers["X-Forwarded-For"]
    end
    if ip == "unknown" or ip == nil then
        ip = ngx.var.remote_addr
    end
    if ip == nil then
        ip = "0.0.0.0"
    end

    return ip
end

function ipDeny()
    local ip = getClientIp()
    local redis = require "resty.redis"
    local red = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect(host, port)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    isDeny, err = red:sismember("ip_blacklist", ip)

    return isDeny
end

function countQueue()
    local redis = require "resty.redis"
    local red = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect(host, port)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    count, err = red:incr("num")

    return count
end

function read(filename)
    local path = "/etc/nginx/conf.d"
    local file = io.open(path..'/'..filename,"r")
    if file == nil then
        return
    end 

    t = {}
    for line in file:lines() do
        table.insert(t,line)
    end 
    file:close()

    return t
end

function uaFilter()
    local rules = read("user-agent")
    local ua = ngx.var.http_user_agent

    if ua == nil then
        ngx.exit(403)
    end 

    for _, rule in pairs(rules) do
        if rule ~= "" and ngx.re.match(ua,rule,"isjo") then
            ngx.exit(403)
        end
    end

    return
end

标签: none

已有 8 条评论

  1. 叼茂SEO.bfbikes.com

  2. 怎么收藏这篇文章?

  3. 想想你的文章写的特别好https://www.237fa.com/

  4. 看的我热血沸腾啊https://www.ea55.com/

  5. 兄弟写的非常好 https://www.cscnn.com/

  6. 《大爸爸,小爸爸和其它故事》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/69254.html

  7. 《罗丹2017》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/165441.html

  8. 《馗降:粽邪2》恐怖片高清在线免费观看:https://www.jgz518.com/xingkong/78914.html

添加新评论