+ elif domain != None:
+ blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}")
+ elif reason != None:
+ blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}")
+ elif reverse != None:
+ blocks = get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}")
+ if blocks != None:
+ if not blocks.ok:
+ raise HTTPException(status_code=blocks.status_code, detail=blocks.text)
+ blocks = blocks.json()
+ for block_level in blocks:
+ for block in blocks[block_level]:
+ block["first_added"] = datetime.utcfromtimestamp(block["first_added"]).strftime('%Y-%m-%d %H:%M')
+ block["last_seen"] = datetime.utcfromtimestamp(block["last_seen"]).strftime('%Y-%m-%d %H:%M')
+
+ return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "reverse": reverse, "info": info})
+
+@app.get(fba.config["base_url"] + "/api/mutual")
+def mutual(domains: list[str] = Query()):
+ """Return 200 if federation is open between the two, 4xx otherwise"""
+ fba.c.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],
+ "aw": "*." + domains[0],
+ "bw": "*." + domains[1],
+ },
+ )
+ res = fba.c.fetchone()
+
+ if res is not None:
+ # Blocks found
+ return responses.JSONResponse(status_code=418, content={})
+
+ # No known blocks
+ return responses.JSONResponse(status_code=200, content={})
+
+@app.get(fba.config["base_url"] + "/rss")
+def rss(request: Request, domain: str = None):
+ if domain != None:
+ wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
+ punycode = domain.encode('idna').decode('utf-8')
+ 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",
+ (domain, "*." + domain, wildchar, fba.get_hash(domain), punycode, "*." + punycode))
+ else:
+ fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks ORDER BY first_added DESC LIMIT 50")
+
+ blocks = fba.c.fetchall()
+
+ result = []
+ for blocker, blocked, block_level, reason, first_added, last_seen in blocks:
+ first_added = utils.format_datetime(datetime.fromtimestamp(first_added))
+ if reason == None or reason == '':
+ reason = "No reason provided."
+ else:
+ reason = "Provided reason: '" + reason + "'"
+
+ result.append(
+ {
+ "blocker": blocker,
+ "blocked": blocked,
+ "block_level": block_level,
+ "reason": reason,
+ "first_added": first_added
+ }
+ )
+
+ timestamp = utils.format_datetime(datetime.now())
+
+ return templates.TemplateResponse("rss.xml", {"request": request, "timestamp": timestamp, "domain": domain, "blocks": result}, headers={"Content-Type": "application/rss+xml"})
+
+if __name__ == "__main__":
+ uvicorn.run("api:app", host="127.0.0.1", port=fba.config["port"], log_level=fba.config["log_level"])