Fix bwcli /bans command

This commit is contained in:
Théophile Diot 2023-01-25 17:08:56 +01:00
parent df7bbb9606
commit 5739144e3d
No known key found for this signature in database
GPG Key ID: E752C80DB72BB014
3 changed files with 34 additions and 2 deletions

View File

@ -161,6 +161,12 @@ api.do_api_call = function(self)
if status ~= ngx.HTTP_OK then
ret = false
end
if (#resp["msg"] == 0) then
resp["msg"] = ""
elseif (type(resp["msg"]) == "table") then
resp["data"] = resp["msg"]
resp["msg"] = resp["status"]
end
return ret, resp["msg"], status, cjson.encode(resp)
end
end

View File

@ -7,6 +7,26 @@ from ApiCaller import ApiCaller
from API import API
def format_remaining_time(seconds):
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
time_parts = []
if days > 0:
time_parts.append(f"{int(days)} day{'' if days == 1 else 's'}")
if hours > 0:
time_parts.append(f"{int(hours)} hour{'' if hours == 1 else 's'}")
if minutes > 0:
time_parts.append(f"{int(minutes)} minute{'' if minutes == 1 else 's'}")
if seconds > 0:
time_parts.append(f"{seconds:.2f} second{'' if seconds == 1 else 's'}")
if len(time_parts) > 1:
time_parts[-1] = f"and {time_parts[-1]}"
return " ".join(time_parts)
class CLI(ApiCaller):
def __init__(self):
self.__variables = dotenv_values("/etc/nginx/variables.env")
@ -126,9 +146,13 @@ class CLI(ApiCaller):
def bans(self):
ret, resp = self._send_to_apis("GET", "/bans", response=True)
if ret:
bans = resp["bans"]
bans = resp.get("data", [])
if len(bans) == 0:
return True, "No ban found"
cli_str = "List of bans :\n"
for ban in bans:
cli_str += f"- {ban['ip']} for {ban['exp']}s : {ban['reason']}\n"
cli_str += f"- {ban['ip']} for {format_remaining_time(ban['exp'])} : {ban.get('reason', 'no reason given')}\n"
return True, cli_str
return False, "error"

View File

@ -125,6 +125,8 @@ class ApiCaller:
)
if response:
if isinstance(resp, dict):
return ret, resp
return ret, resp.json()
return ret