1 from fastapi import FastAPI, Request, HTTPException, responses, Query
2 from fastapi.templating import Jinja2Templates
3 from requests import get
4 from datetime import datetime
5 from email import utils
12 app = FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc")
13 templates = Jinja2Templates(directory=".")
15 @app.get(fba.config["base_url"] + "/info")
17 fba.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)")
18 known, indexed, blocks = fba.c.fetchone()
21 "known_instances": known,
22 "indexed_instances": indexed,
23 "blocks_recorded": blocks,
24 "slogan": fba.config["slogan"]
27 @app.get(fba.config["base_url"] + "/top")
28 def top(blocked: int = None, blockers: int = None):
29 if blocked == None and blockers == None:
30 raise HTTPException(status_code=400, detail="No filter specified")
33 raise HTTPException(status_code=400, detail="Too many results")
34 fba.c.execute("SELECT blocked, count(blocked) FROM blocks WHERE block_level = 'reject' group by blocked ORDER BY count(blocked) DESC LIMIT ?", (blocked,))
35 elif blockers != None:
37 raise HTTPException(status_code=400, detail="Too many results")
38 fba.c.execute("SELECT blocker, count(blocker) FROM blocks WHERE block_level = 'reject' group by blocker ORDER BY count(blocker) DESC LIMIT ?", (blockers,))
39 scores = fba.c.fetchall()
43 for domain, highscore in scores:
44 scoreboard.append({"domain": domain, "highscore": highscore})
48 @app.get(fba.config["base_url"] + "/api")
49 def blocked(domain: str = None, reason: str = None, reverse: str = None):
50 if domain == None and reason == None and reverse == None:
51 raise HTTPException(status_code=400, detail="No filter specified")
53 reason = re.sub("(%|_)", "", reason)
55 raise HTTPException(status_code=400, detail="Keyword is shorter than three characters")
57 wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
58 punycode = domain.encode('idna').decode('utf-8')
59 fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? ORDER BY first_added ASC",
60 (domain, "*." + domain, wildchar, fba.get_hash(domain), punycode, "*." + punycode))
62 fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE blocker = ? ORDER BY first_added ASC", (reverse,))
64 fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE reason like ? AND reason != '' ORDER BY first_added ASC", ("%"+reason+"%",))
65 blocks = fba.c.fetchall()
68 for blocker, blocked, block_level, reason, first_added, last_seen in blocks:
73 "first_added": first_added,
74 "last_seen": last_seen
76 if block_level in result:
77 result[block_level].append(entry)
79 result[block_level] = [entry]
83 @app.get(fba.config["base_url"] + "/scoreboard")
84 def index(request: Request, blockers: int = None, blocked: int = None):
85 if blockers == None and blocked == None:
86 raise HTTPException(status_code=400, detail="No filter specified")
87 elif blockers != None:
88 scores = get(f"http://127.0.0.1:{fba.config['base_url']}{fba.config['base_url']}/top?blockers={blockers}")
90 scores = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blocked={blocked}")
93 raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
94 scores = scores.json()
95 return templates.TemplateResponse("index.html", {"request": request, "scoreboard": True, "blockers": blockers, "blocked": blocked, "scores": scores})
97 @app.get(fba.config["base_url"] + "/")
98 def index(request: Request, domain: str = None, reason: str = None, reverse: str = None):
99 if domain == "" or reason == "" or reverse == "":
100 return responses.RedirectResponse("/")
103 if domain == None and reason == None and reverse == None:
104 info = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/info")
106 raise HTTPException(status_code=info.status_code, detail=info.text)
109 blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}")
111 blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}")
112 elif reverse != None:
113 blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}")
116 raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
117 blocks = blocks.json()
118 for block_level in blocks:
119 for block in blocks[block_level]:
120 block["first_added"] = datetime.utcfromtimestamp(block["first_added"]).strftime('%Y-%m-%d %H:%M')
121 block["last_seen"] = datetime.utcfromtimestamp(block["last_seen"]).strftime('%Y-%m-%d %H:%M')
123 return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "reverse": reverse, "info": info})
125 @app.get(fba.config["base_url"] + "/api/mutual")
126 def mutual(domains: list[str] = Query()):
127 """Return 200 if federation is open between the two, 4xx otherwise"""
129 "SELECT block_level FROM blocks " \
130 "WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a OR blocked = :aw OR blocked = :bw)) " \
131 "AND block_level = 'reject' " \
136 "aw": "*." + domains[0],
137 "bw": "*." + domains[1],
140 res = fba.c.fetchone()
144 return responses.JSONResponse(status_code=418, content={})
147 return responses.JSONResponse(status_code=200, content={})
149 @app.get(fba.config["base_url"] + "/rss")
150 def rss(request: Request, domain: str = None):
152 wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
153 punycode = domain.encode('idna').decode('utf-8')
154 fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? ORDER BY first_added DESC LIMIT 50",
155 (domain, "*." + domain, wildchar, fba.get_hash(domain), punycode, "*." + punycode))
157 fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks ORDER BY first_added DESC LIMIT 50")
159 blocks = fba.c.fetchall()
162 for blocker, blocked, block_level, reason, first_added, last_seen in blocks:
163 first_added = utils.format_datetime(datetime.fromtimestamp(first_added))
164 if reason == None or reason == '':
165 reason = "No reason provided."
167 reason = "Provided reason: '" + reason + "'"
172 "block_level": block_level,
174 "first_added": first_added
177 timestamp = utils.format_datetime(datetime.now())
179 return templates.TemplateResponse("rss.xml", {
181 "timestamp": timestamp,
185 "Content-Type": "application/rss+xml"
188 if __name__ == "__main__":
189 uvicorn.run("api:app", host="127.0.0.1", port=fba.config["port"], log_level=fba.config["log_level"])