From f725d0fe63f5b2b4a08e1534588d4bbf7a33eea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Diot?= Date: Fri, 19 May 2023 16:08:55 -0400 Subject: [PATCH] Update keys name in datastore --- src/common/core/badbehavior/badbehavior.lua | 5 +++-- src/common/core/country/country.lua | 4 ++-- src/common/core/limit/limit.lua | 10 +++++----- src/common/core/reversescan/reversescan.lua | 10 +++++----- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/common/core/badbehavior/badbehavior.lua b/src/common/core/badbehavior/badbehavior.lua index aa6a547c..67277f93 100644 --- a/src/common/core/badbehavior/badbehavior.lua +++ b/src/common/core/badbehavior/badbehavior.lua @@ -174,7 +174,8 @@ function badbehavior.redis_increase(ip, count_time, ban_time) return false, err end -- Execute LUA script - local counter, err = clusterstore:call("eval", redis_script, 2, "bad_behavior_" .. ip, "bans_ip" .. ip, count_time, + local counter, err = clusterstore:call("eval", redis_script, 2, "plugin_bad_behavior_" .. ip, "bans_ip" .. ip, + count_time, ban_time) if not counter then clusterstore:close() @@ -214,7 +215,7 @@ function badbehavior.redis_decrease(ip, count_time) if not ok then return false, err end - local counter, err = clusterstore:call("eval", redis_script, 1, "bad_behavior_" .. ip, count_time) + local counter, err = clusterstore:call("eval", redis_script, 1, "plugin_bad_behavior_" .. ip, count_time) if not counter then clusterstore:close() return false, err diff --git a/src/common/core/country/country.lua b/src/common/core/country/country.lua index caefbedd..f02cb351 100644 --- a/src/common/core/country/country.lua +++ b/src/common/core/country/country.lua @@ -98,7 +98,7 @@ function country:preread() end function country:is_in_cache(ip) - local ok, data = self.cachestore:get("plugin_country_cache_" .. ngx.ctx.bw.server_name .. ip) + local ok, data = self.cachestore:get("plugin_country_" .. ngx.ctx.bw.server_name .. ip) if not ok then return false, data end @@ -106,7 +106,7 @@ function country:is_in_cache(ip) end function country:add_to_cache(ip, country, result) - local ok, err = self.cachestore:set("plugin_country_cache_" .. ngx.ctx.bw.server_name .. ip, + local ok, err = self.cachestore:set("plugin_country_" .. ngx.ctx.bw.server_name .. ip, cjson.encode({ country = country, result = result }), 86400) if not ok then return false, err diff --git a/src/common/core/limit/limit.lua b/src/common/core/limit/limit.lua index a996c579..6621a819 100644 --- a/src/common/core/limit/limit.lua +++ b/src/common/core/limit/limit.lua @@ -154,7 +154,7 @@ function limit:limit_req(rate_max, rate_time) timestamps = redis_timestamps -- Save the new timestamps local ok, err = self.datastore:set( - "plugin_limit_cache_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, + "plugin_limit_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, cjson.encode(timestamps), delay) if not ok then return nil, "can't update timestamps : " .. err @@ -177,8 +177,8 @@ end function limit:limit_req_local(rate_max, rate_time) -- Get timestamps - local timestamps, err = self.datastore:get("plugin_limit_cache_" .. - ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri) + local timestamps, err = self.datastore:get("plugin_limit_" .. + ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri) if not timestamps and err ~= "not found" then return nil, err elseif err == "not found" then @@ -190,7 +190,7 @@ function limit:limit_req_local(rate_max, rate_time) -- Save new timestamps if needed if updated then local ok, err = self.datastore:set( - "plugin_limit_cache_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, + "plugin_limit_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, cjson.encode(new_timestamps), delay) if not ok then return nil, err @@ -256,7 +256,7 @@ function limit:limit_req_redis(rate_max, rate_time) end -- Execute script local timestamps, err = self.clusterstore:call("eval", redis_script, 1, - "limit_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, rate_max, rate_time, + "plugin_limit_" .. ngx.ctx.bw.server_name .. ngx.ctx.bw.remote_addr .. ngx.ctx.bw.uri, rate_max, rate_time, os.time(os.date("!*t"))) if not timestamps then self.clusterstore:close() diff --git a/src/common/core/reversescan/reversescan.lua b/src/common/core/reversescan/reversescan.lua index c71a240e..11a5dd2d 100644 --- a/src/common/core/reversescan/reversescan.lua +++ b/src/common/core/reversescan/reversescan.lua @@ -34,7 +34,7 @@ function reversescan:access() utils.get_deny_status()) elseif not cached then -- Do the scan - local res, err = self:scan(ngx.ctx.bw.remote_addr, tonumber(port), + local res = self:scan(ngx.ctx.bw.remote_addr, tonumber(port), tonumber(self.variables["REVERSE_SCAN_TIMEOUT"])) -- Cache the result local ok, err = self:add_to_cache(ngx.ctx.bw.remote_addr .. ":" .. port, res) @@ -62,13 +62,13 @@ function reversescan:scan(ip, port, timeout) local ok, err = tcpsock:connect(ip, port) tcpsock:close() if not ok then - return "close", err + return "close" end - return "open", nil + return "open" end function reversescan:is_in_cache(ip_port) - local ok, data = self.cachestore:get("plugin_reversescan_cache_" .. ip_port) + local ok, data = self.cachestore:get("plugin_reverse_scan_" .. ip_port) if not ok then return false, data end @@ -76,7 +76,7 @@ function reversescan:is_in_cache(ip_port) end function reversescan:add_to_cache(ip_port, value) - local ok, err = self.cachestore:set("plugin_reversescan_cache_" .. ip_port, value, 86400) + local ok, err = self.cachestore:set("plugin_reverse_scan_" .. ip_port, value, 86400) if not ok then return false, err end