From 603bce54f18e6f377bbd5ea7cdb4cf96dfe6ed31 Mon Sep 17 00:00:00 2001
From: Jordan Blasenhauer
Date: Fri, 2 Feb 2024 18:22:58 +0100
Subject: [PATCH] metric work + start update ui
*function execution calling app object working
*start updating core plugins component and key names for template.html and actions.py
---
src/common/core/antibot/ui/actions.py | 13 +-
src/common/core/antibot/ui/template.html | 4 +-
src/common/core/authbasic/ui/actions.py | 18 +-
src/common/core/badbehavior/ui/actions.py | 22 ++-
src/common/core/blacklist/ui/actions.py | 34 ++--
src/common/core/blacklist/ui/template.html | 10 +-
src/common/core/cors/ui/actions.py | 19 +-
src/common/core/cors/ui/template.html | 2 +-
src/common/core/country/ui/actions.py | 13 ++
src/common/core/country/ui/template.html | 53 +-----
src/common/core/dnsbl/ui/actions.py | 19 +-
src/common/core/dnsbl/ui/template.html | 83 ++++----
src/common/core/errors/ui/actions.py | 21 +-
src/common/core/greylist/ui/actions.py | 22 +--
src/common/core/greylist/ui/template.html | 211 ++-------------------
src/common/core/limit/ui/actions.py | 21 +-
src/common/core/misc/ui/actions.py | 23 ++-
src/common/core/misc/ui/template.html | 4 +-
src/common/core/reversescan/ui/actions.py | 25 +--
src/common/core/whitelist/ui/actions.py | 22 +--
src/common/core/whitelist/ui/template.html | 204 ++------------------
src/ui/main.py | 6 +-
src/ui/src/Instances.py | 6 +-
23 files changed, 254 insertions(+), 601 deletions(-)
diff --git a/src/common/core/antibot/ui/actions.py b/src/common/core/antibot/ui/actions.py
index be7c4748c..52b4f80f6 100644
--- a/src/common/core/antibot/ui/actions.py
+++ b/src/common/core/antibot/ui/actions.py
@@ -1,2 +1,11 @@
-def antibot():
- return {"counter_failed_challenges": 0}
+def antibot(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("antibot")
+
+ if data.get("counter_failed_challenges") is None:
+ data["counter_failed_challenges"] = 0
+
+ return data
+
+ except:
+ return {"counter_failed_challenges": 0}
diff --git a/src/common/core/antibot/ui/template.html b/src/common/core/antibot/ui/template.html
index 6cc57a3a8..338068f13 100644
--- a/src/common/core/antibot/ui/template.html
+++ b/src/common/core/antibot/ui/template.html
@@ -36,8 +36,8 @@
"unknown"
- total number
+ total failed
diff --git a/src/common/core/authbasic/ui/actions.py b/src/common/core/authbasic/ui/actions.py
index 64fd4d12f..f40e8fcbb 100644
--- a/src/common/core/authbasic/ui/actions.py
+++ b/src/common/core/authbasic/ui/actions.py
@@ -1,8 +1,10 @@
-def authbasic():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "count": 3,
- },
- }
+def authbasic(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("authbasic")
+
+ if data.get("counter_failed_challenges") is None:
+ data["counter_failed_challenges"] = 0
+
+ return data
+ except:
+ return {"counter_failed_challenges": 0}
diff --git a/src/common/core/badbehavior/ui/actions.py b/src/common/core/badbehavior/ui/actions.py
index 84a7b362e..648c1b38f 100644
--- a/src/common/core/badbehavior/ui/actions.py
+++ b/src/common/core/badbehavior/ui/actions.py
@@ -1,9 +1,13 @@
-def badbehavior():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "count": 3,
- "items": [{"code": 400, "count": 24}, {"code": 403, "count": 845}, {"code": 402, "count": 12}],
- },
- }
+def badbehavior(**kwargs):
+ try:
+ # Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
+ data = kwargs["app"].config["INSTANCES"].get_metrics("badbehavior")
+ format_data = []
+ # Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
+ for key, value in data.items():
+ format_data[key] = {"code": int(key.split("_")[1]), "count": value}
+
+ return {"items": format_data}
+
+ except:
+ return {"items": []}
diff --git a/src/common/core/blacklist/ui/actions.py b/src/common/core/blacklist/ui/actions.py
index 5b8b538a8..86a9feb87 100644
--- a/src/common/core/blacklist/ui/actions.py
+++ b/src/common/core/blacklist/ui/actions.py
@@ -1,11 +1,23 @@
-def blacklist():
- return {
- "message": "ok",
- "data": {
- "count_url": 4,
- "count_ip": 2,
- "count_rdns": 1,
- "count_asn": 10,
- "count_user_agent": 10,
- },
- }
+def blacklist(**kwargs):
+ keys = [
+ "counter_blacklist_url",
+ "counter_blacklist_ip",
+ "counter_blacklist_rdns",
+ "counter_blacklist_asn",
+ "counter_blacklist_usa",
+ ]
+
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("blacklist")
+
+ for key in keys:
+ if data.get(key) is None:
+ data[key] = 0
+
+ return data
+
+ except:
+ data = {}
+ for key in keys:
+ data[key] = 0
+ return data
diff --git a/src/common/core/blacklist/ui/template.html b/src/common/core/blacklist/ui/template.html
index 687dfdcbe..e2c75a2a7 100644
--- a/src/common/core/blacklist/ui/template.html
+++ b/src/common/core/blacklist/ui/template.html
@@ -235,27 +235,27 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
- count_url: {
+ counter_blacklist_url: {
el: document.querySelector("[data-count-url]"),
value: "unknown",
type: "text",
},
- count_ip: {
+ counter_blacklist_ip: {
el: document.querySelector("[data-count-ip]"),
value: "unknown",
type: "text",
},
- count_rdns: {
+ counter_blacklist_rdns: {
el: document.querySelector("[data-count-rdns]"),
value: "unknown",
type: "text",
},
- count_asn: {
+ counter_blacklist_asn: {
el: document.querySelector("[data-count-asn]"),
value: "unknown",
type: "text",
},
- count_user_agent: {
+ counter_blacklist_ua: {
el: document.querySelector("[data-count-user-agent]"),
value: "unknown",
type: "text",
diff --git a/src/common/core/cors/ui/actions.py b/src/common/core/cors/ui/actions.py
index ebc717152..13aab1b98 100644
--- a/src/common/core/cors/ui/actions.py
+++ b/src/common/core/cors/ui/actions.py
@@ -1,8 +1,11 @@
-def cors():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "count": 3,
- },
- }
+def cors(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("cors")
+
+ if data.get("counter_failed_cors") is None:
+ data["counter_failed_cors"] = 0
+
+ return data
+
+ except:
+ return {"counter_failed_cors": 0}
diff --git a/src/common/core/cors/ui/template.html b/src/common/core/cors/ui/template.html
index e9dfdc281..a71563da4 100644
--- a/src/common/core/cors/ui/template.html
+++ b/src/common/core/cors/ui/template.html
@@ -74,7 +74,7 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
- count: {
+ counter_failed_cors: {
el: document.querySelector("[data-count]"),
value: "unknown",
type: "text",
diff --git a/src/common/core/country/ui/actions.py b/src/common/core/country/ui/actions.py
index a9b64b967..1f3356eb4 100644
--- a/src/common/core/country/ui/actions.py
+++ b/src/common/core/country/ui/actions.py
@@ -7,3 +7,16 @@ def country():
"whitelist_count": 23,
},
}
+
+
+def country(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("country")
+
+ if data.get("counter_failed_country") is None:
+ data["counter_failed_country"] = 0
+
+ return data
+
+ except:
+ return {"counter_failed_country": 0}
diff --git a/src/common/core/country/ui/template.html b/src/common/core/country/ui/template.html
index 49ad3ccbb..e3a8c6e63 100644
--- a/src/common/core/country/ui/template.html
+++ b/src/common/core/country/ui/template.html
@@ -35,11 +35,11 @@
>
Country
-
+
blacklist request blockedrequest blocked
@@ -67,46 +67,6 @@
-
-
-
-
- Country
-
-
-
-
- whitelist request passed
-
-
-
-
-
-
-
-
diff --git a/src/common/core/errors/ui/actions.py b/src/common/core/errors/ui/actions.py
index 6b2da6100..4b7f885cf 100644
--- a/src/common/core/errors/ui/actions.py
+++ b/src/common/core/errors/ui/actions.py
@@ -1,8 +1,13 @@
-def errors():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "items": [{"count": 74, "code": "403"}, {"count": 82, "code": "404"}, {"count": "32", "code": "400"}],
- },
- }
+def errors(**kwargs):
+ try:
+ # Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
+ data = kwargs["app"].config["INSTANCES"].get_metrics("errors")
+ format_data = []
+ # Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
+ for key, value in data.items():
+ format_data[key] = {"code": int(key.split("_")[1]), "count": value}
+
+ return {"items": format_data}
+
+ except:
+ return {"items": []}
diff --git a/src/common/core/greylist/ui/actions.py b/src/common/core/greylist/ui/actions.py
index 603f75042..3bd3bb452 100644
--- a/src/common/core/greylist/ui/actions.py
+++ b/src/common/core/greylist/ui/actions.py
@@ -1,11 +1,11 @@
-def greylist():
- return {
- "message": "ok",
- "data": {
- "count_url": 4,
- "count_ip": 2,
- "count_rdns": 1,
- "count_asn": 10,
- "count_user_agent": 10,
- },
- }
+def greylist(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("greylist")
+
+ if data.get("counter_failed_greylist") is None:
+ data["counter_failed_greylist"] = 0
+
+ return data
+
+ except:
+ return {"counter_failed_greylist": 0}
diff --git a/src/common/core/greylist/ui/template.html b/src/common/core/greylist/ui/template.html
index 687dfdcbe..64d60f27f 100644
--- a/src/common/core/greylist/ui/template.html
+++ b/src/common/core/greylist/ui/template.html
@@ -24,7 +24,6 @@
-
@@ -33,194 +32,34 @@
- URL
+ GREYLIST
-
+
-
- denied
-
+ request blocked
-
-
-
-
-
-
-
- IP
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- RDNS
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- ASN
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- User Agent
-
-
-
-
-
- denied
-
-
-
-
-
-
@@ -235,28 +74,8 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
- count_url: {
- el: document.querySelector("[data-count-url]"),
- value: "unknown",
- type: "text",
- },
- count_ip: {
- el: document.querySelector("[data-count-ip]"),
- value: "unknown",
- type: "text",
- },
- count_rdns: {
- el: document.querySelector("[data-count-rdns]"),
- value: "unknown",
- type: "text",
- },
- count_asn: {
- el: document.querySelector("[data-count-asn]"),
- value: "unknown",
- type: "text",
- },
- count_user_agent: {
- el: document.querySelector("[data-count-user-agent]"),
+ counter_failed_greylist: {
+ el: document.querySelector("[data-count]"),
value: "unknown",
type: "text",
},
diff --git a/src/common/core/limit/ui/actions.py b/src/common/core/limit/ui/actions.py
index e61fd917f..04ab8a9c7 100644
--- a/src/common/core/limit/ui/actions.py
+++ b/src/common/core/limit/ui/actions.py
@@ -1,8 +1,13 @@
-def limit():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "items": [{"url": "http://www.example.com", "count": 24}, {"url": "http://www.example.com", "count": 24}],
- },
- }
+def limit(**kwargs):
+ try:
+ # Here we will have a list { 'limit_uri_url1': X, 'limit_uri_url2': Y ... }
+ data = kwargs["app"].config["INSTANCES"].get_metrics("limit")
+ format_data = []
+ # Format to fit [{url: "url1", count: X}, {url: "url2", count: Y} ...]
+ for key, value in data.items():
+ format_data[key] = {"url": key.replace("limit_uri_", ""), "count": value}
+
+ return {"items": format_data}
+
+ except:
+ return {"items": []}
diff --git a/src/common/core/misc/ui/actions.py b/src/common/core/misc/ui/actions.py
index e741e06d7..10598b407 100644
--- a/src/common/core/misc/ui/actions.py
+++ b/src/common/core/misc/ui/actions.py
@@ -1,9 +1,14 @@
-def misc():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "count_disabled_servers": 0,
- "count_disallowed_methods": 0,
- },
- }
+def misc(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("misc")
+
+ if "counter_failed_default" not in data:
+ data["counter_failed_default"] = 0
+
+ if "counter_failed_method" not in data:
+ data["counter_failed_method"] = 0
+
+ return data
+
+ except:
+ return {"counter_failed_default": 0, "counter_failed_method": 0}
diff --git a/src/common/core/misc/ui/template.html b/src/common/core/misc/ui/template.html
index 9e0a86eb9..a22092192 100644
--- a/src/common/core/misc/ui/template.html
+++ b/src/common/core/misc/ui/template.html
@@ -120,12 +120,12 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
- count_disabled_servers: {
+ counter_failed_default: {
el: document.querySelector("[data-count-server-disabled]"),
value: "unknown",
type: "text",
},
- count_disallowed_methods: {
+ counter_failed_method: {
el: document.querySelector("[data-count-disallowed-methods]"),
value: "unknown",
type: "text",
diff --git a/src/common/core/reversescan/ui/actions.py b/src/common/core/reversescan/ui/actions.py
index c91786f18..4243e8164 100644
--- a/src/common/core/reversescan/ui/actions.py
+++ b/src/common/core/reversescan/ui/actions.py
@@ -1,12 +1,13 @@
-def reversescan():
- return {
- "message": "ok",
- "data": {
- "info": "test",
- "items": [
- {"port": 4000, "count": 400},
- {"port": 4400, "count": 780},
- {"port": 5000, "count": 40},
- ],
- },
- }
+def reversescan(**kwargs):
+ try:
+ # Here we will have a list { 'counter_403': X, 'counter_401': Y ... }
+ data = kwargs["app"].config["INSTANCES"].get_metrics("reversescan")
+ format_data = []
+ # Format to fit [{code: 403, count: X}, {code: 401, count: Y} ...]
+ for key, value in data.items():
+ format_data[key] = {"port": int(key.split("_")[1]), "count": value}
+
+ return {"items": format_data}
+
+ except:
+ return {"items": []}
diff --git a/src/common/core/whitelist/ui/actions.py b/src/common/core/whitelist/ui/actions.py
index f828be3ac..19aeeb591 100644
--- a/src/common/core/whitelist/ui/actions.py
+++ b/src/common/core/whitelist/ui/actions.py
@@ -1,11 +1,11 @@
-def whitelist():
- return {
- "message": "ok",
- "data": {
- "count_url": 4,
- "count_ip": 2,
- "count_rdns": 1,
- "count_asn": 10,
- "count_user_agent": 10,
- },
- }
+def whitelist(**kwargs):
+ try:
+ data = kwargs["app"].config["INSTANCES"].get_metrics("whitelist")
+
+ if "counter_passed_whitelist" not in data:
+ data["counter_passed_whitelist"] = 0
+
+ return data
+
+ except:
+ return {"counter_passed_whitelist": 0}
diff --git a/src/common/core/whitelist/ui/template.html b/src/common/core/whitelist/ui/template.html
index 687dfdcbe..8d450ecd3 100644
--- a/src/common/core/whitelist/ui/template.html
+++ b/src/common/core/whitelist/ui/template.html
@@ -24,7 +24,6 @@
-
@@ -33,193 +32,32 @@
- URL
+ WHITELIST
-
+
-
- denied
-
+ request passed
+
-
-
-
-
-
-
-
- IP
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- RDNS
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- ASN
-
-
-
-
-
- denied
-
-
-
-
-
-
-
-
-
-
-
-
-
- User Agent
-
-
-
-
-
- denied
-
-
-
-
-
-
-
@@ -235,28 +73,8 @@
value: "{{ plugin['description'] or ''}}",
type: "text",
},
- count_url: {
- el: document.querySelector("[data-count-url]"),
- value: "unknown",
- type: "text",
- },
- count_ip: {
- el: document.querySelector("[data-count-ip]"),
- value: "unknown",
- type: "text",
- },
- count_rdns: {
- el: document.querySelector("[data-count-rdns]"),
- value: "unknown",
- type: "text",
- },
- count_asn: {
- el: document.querySelector("[data-count-asn]"),
- value: "unknown",
- type: "text",
- },
- count_user_agent: {
- el: document.querySelector("[data-count-user-agent]"),
+ counter_passed_whitelist: {
+ el: document.querySelector("[data-count]"),
value: "unknown",
type: "text",
},
diff --git a/src/ui/main.py b/src/ui/main.py
index 641a6be65..5bef5ef59 100755
--- a/src/ui/main.py
+++ b/src/ui/main.py
@@ -1317,11 +1317,11 @@ def custom_plugin(plugin: str):
# Try to get the custom plugin custom function and call it
method = getattr(actions, plugin)
if request.args:
- res = method(**request.args.to_dict())
+ res = method(app=app, args=request.args.to_dict())
elif request.is_json:
- res = method(**request.json)
+ res = method(app=app, args=request.json)
else:
- res = method()
+ res = method(app=app)
except AttributeError:
message = f'The plugin "{plugin}" does not have a "{plugin}" method, see logs for more details'
error = 404
diff --git a/src/ui/src/Instances.py b/src/ui/src/Instances.py
index 07db38cc0..0132b4b7d 100644
--- a/src/ui/src/Instances.py
+++ b/src/ui/src/Instances.py
@@ -375,7 +375,10 @@ class Instances:
# Get metrics from all instances
metrics = {}
for instance in self.get_instances():
- resp, instance_metrics = instance.metrics(plugin_id)
+ try:
+ resp, instance_metrics = instance.metrics(plugin_id)
+ except:
+ continue
# filters
if not resp:
@@ -419,5 +422,4 @@ class Instances:
if isinstance(v, str):
metrics[key][k] = v
continue
-
return metrics