Skip to content

Commit

Permalink
fix the startup
Browse files Browse the repository at this point in the history
  • Loading branch information
sabban committed Oct 16, 2024
1 parent 4e63a10 commit 87a5edf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ function csmod.GetCaptchaBackendKey()
end

local function SetupStream()
local function query_wrapper()
local err = stream:stream_query(
runtime.conf["API_URL"],
runtime.conf["REQUEST_TIMEOUT"],
REMEDIATION_API_KEY_HEADER,
runtime.conf["API_KEY"],
runtime.userAgent,
runtime.conf["SSL_VERIFY"],
runtime.conf["BOUNCING_ON_TYPE"]
)
if err ~=nil then
ngx.log(ngx.ERR, "Failed to query the stream: " .. err)
error("Failed to query the stream: " .. err)
end
end
-- if it stream mode and startup start timer
if runtime.conf["API_URL"] == "" then
return
Expand Down Expand Up @@ -253,7 +268,7 @@ local function SetupStream()
ngx.log(ngx.DEBUG, "timer started: " .. tostring(runtime.timer_started) .. " in worker " .. tostring(ngx.worker.id()))
if not runtime.timer_started then
local ok, err
ok, err = ngx.timer.at(runtime.conf["UPDATE_FREQUENCY"],SetupStream)
ok, err = ngx.timer.at(runtime.conf["UPDATE_FREQUENCY"],query_wrapper)
if not ok then
return true, nil, "Failed to create the timer: " .. (err or "unknown")
end
Expand Down Expand Up @@ -361,6 +376,7 @@ function csmod.allowIp(ip)
runtime.conf["SSL_VERIFY"],
runtime.conf["BOUNCING_ON_TYPE"]
)
-- debug: wip
ngx.log(ngx.DEBUG, "live_query: " .. ip .. " | " .. (ok and "not banned with" or "banned with") .. " | " .. tostring(remediation) .. " | " .. tostring(origin) .. " | " .. tostring(err))
if remediation ~= nil and remediation ~= "none" then
metrics:increment(origin,1)
Expand Down
5 changes: 2 additions & 3 deletions lib/plugins/crowdsec/stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ end
-- @param user_agent string: the user agent to use for the request
-- @param ssl_verify boolean: whether to verify the SSL certificate or not
-- @param bouncing_on_type string: the type of decision to bounce on
-- @return string: the error message if any
function stream:stream_query(api_url, timeout, api_key_header, api_key, user_agent, ssl_verify, bouncing_on_type)

-- As this function is running inside coroutine (with ngx.timer.at),
Expand All @@ -138,7 +137,7 @@ function stream:stream_query(api_url, timeout, api_key_header, api_key, user_age
ssl_verify)
if not res then
set_refreshing(false)
return "request failed: " .. err
ngx.log(ngx.ERR, "request failed: " .. err)
end

local succ, err, forcible = stream.cache:set("last_refresh", ngx.time())
Expand All @@ -156,7 +155,7 @@ function stream:stream_query(api_url, timeout, api_key_header, api_key, user_age

if status~=200 then
set_refreshing(false)
return "HTTP error while request to Local API '" .. status .. "' with message (" .. tostring(body) .. ")"
ngx.log(ngx.ERR, "HTTP error while request to Local API '" .. status .. "' with message (" .. tostring(body) .. ")")
end

local decisions = cjson.decode(body)
Expand Down

0 comments on commit 87a5edf

Please sign in to comment.