From: Roland Häder Date: Tue, 29 Apr 2025 18:04:36 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=49c205fad8fac6b2dd5aca5e2c89b3263cd4100d;p=fba.git Continued: - added more parameter checks - renamed `domain` to `data` and anything not `score` to it (scoreboard view) --- diff --git a/daemon.py b/daemon.py index e5bb81f..f007e72 100755 --- a/daemon.py +++ b/daemon.py @@ -83,44 +83,48 @@ SELECT COUNT(domain) FROM instances) AS total_websites, \ @router.get(_base_url + "/api/scoreboard.json", response_class=JSONResponse) def api_scoreboard(mode: str, amount: int) -> None: - if amount > config.get("api_limit"): + if mode is None or value is None or amount is None: + raise HTTPException(status_code=500, detail="No filter specified") + elif amount <= 0: + raise HTTPException(status_code=500, detail=f"amount={amount} is to small") + elif amount > config.get("api_limit"): raise HTTPException(status_code=400, detail="Too many results") if mode == "blocked": - database.cursor.execute("SELECT blocked, COUNT(blocked) AS score FROM blocks GROUP BY blocked ORDER BY score DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT blocked AS data, COUNT(blocked) AS score FROM blocks GROUP BY blocked ORDER BY score DESC LIMIT ?", [amount]) elif mode == "blocker": - database.cursor.execute("SELECT blocker, COUNT(blocker) AS score FROM blocks GROUP BY blocker ORDER BY score DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT blocker AS data, COUNT(blocker) AS score FROM blocks GROUP BY blocker ORDER BY score DESC LIMIT ?", [amount]) elif mode == "reference": - database.cursor.execute("SELECT origin, COUNT(domain) AS score FROM instances WHERE origin IS NOT NULL GROUP BY origin ORDER BY score DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT origin AS data, COUNT(domain) AS score FROM instances WHERE origin IS NOT NULL GROUP BY origin ORDER BY score DESC LIMIT ?", [amount]) elif mode == "original_software": - database.cursor.execute("SELECT original_software, COUNT(domain) AS score FROM instances WHERE original_software IS NOT NULL GROUP BY original_software ORDER BY score DESC, original_software ASC LIMIT ?", [amount]) + database.cursor.execute("SELECT original_software AS data, COUNT(domain) AS score FROM instances WHERE original_software IS NOT NULL GROUP BY original_software ORDER BY score DESC, original_software ASC LIMIT ?", [amount]) elif mode == "software": - database.cursor.execute("SELECT software, COUNT(domain) AS score FROM instances WHERE software IS NOT NULL GROUP BY software ORDER BY score DESC, software ASC LIMIT ?", [amount]) + database.cursor.execute("SELECT software AS data, COUNT(domain) AS score FROM instances WHERE software IS NOT NULL GROUP BY software ORDER BY score DESC, software ASC LIMIT ?", [amount]) elif mode == "command": - database.cursor.execute("SELECT command, COUNT(domain) AS score FROM instances WHERE command IS NOT NULL GROUP BY command ORDER BY score DESC, command ASC LIMIT ?", [amount]) + database.cursor.execute("SELECT command AS data, COUNT(domain) AS score FROM instances WHERE command IS NOT NULL GROUP BY command ORDER BY score DESC, command ASC LIMIT ?", [amount]) elif mode == "error_code": - database.cursor.execute("SELECT last_status_code, COUNT(domain) AS score FROM instances WHERE last_status_code IS NOT NULL AND last_status_code != '200' GROUP BY last_status_code ORDER BY score DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT last_status_code AS data, COUNT(domain) AS score FROM instances WHERE last_status_code IS NOT NULL AND last_status_code != '200' GROUP BY last_status_code ORDER BY score DESC LIMIT ?", [amount]) elif mode == "detection_mode": - database.cursor.execute("SELECT detection_mode, COUNT(domain) AS cnt FROM instances GROUP BY detection_mode ORDER BY cnt DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT detection_mode AS data, COUNT(domain) AS score FROM instances GROUP BY detection_mode ORDER BY score DESC LIMIT ?", [amount]) elif mode == "avg_peers": - database.cursor.execute("SELECT software, AVG(total_peers) AS average FROM instances WHERE software IS NOT NULL AND total_peers IS NOT NULL GROUP BY software HAVING average > 0 ORDER BY average DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT software AS data, AVG(total_peers) AS score FROM instances WHERE software IS NOT NULL AND total_peers IS NOT NULL GROUP BY software HAVING average > 0 ORDER BY average DESC LIMIT ?", [amount]) elif mode == "avg_blocks": - database.cursor.execute("SELECT software, AVG(total_blocks) AS average FROM instances WHERE software IS NOT NULL AND total_blocks IS NOT NULL GROUP BY software HAVING average > 0 ORDER BY average DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT software AS data, AVG(total_blocks) AS score FROM instances WHERE software IS NOT NULL AND total_blocks IS NOT NULL GROUP BY software HAVING average > 0 ORDER BY average DESC LIMIT ?", [amount]) elif mode == "obfuscator": - database.cursor.execute("SELECT software, COUNT(domain) AS cnt FROM instances WHERE has_obfuscation = 1 GROUP BY software ORDER BY cnt DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT software AS data, COUNT(domain) AS score FROM instances WHERE has_obfuscation = 1 GROUP BY software ORDER BY score DESC LIMIT ?", [amount]) elif mode == "obfuscation": - database.cursor.execute("SELECT has_obfuscation, COUNT(domain) AS cnt FROM instances WHERE software IN ('pleroma', 'lemmy', 'mastodon', 'misskey', 'friendica') GROUP BY has_obfuscation ORDER BY cnt DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT has_obfuscation AS data, COUNT(domain) AS score FROM instances WHERE software IN ('pleroma', 'lemmy', 'mastodon', 'misskey', 'friendica') GROUP BY has_obfuscation ORDER BY score DESC LIMIT ?", [amount]) elif mode == "block_level": - database.cursor.execute("SELECT block_level, COUNT(rowid) AS cnt FROM blocks GROUP BY block_level ORDER BY cnt DESC LIMIT ?", [amount]) + database.cursor.execute("SELECT block_level AS data, COUNT(rowid) AS score FROM blocks GROUP BY block_level ORDER BY score DESC LIMIT ?", [amount]) else: - raise HTTPException(status_code=400, detail="No filter specified") + raise HTTPException(status_code=400, detail=f"mode='{mode}' is not supported") scores = [] for row in database.cursor.fetchall(): scores.append({ - "domain": row[0], - "score" : round(row[1]), + "data" : row["data"], + "score": round(row["score"]), }) return JSONResponse(status_code=200, content=scores) @@ -129,10 +133,12 @@ def api_scoreboard(mode: str, amount: int) -> None: def api_list(request: Request, mode: str, value: str, amount: int) -> None: if mode is None or value is None or amount is None: raise HTTPException(status_code=500, detail="No filter specified") + elif amount <= 0: + raise HTTPException(status_code=500, detail=f"amount={amount} is to small") elif amount > config.get("api_limit"): raise HTTPException(status_code=500, detail=f"amount={amount} is to big") - if mode in ("detection_mode", "original_software", "software", "command", "origin"): + if mode in ["detection_mode", "original_software", "software", "command", "origin"]: database.cursor.execute( f"SELECT * \ FROM instances \ @@ -164,6 +170,8 @@ LIMIT ?", [amount] def api_top(request: Request, mode: str, value: str, amount: int) -> None: if mode is None or value is None or amount is None: raise HTTPException(status_code=500, detail="No filter specified") + elif amount <= 0: + raise HTTPException(status_code=500, detail=f"amount={amount} is to small") elif amount > config.get("api_limit"): raise HTTPException(status_code=500, detail=f"amount={amount} is to big") @@ -195,7 +203,7 @@ LIMIT ?", domain, "*." + domain, wildchar, - utils.get_hash(domain), + utils.hash_domain(domain), punycode, "*." + punycode, amount @@ -210,7 +218,7 @@ LIMIT ?", [ domain, "*." + domain, wildchar, - utils.get_hash(domain), + utils.hash_domain(domain), punycode, "*." + punycode, amount @@ -262,11 +270,10 @@ def api_domain(domain: str) -> None: @router.get(_base_url + "/api/mutual.json", response_class=JSONResponse) def api_mutual(domains: list[str] = Query()) -> None: """Return 200 if federation is open between the two, 4xx otherwise""" - database.cursor.execute( - "SELECT block_level FROM blocks " \ - "WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a OR blocked = :aw OR blocked = :bw)) " \ - "AND block_level = 'reject' " \ - "LIMIT 1", + database.cursor.execute("SELECT block_level FROM blocks \ +WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a OR blocked = :aw OR blocked = :bw)) \ +AND block_level = 'reject' \ +LIMIT 1", { "a" : domains[0], "b" : domains[1], @@ -335,13 +342,12 @@ def nodeinfo_1_0(request: Request) -> None: @router.get(_base_url + "/api/v1/instance/domain_blocks", response_class=JSONResponse) def api_domain_blocks(request: Request) -> None: - blocked = blacklist.get_all() blocking = [] - for block in blocked: + for block in blacklist.get_all(): blocking.append({ "domain" : block, - "digest" : utils.get_hash(block), + "digest" : utils.hash_domain(block), "severity": "suspend", "comment" : blocked[block], }) @@ -505,7 +511,7 @@ ORDER BY first_seen DESC \ LIMIT ?", [ domain, "*." + domain, wildchar, - utils.get_hash(domain), + utils.hash_domain(domain), punycode, "*." + punycode, config.get("rss_limit") diff --git a/fba.py b/fba.py index d131b8a..42b9f06 100755 --- a/fba.py +++ b/fba.py @@ -20,14 +20,15 @@ import sys from fba import boot -# Init parser -boot.init_parser() +if __name__ == "__main__": + # Init parser + boot.init_parser() -# Run command -status = boot.run_command() + # Run command + status = boot.run_command() -# Shutdown again -boot.shutdown() + # Shutdown again + boot.shutdown() -# Exit with status code -sys.exit(status) + # Exit with status code + sys.exit(status) diff --git a/fba/models/instances.py b/fba/models/instances.py index a527038..19572f3 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -237,7 +237,7 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str domain, origin, command, - utils.get_hash(domain), + utils.hash_domain(domain), software, software, time.time() diff --git a/fba/utils.py b/fba/utils.py index 4c7440a..d4569a6 100644 --- a/fba/utils.py +++ b/fba/utils.py @@ -36,7 +36,7 @@ def is_primitive(var: any) -> bool: logger.debug("var[]='%s' - CALLED!", type(var)) return type(var) in [int, str, float, bool, None] or var is None -def get_hash(domain: str) -> str: +def hash_domain(domain: str) -> str: logger.debug("domain='%s' - CALLED!", domain) domain_helper.raise_on(domain) diff --git a/templates/views/scoreboard.html b/templates/views/scoreboard.html index 1cc68b7..f636122 100644 --- a/templates/views/scoreboard.html +++ b/templates/views/scoreboard.html @@ -73,27 +73,27 @@ - {% for entry in scores %} + {% for row in scores %} {{loop.index}} - {% if entry['domain'] == None %} + {% if row['data'] == None %} - {% elif mode in ('error_code', 'obfuscation') %} - {{entry['domain']}} + {{row['data']}} {% elif mode == 'block_level' %} - {{entry['domain']}} + {{row['data']}} {% elif mode in ('avg_peers', 'avg_blocks', 'obfuscator') %} - {{entry['domain']}} + {{row['data']}} {% elif mode in ('original_software', 'software', 'detection_mode', 'command') %} - {{entry['domain']}} + {{row['data']}} {% else %} - {% with domain=entry['domain'] %} + {% with domain=row['data'] %} {% include "widgets/links.html" %} {% endwith %} {% endif %} - {{entry['score']}} + {{row['score']}} {% endfor %}