]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 29 Apr 2025 18:04:36 +0000 (20:04 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 29 Apr 2025 18:04:36 +0000 (20:04 +0200)
- added more parameter checks
- renamed `domain` to `data` and anything not `score` to it (scoreboard view)

daemon.py
fba.py
fba/models/instances.py
fba/utils.py
templates/views/scoreboard.html

index e5bb81f9f4c10553fb1591d2f6394de60e2d968a..f007e726b6bbda6a3e6a1ab58493b0b2e9985190 100755 (executable)
--- 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 d131b8aed529542c2d1008108c0e4343da0ed0fb..42b9f06c4130b34c5558af80e31df5a960e7bdd8 100755 (executable)
--- a/fba.py
+++ b/fba.py
 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)
index a5270382a15670b70208e96b322a2c6fdbd20cd4..19572f3752c2769385cbf74dd4abe94dd3ea0eb4 100644 (file)
@@ -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()
index 4c7440ad9aeb151d95cafb73239bef394206a69d..d4569a661e43e5fe1c34d3b8384369910aa7aea7 100644 (file)
@@ -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)
 
index 1cc68b7bf487e737b7c383c2a31c522802427213..f6361227ee7514df5ccd83a4a23725eb41e06cd2 100644 (file)
             </thead>
 
             <tbody>
-            {% for entry in scores %}
+            {% for row in scores %}
                 <tr>
                     <td>{{loop.index}}</td>
                     <td>
-                        {% if entry['domain'] == None %}
+                        {% if row['data'] == None %}
                             -
                         {% elif mode in ('error_code', 'obfuscation') %}
-                            {{entry['domain']}}
+                            {{row['data']}}
                         {% elif mode == 'block_level' %}
-                            <a href="top?mode={{mode}}&amp;value={{entry['domain']}}&amp;amount=50">{{entry['domain']}}</a>
+                            <a href="top?mode={{mode}}&amp;value={{row['data']}}&amp;amount=50">{{row['data']}}</a>
                         {% elif mode in ('avg_peers', 'avg_blocks', 'obfuscator') %}
-                            <a href="list?mode=software&amp;value={{entry['domain']}}&amp;amount=50">{{entry['domain']}}</a>
+                            <a href="list?mode=software&amp;value={{row['data']}}&amp;amount=50">{{row['data']}}</a>
                         {% elif mode in ('original_software', 'software', 'detection_mode', 'command') %}
-                            <a href="list?mode={{mode}}&amp;value={{entry['domain']}}&amp;amount=50">{{entry['domain']}}</a>
+                            <a href="list?mode={{mode}}&amp;value={{row['data']}}&amp;amount=50">{{row['data']}}</a>
                         {% else %}
-                            {% with domain=entry['domain'] %}
+                            {% with domain=row['data'] %}
                             {% include "widgets/links.html" %}
                             {% endwith %}
                         {% endif %}
                     </td>
-                    <td>{{entry['score']}}</td>
+                    <td>{{row['score']}}</td>
                 </tr>
             {% endfor %}
             </tbody>