From f011839595a0215b1c61d2341b9d523b42fd04bf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 21 May 2023 16:31:43 +0200 Subject: [PATCH] Continued: - also log last detailed error message - introduced fba.is_blacklisted() --- api.py | 16 ++++++++-------- blocks_empty.db | Bin 28672 -> 28672 bytes fba.py | 14 +++++++++++++- fetch_instances.py | 19 +++++-------------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/api.py b/api.py index f0e11ef..e595f61 100644 --- a/api.py +++ b/api.py @@ -94,20 +94,20 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference scores = None if blockers != None and blockers > 0: - scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blockers={blockers}") + res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blockers={blockers}") elif blocked != None and blocked > 0: - scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blocked={blocked}") + res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?blocked={blocked}") elif reference != None and reference > 0: - scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?reference={reference}") + res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?reference={reference}") elif software != None and software > 0: - scores = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?software={software}") + res = requests.get(f"http://127.0.0.1:{fba.config['port']}{fba.config['base_url']}/top?software={software}") else: raise HTTPException(status_code=400, detail="No filter specified") - if scores == None: + if res == None: raise HTTPException(status_code=500, detail="Could not determine scores") - elif not scores.ok: - raise HTTPException(status_code=scores.status_code, detail=scores.text) + elif not res.ok: + raise HTTPException(status_code=res.status_code, detail=res.text) return templates.TemplateResponse("index.html", { "base_url" : fba.config["base_url"], @@ -117,7 +117,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference "blocked" : blocked, "reference" : reference, "software" : software, - "scores" : scores.json() + "scores" : res.json() }) @app.get(fba.config["base_url"] + "/") diff --git a/blocks_empty.db b/blocks_empty.db index bcb93703d52078ccf7038842654a614701d92c94..0832008ed05aaa9e89c0d6b540f1e8db3b2bd13c 100644 GIT binary patch delta 129 zcmZp8z}WDBae}nqTLuOOb|_{9(#jKcj3wVP=y^}%<=0_g<-X6r*UbBl`#x_suQ5;g z#>RARuBJdPc5!)m#+Jg(l01@(2098kiNz)HsYONkMe!-AC5f3i#R?&=5g`hWZXvEg W5ZU6A#FEnD_~iVQ)Xhe`=aK+Z#3fw- delta 81 zcmZp8z}WDBae}nq8wLgjb|_{9(n=F`j3wVN=y^}%<=0_g<>6=GYvz5&!_V8zYs^!= fSx{g)H&>G)7rVH;JY$RC=9k=(jGK*k&m{o>z$X$1 diff --git a/fba.py b/fba.py index 0d467b0..93a5de8 100644 --- a/fba.py +++ b/fba.py @@ -27,6 +27,17 @@ headers = { conn = sqlite3.connect("blocks.db") c = conn.cursor() +def is_blacklisted(domain: str) -> bool: + # NOISY-DEBUG: print("DEBUG: Checking blacklist for domain:", domain) + blacklisted = False + for peer in blacklist: + # NOISY-DEBUG: print("DEBUG: domain,peer:", domain, peer) + if peer in domain: + blacklisted = True + + # NOISY-DEBUG: print("DEBUG: blacklisted:", blacklisted) + return blacklisted + def get_hash(domain: str) -> str: # NOISY-DEBUG: print("DEBUG: Calculating hash for domain:", domain) return sha256(domain.encode("utf-8")).hexdigest() @@ -49,8 +60,9 @@ def update_last_error(domain: str, res: any): # NOISY-DEBUG: print("DEBUG: domain,res.status_code", domain, res.status_code) try: - c.execute("UPDATE instances SET last_status_code = ?, last_updated = ? WHERE domain = ?", [ + c.execute("UPDATE instances SET last_status_code = ?, last_error_details = ?, last_updated = ? WHERE domain = ?", [ res.status_code, + res.text, time.time(), domain ]) diff --git a/fetch_instances.py b/fetch_instances.py index 3a27338..4285336 100644 --- a/fetch_instances.py +++ b/fetch_instances.py @@ -23,17 +23,13 @@ def fetch_instances(domain: str, origin: str): print(f"INFO: Checking {len(peerlist)} instances from {domain} ...") for instance in peerlist: instance = instance.lower() + if instance.find("@") > 0: print("WARNING: Bad instance name,domain:", instance, domain) continue - blacklisted = False - for peer in fba.blacklist: - if peer in instance: - blacklisted = True - - if blacklisted: - # NOISY-DEBUG: print("DEBUG: domain is blacklisted:", domain) + if fba.is_blacklisted(instance): + # NOISY-DEBUG: print("DEBUG: instance is blacklisted:", instance) continue # NOISY-DEBUG: print("DEBUG: Handling instance:", instance) @@ -59,16 +55,11 @@ fetch_instances(instance, None) # Loop through some instances fba.c.execute( - "SELECT domain FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial') AND (last_nodeinfo IS NULL OR last_nodeinfo < ?) ORDER BY rowid DESC", [time.time() - fba.config["recheck_instance"]] + "SELECT domain FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial') AND (software IS NULL OR last_nodeinfo IS NULL OR last_nodeinfo < ?) ORDER BY rowid DESC", [time.time() - fba.config["recheck_instance"]] ) for instance in fba.c.fetchall(): - blacklisted = False - for domain in fba.blacklist: - if domain in instance[0]: - blacklisted = True - - if blacklisted: + if fba.is_blacklisted(instance[0]): # NOISY-DEBUG: print("DEBUG: domain is blacklisted:", instance) continue -- 2.39.5