pull/216/head
midoks 3 years ago
parent 24bd8daca0
commit b8421d36ae
  1. 10
      plugins/op_waf/t/index.py
  2. 110
      plugins/op_waf/waf/lua/common.lua

@ -309,13 +309,13 @@ def test_Upload():
def test_start(): def test_start():
# test_OK() # test_OK()
test_Dir() # test_Dir()
# test_UA() # test_UA()
# test_Header() test_Header()
# test_UA_for(1000) # test_UA_for(1000)
# test_POST() test_POST()
# test_scan() test_scan()
test_CC() # test_CC()
# test_url_ext() # test_url_ext()
# test_cdn() # test_cdn()
# test_Upload() # test_Upload()

@ -66,20 +66,14 @@ function _M.cron(self)
self.clean_log() self.clean_log()
end end
ngx.timer.every(10, timer_every_get_data) ngx.timer.every(10, timer_every_get_data)
end
local timer_every_import_data = function (premature)
function _M.clean_log(self) local llen, _ = ngx.shared.waf_limit:llen('waf_limit_logs')
local db = self:initDB() if llen == 0 then
local now_date = os.date("*t") return true
local save_day = 90
local save_date_timestamp = os.time{year=now_date.year,
month=now_date.month, day=now_date.day-save_day, hour=0}
-- delete expire data
db:exec("DELETE FROM web_logs WHERE time<"..tostring(save_date_timestamp))
end end
function _M.log(self, args, rule_name, reason)
local db = self:initDB() local db = self:initDB()
local stmt2 = db:prepare[[INSERT INTO logs(time, ip, domain, server_name, method, status_code, uri, user_agent, rule_name, reason) local stmt2 = db:prepare[[INSERT INTO logs(time, ip, domain, server_name, method, status_code, uri, user_agent, rule_name, reason)
@ -87,35 +81,103 @@ function _M.log(self, args, rule_name, reason)
db:exec([[BEGIN TRANSACTION]]) db:exec([[BEGIN TRANSACTION]])
for i=1,llen do
local data, _ = ngx.shared.waf_limit:lpop('waf_limit_logs')
-- self:D("waf_limit_logs:"..data)
if not data then
break
end
local info = json.decode(data)
stmt2:bind_names{ stmt2:bind_names{
time=args["time"], time=info["time"],
ip=args["ip"], ip=info["ip"],
domain=args["server_name"], domain=info["server_name"],
server_name=args["server_name"], server_name=info["server_name"],
method=args["method"], method=info["method"],
status_code=args["status_code"], status_code=info["status_code"],
user_agent=args["user_agent"], user_agent=info["user_agent"],
uri=args["request_uri"], uri=info["request_uri"],
rule_name=rule_name, rule_name=info['rule_name'],
reason=reason reason=info['reason']
} }
local res, err = stmt2:step() local res, err = stmt2:step()
-- self:D("LOG[1]:"..tostring(res)..":"..tostring(err))
if tostring(res) == "5" then if tostring(res) == "5" then
self.D("waf the step database connection is busy, so it will be stored later.") self.D("waf the step database connection is busy, so it will be stored later.")
return false return false
end end
stmt2:reset() stmt2:reset()
end
local res, err = db:execute([[COMMIT]]) local res, err = db:execute([[COMMIT]])
-- self:D("LOG[2]:"..tostring(res)..":"..tostring(err))
if db and db:isopen() then if db and db:isopen() then
db:close() db:close()
end end
return true
end
ngx.timer.every(0.5, timer_every_import_data)
end end
function _M.clean_log(self)
local db = self:initDB()
local now_date = os.date("*t")
local save_day = 90
local save_date_timestamp = os.time{year=now_date.year,
month=now_date.month, day=now_date.day-save_day, hour=0}
-- delete expire data
db:exec("DELETE FROM web_logs WHERE time<"..tostring(save_date_timestamp))
end
function _M.log(self, args, rule_name, reason)
args["rule_name"] = rule_name
args["reason"] = reason
local push_data = json.encode(args)
-- self:D("push_data:"..push_data)
ngx.shared.waf_limit:rpush("waf_limit_logs", push_data)
-- local db = self:initDB()
-- local stmt2 = db:prepare[[INSERT INTO logs(time, ip, domain, server_name, method, status_code, uri, user_agent, rule_name, reason)
-- VALUES(:time, :ip, :domain, :server_name, :method, :status_code, :uri, :user_agent, :rule_name, :reason)]]
-- db:exec([[BEGIN TRANSACTION]])
-- stmt2:bind_names{
-- time=args["time"],
-- ip=args["ip"],
-- domain=args["server_name"],
-- server_name=args["server_name"],
-- method=args["method"],
-- status_code=args["status_code"],
-- user_agent=args["user_agent"],
-- uri=args["request_uri"],
-- rule_name=rule_name,
-- reason=reason
-- }
-- local res, err = stmt2:step()
-- -- self:D("LOG[1]:"..tostring(res)..":"..tostring(err))
-- if tostring(res) == "5" then
-- self.D("waf the step database connection is busy, so it will be stored later.")
-- return false
-- end
-- stmt2:reset()
-- local res, err = db:execute([[COMMIT]])
-- -- self:D("LOG[2]:"..tostring(res)..":"..tostring(err))
-- if db and db:isopen() then
-- db:close()
-- end
-- return true
end
function _M.setDebug(self, mode) function _M.setDebug(self, mode)
debug_mode = mode debug_mode = mode
end end

Loading…
Cancel
Save