diff --git a/src/bw/lua/api.lua b/src/bw/lua/api.lua index 5a25c0faa..dfe9da3c8 100644 --- a/src/bw/lua/api.lua +++ b/src/bw/lua/api.lua @@ -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 diff --git a/src/common/cli/CLI.py b/src/common/cli/CLI.py index 736506ee2..36342d8c1 100644 --- a/src/common/cli/CLI.py +++ b/src/common/cli/CLI.py @@ -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" diff --git a/src/common/utils/ApiCaller.py b/src/common/utils/ApiCaller.py index f54216d91..427821d5d 100644 --- a/src/common/utils/ApiCaller.py +++ b/src/common/utils/ApiCaller.py @@ -125,6 +125,8 @@ class ApiCaller: ) if response: + if isinstance(resp, dict): + return ret, resp return ret, resp.json() return ret