mirror of
https://github.com/bunkerity/bunkerweb
synced 2026-05-24 09:28:37 +00:00
Add back-end logic for web UI reports page
This commit is contained in:
parent
3dda5b19de
commit
0c0c221f61
3 changed files with 31 additions and 40 deletions
|
|
@ -1600,47 +1600,15 @@ def logs_container(container_id):
|
|||
@app.route("/reports", methods=["GET"])
|
||||
@login_required
|
||||
def reports():
|
||||
# TODO : Get block requests from database to send it
|
||||
reports = [
|
||||
{
|
||||
"user_agent": "Version 0.6.1 - Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1",
|
||||
"ip": "124.0.0.1",
|
||||
"country": "FR",
|
||||
"url": "/test",
|
||||
"date": "12/51/9851",
|
||||
"reason": "antibot",
|
||||
"method": "GET",
|
||||
"status": 403,
|
||||
"data": "{fesfmk fesfsf sfesfes}",
|
||||
},
|
||||
{
|
||||
"user_agent": "Version 0.6.1 - Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1",
|
||||
"ip": "124.0.0.2",
|
||||
"country": "EN",
|
||||
"url": "/test",
|
||||
"date": "12/51/9851",
|
||||
"reason": "test",
|
||||
"method": "GET",
|
||||
"status": 403,
|
||||
"data": "{fesfmk fesfsf sfesfes}",
|
||||
},
|
||||
{
|
||||
"user_agent": "Version 0.6.1 - Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1",
|
||||
"ip": "124.0.0.3",
|
||||
"country": "ES",
|
||||
"url": "/test",
|
||||
"date": "12/51/9851",
|
||||
"reason": "antibot",
|
||||
"method": "GET",
|
||||
"status": 403,
|
||||
"data": "{fesfmk fesfsf sfesfes}",
|
||||
},
|
||||
]
|
||||
reports = app.config["INSTANCES"].get_reports()
|
||||
total_reports = len(reports)
|
||||
reports = reports[:100]
|
||||
|
||||
# Prepare data
|
||||
reasons = {}
|
||||
codes = {}
|
||||
for report in reports:
|
||||
for i, report in enumerate(deepcopy(reports)):
|
||||
reports[i]["date"] = datetime.fromtimestamp(floor(reports[i]["date"])).strftime("%d/%m/%Y %H:%M:%S")
|
||||
# Get top reasons
|
||||
if not report["reason"] in reasons:
|
||||
reasons[report["reason"]] = 0
|
||||
|
|
@ -1650,12 +1618,13 @@ def reports():
|
|||
codes[report["status"]] = 0
|
||||
codes[report["status"]] = codes[report["status"]] + 1
|
||||
|
||||
top_reason = [k for k, v in reasons.items() if v == max(reasons.values())][0]
|
||||
top_code = [k for k, v in codes.items() if v == max(codes.values())][0]
|
||||
top_reason = ([k for k, v in reasons.items() if v == max(reasons.values())] or [""])[0]
|
||||
top_code = ([k for k, v in codes.items() if v == max(codes.values())] or [""])[0]
|
||||
|
||||
return render_template(
|
||||
"reports.html",
|
||||
reports=reports,
|
||||
total_reports=total_reports,
|
||||
top_code=top_code,
|
||||
top_reason=top_reason,
|
||||
username=current_user.get_id(),
|
||||
|
|
|
|||
|
|
@ -115,6 +115,9 @@ class Instance:
|
|||
def unban(self, ip: str) -> bool:
|
||||
return self.apiCaller.send_to_apis("POST", "/unban", data={"ip": ip})
|
||||
|
||||
def reports(self) -> Tuple[bool, dict[str, Any]]:
|
||||
return self.apiCaller.send_to_apis("GET", "/metrics/requests", response=True)
|
||||
|
||||
|
||||
class Instances:
|
||||
def __init__(self, docker_client, kubernetes_client, integration: str):
|
||||
|
|
@ -342,3 +345,22 @@ class Instances:
|
|||
return f"Can't unban {ip} on {instance.name}"
|
||||
|
||||
return [instance.name for instance in self.get_instances() if not instance.unban(ip)]
|
||||
|
||||
def get_reports(self, _id: Optional[int] = None) -> List[dict[str, Any]]:
|
||||
if _id:
|
||||
instance = self.__instance_from_id(_id)
|
||||
resp, instance_reports = instance.reports()
|
||||
if not resp:
|
||||
return []
|
||||
return instance_reports[instance.name].get("msg", [])
|
||||
|
||||
reports: List[dict[str, Any]] = []
|
||||
for instance in self.get_instances():
|
||||
resp, instance_reports = instance.reports()
|
||||
if not resp:
|
||||
continue
|
||||
reports.extend(instance_reports[instance.name].get("msg", []))
|
||||
|
||||
reports.sort(key=lambda x: x["date"], reverse=True)
|
||||
|
||||
return reports
|
||||
|
|
|
|||
2
src/ui/templates/reports.html
vendored
2
src/ui/templates/reports.html
vendored
|
|
@ -45,7 +45,7 @@ url_for(request.endpoint)[1:].split("/")[-1].strip() %}
|
|||
<p
|
||||
class="transition duration-300 ease-in-out pl-2 col-span-1 mb-0 font-sans text-sm font-semibold leading-normal uppercase dark:text-white dark:opacity-80"
|
||||
>
|
||||
{{reports|length}}
|
||||
{{ total_reports }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="mx-1 flex items-center my-4">
|
||||
|
|
|
|||
Loading…
Reference in a new issue