From 4ec3ec715886aad7d22bb33e034c981484e7e018 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 29 May 2023 19:13:00 +0200 Subject: [PATCH] Continued: - introduced config key 'host', so you can let this run on an other IP address - recommended is 127.0.0.1, of course and then setup a reverse-proxy (in Apache's terminology) - cleaned up imports --- api.py | 53 +++++++++++++++++++++++--------------------- config.defaults.json | 1 + 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/api.py b/api.py index fb76f9b..1c6a98d 100644 --- a/api.py +++ b/api.py @@ -14,18 +14,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from fastapi import FastAPI, Request, HTTPException, responses, Query +from fastapi import Request, HTTPException, responses, Query from fastapi.templating import Jinja2Templates from datetime import datetime from email import utils +import fastapi import uvicorn import requests -import sqlite3 import re import fba -app = FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") +app = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") templates = Jinja2Templates(directory=".") @app.get(fba.config["base_url"] + "/api/info") @@ -78,10 +78,12 @@ def top(blocked: int = None, blockers: int = None, reference: int = None, softwa def blocked(domain: str = None, reason: str = None, reverse: str = None): if domain == None and reason == None and reverse == None: raise HTTPException(status_code=400, detail="No filter specified") + if reason != None: reason = re.sub("(%|_)", "", reason) if len(reason) < 3: raise HTTPException(status_code=400, detail="Keyword is shorter than three characters") + if domain != None: wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) punycode = domain.encode('idna').decode('utf-8') @@ -91,16 +93,17 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None): fba.cursor.execute("SELECT blocker, blocked, block_level, reason, first_seen, last_seen FROM blocks WHERE blocker = ? ORDER BY first_seen ASC", [reverse]) else: fba.cursor.execute("SELECT blocker, blocked, block_level, reason, first_seen, last_seen FROM blocks WHERE reason like ? AND reason != '' ORDER BY first_seen ASC", ["%" + reason + "%"]) + blocks = fba.cursor.fetchall() result = {} for blocker, blocked, block_level, reason, first_seen, last_seen in blocks: entry = { - "blocker" : blocker, - "blocked" : blocked, - "reason" : reason, + "blocker" : blocker, + "blocked" : blocked, + "reason" : reason, "first_seen": first_seen, - "last_seen" : last_seen + "last_seen" : last_seen } if block_level in result: result[block_level].append(entry) @@ -114,13 +117,13 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference scores = None if blockers != None and blockers > 0: - res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api/top?blockers={blockers}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?blockers={blockers}") elif blocked != None and blocked > 0: - res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api/top?blocked={blocked}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?blocked={blocked}") elif reference != None and reference > 0: - res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api/top?reference={reference}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?reference={reference}") elif software != None and software > 0: - res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api/top?software={software}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?software={software}") else: raise HTTPException(status_code=400, detail="No filter specified") @@ -149,18 +152,18 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str blocks = None if domain == None and reason == None and reverse == None: - info = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api/info") + info = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/info") if not info.ok: raise HTTPException(status_code=info.status_code, detail=info.text) info = info.json() elif domain != None: - blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}") elif reason != None: - blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}") elif reverse != None: - blocks = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}") if blocks != None: if not blocks.ok: @@ -189,8 +192,8 @@ def mutual(domains: list[str] = Query()): "AND block_level = 'reject' " \ "LIMIT 1", { - "a": domains[0], - "b": domains[1], + "a" : domains[0], + "b" : domains[1], "aw": "*." + domains[0], "bw": "*." + domains[1], }, @@ -225,23 +228,23 @@ def rss(request: Request, domain: str = None): reason = "Provided reason: '" + reason + "'" result.append({ - "blocker": blocker, - "blocked": blocked, + "blocker" : blocker, + "blocked" : blocked, "block_level": block_level, - "reason": reason, - "first_seen": first_seen + "reason" : reason, + "first_seen" : first_seen }) timestamp = utils.format_datetime(datetime.now()) return templates.TemplateResponse("rss.xml", { - "request": request, + "request" : request, "timestamp": timestamp, - "domain": domain, - "blocks": result + "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"]) + uvicorn.run("api:app", host=fba.config["host"], port=fba.config["port"], log_level=fba.config["log_level"]) diff --git a/config.defaults.json b/config.defaults.json index 7cd87ed..9ab743b 100644 --- a/config.defaults.json +++ b/config.defaults.json @@ -1,6 +1,7 @@ { "base_url" : "", "log_level" : "info", + "host" : "127.0.0.1", "port" : 8069, "useragent" : "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0", "connection_timeout": 2, -- 2.39.5