Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/plugins/crowdsec/stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ end
--- Parse a golang duration string and return the number of seconds
--- @param duration string: the duration string to parse
--- @return number: the number of seconds
--- @return string: the error message if any
--- @return string: the error message or nil if no error
local function parse_duration(duration)
local match, err = ngx.re.match(duration, "^((?<hours>[0-9]+)h)?((?<minutes>[0-9]+)m)?(?<seconds>[0-9]+)")
local match, err = ngx.re.match(duration, "^((?<hours>[0-9]+)h)?((?<minutes>[0-9]+)m)?((?<seconds>[0-9]+)s)?((?<milliseconds>[0-9]+)ms)?((?<microseconds>[0-9]+)(µ|u)s)?((?<nanoseconds>[0-9]+)ns)?$")
local ttl = 0
if not match then
if not match or err then
if err then
return ttl, err
ngx.log(ngx.ERR, "Error while parsing duration: " .. err)
end
return ttl, err
end
if match["hours"] ~= nil and match["hours"] ~= false then
local hours = tonumber(match["hours"])
Expand All @@ -89,6 +90,11 @@ local function parse_duration(duration)
local seconds = tonumber(match["seconds"])
ttl = ttl + seconds
end
if match["milliseconds"] ~= nil and match["milliseconds"] ~= false then
local milliseconds = tonumber(match["milliseconds"])
ttl = ttl + (milliseconds / 1000)
end
--- microseconds and nanoseconds are ignored as they are too small to be useful in this context
return ttl, nil
end

Expand Down Expand Up @@ -213,7 +219,8 @@ function stream:stream_query(api_url, timeout, api_key_header, api_key, user_age
decision.origin = "lists:" .. decision.scenario
end
if bouncing_on_type == decision.type or bouncing_on_type == "all" then
local ttl, err = parse_duration(decision.duration)
local ttl
ttl, err = parse_duration(decision.duration)
if err ~= nil then
ngx.log(ngx.ERR, "[Crowdsec] failed to parse ban duration '" .. decision.duration .. "' : " .. err)
end
Expand Down