代码拉取完成,页面将自动刷新
同步操作将从 bukale/zhongkui-waf 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
-- Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
-- Copyright (c) 2023 bukale bukale2022@163.com
local cjson = require "cjson"
local config = require "config"
local redis_cli = require "redis_cli"
local isarray = require "table.isarray"
local sql = require "sql"
local utils = require "utils"
local constants = require "constants"
local md5 = ngx.md5
local pairs = pairs
local tonumber = tonumber
local cjson_decode = cjson.decode
local cjson_encode = cjson.encode
local dict_config = ngx.shared.dict_config
local dict_hits = ngx.shared.dict_config_rules_hits
local is_global_option_on = config.is_global_option_on
local is_system_option_on = config.is_system_option_on
local get_system_config = config.get_system_config
local prefix = "waf_rules_hits:"
local function sort(key_str, t)
for _, rt in pairs(t) do
local rule_md5 = md5(rt.rule)
local key = key_str .. '_' .. rule_md5
local key_total = key_str .. '_total_' .. rule_md5
local hits = nil
local totalHits = nil
if is_system_option_on("redis") then
hits = redis_cli.get(prefix .. key)
totalHits = redis_cli.get(prefix .. key_total)
else
hits = dict_hits:get(key)
totalHits = dict_hits:get(key_total)
end
rt.hits = tonumber(hits) or 0
rt.totalHits = tonumber(totalHits) or 0
end
table.sort(t, function(a, b)
if a.hits > b.hits then
return true
elseif a.hits == b.hits then
if a.totalHits > b.totalHits then
return true
end
end
return false
end)
return t
end
local sort_timer_handler = function(premature)
if premature then
return
end
local config_table = config.get_config_table()
if config_table then
for server_name, _ in pairs(config_table) do
local json = dict_config:get(server_name)
if json then
local security_modules = cjson_decode(json)
for _, module in pairs(security_modules) do
local rules = module.rules
if isarray(rules) then
rules = sort(server_name .. module['moduleName'], rules)
end
end
local json_new = cjson_encode(security_modules)
dict_config:set(server_name, json_new)
end
end
end
end
local get_rules_timer_handler = function(premature)
if premature then
return
end
local config_table = config.get_config_table()
if config_table then
for key, conf in pairs(config_table) do
local json = dict_config:get(key)
if json then
local security_modules = cjson_decode(json)
conf.security_modules = security_modules
end
end
end
end
if is_global_option_on("waf") then
local worker_id = ngx.worker.id()
if is_system_option_on('rulesSort') then
local delay = get_system_config('rulesSort').period
if worker_id == 0 then
utils.start_timer_every(delay, sort_timer_handler)
end
utils.start_timer_every(delay, get_rules_timer_handler)
end
if is_system_option_on("mysql") then
if worker_id == 0 then
utils.start_timer(0, sql.check_table)
utils.start_timer_every(2, sql.write_sql_queue_to_mysql, constants.KEY_ATTACK_LOG)
utils.start_timer_every(2, sql.write_sql_queue_to_mysql, constants.KEY_IP_BLOCK_LOG)
utils.start_timer_every(2, sql.update_waf_status)
utils.start_timer_every(2, sql.update_traffic_stats)
end
end
end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。