From e4caf63d48037c1d5597155e68d82993c56bb9f1 Mon Sep 17 00:00:00 2001 From: pwm Date: Sun, 26 Feb 2023 15:23:35 -0600 Subject: [PATCH] Add api endpoint to test if there are blocks between two domains. Added an index to cover the query. --- api.py | 26 +++++++++++++++++++++++++- blocks_empty.db | Bin 16384 -> 16384 bytes 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index f2742ed..43086fd 100644 --- a/api.py +++ b/api.py @@ -1,5 +1,5 @@ import uvicorn -from fastapi import FastAPI, Request, HTTPException, responses +from fastapi import FastAPI, Request, HTTPException, responses, Query import sqlite3 from hashlib import sha256 from fastapi.templating import Jinja2Templates @@ -129,5 +129,29 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str return templates.TemplateResponse("index.html", {"request": request, "domain": domain, "blocks": blocks, "reason": reason, "reverse": reverse, "info": info}) +@app.get(base_url+"/api/mutual") +def mutual(domains: list[str] = Query()): + """Return 200 if federation is open between the two, 4xx otherwise""" + conn = sqlite3.connect('blocks.db') + c = conn.cursor() + c.execute( + "SELECT block_level FROM blocks " \ + "WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a)) " \ + "AND block_level = 'reject' " \ + "LIMIT 1", + { + "a": domains[0], + "b": domains[1], + }, + ) + res = c.fetchone() + c.close() + if res is not None: + # Blocks found + return responses.JSONResponse(status_code=418, content={}) + # No known blocks + return responses.JSONResponse(status_code=200, content={}) + if __name__ == "__main__": uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info") + diff --git a/blocks_empty.db b/blocks_empty.db index 6ebc131a7ef956a28f0fbd19884aaafca7649b6c..f8234800b604d6d56272c4a21868c243af881a1a 100644 GIT binary patch delta 201 zcmZo@U~Fh$oFFZDkAZ=K1&CpQYod;^@I3~-Xj5MP2@K49Yz+K5e9e4pn-}tD@J_zP zt5aXi!Y*zv$=Il!nU|7Uk(p8vpOllIoSj+(rBmW_Qp-|vz=Fk0&Oxq@A+8FZelD&N s3OE%j`1>h9G-yB#(}D0)Q=klpp_+|qn_Ky+85y}YGb;R-U!))a0LXkjqyPW_ delta 100 zcmZo@U~Fh$oFFZDn}LCW1&En|m=TECC+Zjr-)7K@m*VB0z`(@U%)qb1*UWosv!Fl* l@8ny&Iuj?PiveZ!1J$B|&CPtZjGGk%9`H|`ps+|m0RZN|6u|%h -- 2.39.5