local red = redis:new() function M:redis() red:set_timeout(1000) local ok,err = red:connect("127.0.0.1",6379) if not ok then ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end end
function M:check1() local time=os.time() --system time local res,err = red:get("block:"..ngx.var.remote_addr) if not res then -- redis error ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error end
if type(res) == "string" then --if red not null then type(red)==string if tonumber(res) >= tonumber(time) then ?--check if forbidden expired ngx.exit(ngx.HTTP_FORBIDDEN) --ngx.say("forbidden") end end }
接下来会做检测,是否访问频率过高,如果过高,要拉到黑名单的,
实现的方法是,检测user:127.0.0.1:time的值是否超标:
function M:check2() local time=os.time() --system time local res,err = red:get("user:"..ngx.var.remote_addr..":"..time) if not res then -- redis error ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error end
if type(res) == "string" then if tonumber(res) >= 10 then -- attack,10 times request/s red:del("block:"..self.ip) red:set("block:"..self.ip,tonumber(time)+5*60 ) --set block time ngx.exit(ngx.HTTP_FORBIDDEN) end end end
最后呢,还要记得,把每次访问时间做一个自增长,user:127.0.0.1:time:
function M:add() local time=os.time() --system time ok,err = red:incr("user:"..ngx.var.remote_addr..":"..time) if not ok then ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error end end