From 987dd2f14f540b93eded9b504adeab4ca034f799 Mon Sep 17 00:00:00 2001 From: Enju Aihara <9839590-EnjuAihara@users.noreply.gitlab.com> Date: Sat, 23 Apr 2022 15:06:31 +0200 Subject: [PATCH] added option to get blocks by reason --- api.py | 61 ++++++++++++++++++++++++++++++++++-------------------- index.html | 23 +++++++++++++++++++- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/api.py b/api.py index 5f0bb53..149dfd3 100644 --- a/api.py +++ b/api.py @@ -31,47 +31,62 @@ def info(): } @app.get(base_url+"/api") -def blocked(domain: str = None): - if domain == None: - raise HTTPException(status_code=400, detail="No domain specified") +def blocked(domain: str = None, reason: str = None): + if domain == None and reason == None: + raise HTTPException(status_code=400, detail="No filter specified") conn = sqlite3.connect("blocks.db") c = conn.cursor() - wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) - c.execute("select blocker, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ?", (domain, wildchar, get_hash(domain))) + if domain != None: + wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) + c.execute("select blocker, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ?", (domain, wildchar, get_hash(domain))) + else: + c.execute("select * from blocks where reason like ? and reason != ''", ("%"+reason+"%",)) blocks = c.fetchall() conn.close() result = {} reasons = {} + if domain != None: + for domain, block_level, reason in blocks: + if block_level in result: + result[block_level].append(domain) + else: + result[block_level] = [domain] + + if reason != "": + if block_level in reasons: + reasons[block_level][domain] = reason + else: + reasons[block_level] = {domain: reason} + return {"blocks": result, "reasons": reasons} - for domain, block_level, reason in blocks: + for blocker, blocked, reason, block_level in blocks: if block_level in result: - result[block_level].append(domain) + result[block_level].append({"blocker": blocker, "blocked": blocked, "reason": reason}) else: - result[block_level] = [domain] - - if reason != "": - if block_level in reasons: - reasons[block_level][domain] = reason - else: - reasons[block_level] = {domain: reason} - - return {"blocks": result, "reasons": reasons} + result[block_level] = [{"blocker": blocker, "blocked": blocked, "reason": reason}] + return {"blocks": result} @app.get(base_url+"/") -def index(request: Request, domain: str = None): - if domain == "": +def index(request: Request, domain: str = None, reason: str = None): + if domain == "" or reason == "": return responses.RedirectResponse("/") - blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}") info = None - if domain == None: + blocks = None + if domain == None and reason == None: info = get(f"http://127.0.0.1:{port}{base_url}/info") if not info.ok: raise HTTPException(status_code=info.status_code, detail=info.text) info = info.json() - if not blocks.ok: - raise HTTPException(status_code=blocks.status_code, detail=blocks.text) - return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks.json(), "info": info}) + elif domain != None: + blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}") + elif reason != None: + blocks = get(f"http://127.0.0.1:{port}{base_url}/api?reason={reason}") + if blocks != None: + if not blocks.ok: + raise HTTPException(status_code=blocks.status_code, detail=blocks.text) + blocks = blocks.json() + return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "info": info}) if __name__ == "__main__": uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info") diff --git a/index.html b/index.html index e548942..f5cde03 100644 --- a/index.html +++ b/index.html @@ -28,7 +28,23 @@ - {% if domain %} + {% if reason %} +

Instances that use "{{reason}}" in their Reason

+ {% for block_level in blocks.blocks %} +
+

{{block_level}} ({{blocks.blocks[block_level]|length}})

+ {% for block in blocks.blocks[block_level] %} +
+ + {{block.blocker}} -> + + {{block.blocked}}
+ {{block.reason}} +
+ {% endfor %} +
+ {% endfor %} + {% elif blocks %}

Instances that block {{domain}}

{% for block_level in blocks.blocks %}
@@ -50,6 +66,11 @@ +

Enter a Reason

+
+ + +
known instances: {{info.known_instances}}
indexed instances: {{info.indexed_instances}}
-- 2.39.2