diff --git a/lib/plugins/crowdsec/stream.lua b/lib/plugins/crowdsec/stream.lua index cc05d15..92dd2d7 100644 --- a/lib/plugins/crowdsec/stream.lua +++ b/lib/plugins/crowdsec/stream.lua @@ -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, "^((?[0-9]+)h)?((?[0-9]+)m)?(?[0-9]+)") + local match, err = ngx.re.match(duration, "^((?[0-9]+)h)?((?[0-9]+)m)?((?[0-9]+)s)?((?[0-9]+)ms)?((?[0-9]+)(ยต|u)s)?((?[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"]) @@ -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 @@ -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