]> git.mxchange.org Git - fba.git/blob - api.py
051fca76c4ddc24d65f53d05815f74953e7a2434
[fba.git] / api.py
1 import uvicorn
2 from fastapi import FastAPI, Request, HTTPException, responses
3 import sqlite3
4 from hashlib import sha256
5 from fastapi.templating import Jinja2Templates
6 from requests import get
7 from json import loads
8 from re import sub
9
10 with open("config.json") as f:
11     config = loads(f.read())
12     base_url = config["base_url"]
13     port = config["port"]
14 app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc")
15 templates = Jinja2Templates(directory=".")
16
17 def get_hash(domain: str) -> str:
18     return sha256(domain.encode("utf-8")).hexdigest()
19
20 @app.get(base_url+"/info")
21 def info():
22     conn = sqlite3.connect("blocks.db")
23     c = conn.cursor()
24     c.execute("select (select count(domain) from instances), (select count(domain) from instances where software in ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica')), (select count(blocker) from blocks)")
25     known, indexed, blocks = c.fetchone()
26     c.close()
27     return {
28         "known_instances": known,
29         "indexed_instances": indexed,
30         "blocks_recorded": blocks
31     }
32
33 @app.get(base_url+"/api")
34 def blocked(domain: str = None, reason: str = None):
35     if domain == None and reason == None:
36         raise HTTPException(status_code=400, detail="No filter specified")
37     if reason != None:
38         reason = sub("(%|_)", "", reason)
39         if len(reason) < 3:
40             raise HTTPException(status_code=400, detail="Keyword is shorter than three characters")
41     conn = sqlite3.connect("blocks.db")
42     c = conn.cursor()
43     if domain != None:
44         wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
45         punycode = domain.encode('idna').decode('utf-8')
46         c.execute("select blocker, blocked, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ?",
47                   (domain, "*." + domain, wildchar, get_hash(domain), punycode, "*." + punycode))
48     else:
49         c.execute("select blocker, blocked, reason, block_level from blocks where reason like ? and reason != ''", ("%"+reason+"%",))
50     blocks = c.fetchall()
51     conn.close()
52
53     result = {}
54     reasons = {}
55     wildcards = []
56     if domain != None:
57         for domain, blocked, block_level, reason in blocks:
58             if block_level in result:
59                 result[block_level].append(domain)
60             else:
61                 result[block_level] = [domain]
62             if blocked == "*." + ".".join(blocked.split(".")[-blocked.count("."):]):
63                 wildcards.append(domain)
64             if reason != "":
65                 if block_level in reasons:
66                     reasons[block_level][domain] = reason
67                 else:
68                     reasons[block_level] = {domain: reason}
69         return {"blocks": result, "reasons": reasons, "wildcards": wildcards}
70
71     for blocker, blocked, reason, block_level in blocks:
72         if block_level in result:
73             result[block_level].append({"blocker": blocker, "blocked": blocked, "reason": reason})
74         else:
75             result[block_level] = [{"blocker": blocker, "blocked": blocked, "reason": reason}]
76     return {"blocks": result}
77
78 @app.get(base_url+"/")
79 def index(request: Request, domain: str = None, reason: str = None):
80     if domain == "" or reason == "":
81         return responses.RedirectResponse("/")
82     info = None
83     blocks = None
84     if domain == None and reason == None:
85         info = get(f"http://127.0.0.1:{port}{base_url}/info")
86         if not info.ok:
87             raise HTTPException(status_code=info.status_code, detail=info.text)
88         info = info.json()
89     elif domain != None:
90         blocks = get(f"http://127.0.0.1:{port}{base_url}/api?domain={domain}")
91     elif reason != None:
92         blocks = get(f"http://127.0.0.1:{port}{base_url}/api?reason={reason}")
93     if blocks != None:
94         if not blocks.ok:
95             raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
96         blocks = blocks.json()
97     return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "info": info})
98
99 if __name__ == "__main__":
100     uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info")