9 "SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial', 'bookwyrm', 'takahe') AND (last_blocked IS NULL OR last_blocked < ?) ORDER BY rowid DESC", [time.time() - fba.config["recheck_block"]]
12 rows = fba.cursor.fetchall()
13 print(f"INFO: Checking {len(rows)} entries ...")
14 for blocker, software in rows:
15 # NOISY-DEBUG: print("DEBUG: BEFORE blocker,software:", blocker, software)
17 blocker = fba.tidyup(blocker)
18 # NOISY-DEBUG: print("DEBUG: AFTER blocker,software:", blocker, software)
21 print("WARNING: blocker is now empty!")
23 elif fba.is_blacklisted(blocker):
24 print(f"WARNING: blocker='{blocker}' is blacklisted now!")
27 # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}'")
28 fba.update_last_blocked(blocker)
30 if software == "pleroma":
31 print("INFO: blocker:", blocker)
34 json = fba.fetch_nodeinfo(blocker)
36 print("WARNING: Could not fetch nodeinfo from blocker:", blocker)
39 print("DEBUG: Updating nodeinfo:", blocker)
40 fba.update_last_nodeinfo(blocker)
42 federation = json["metadata"]["federation"]
44 if "enabled" in federation:
45 # NOISY-DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)
48 if "mrf_simple" in federation:
49 for block_level, blocks in (
50 {**federation["mrf_simple"],
51 **{"quarantined_instances": federation["quarantined_instances"]}}
53 # NOISY-DEBUG: print("DEBUG: block_level, blocks():", block_level, len(blocks))
54 block_level = fba.tidyup(block_level)
55 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
58 print("WARNING: block_level is now empty!")
61 for blocked in blocks:
62 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
63 blocked = fba.tidyup(blocked)
64 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
67 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
70 if blocked.count("*") > 1:
71 # -ACK!-oma also started obscuring domains without hash
73 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
75 searchres = fba.cursor.fetchone()
76 # NOISY-DEBUG: print("DEBUG: searchres[]:", type(searchres))
78 blocked = searchres[0]
79 # NOISY-DEBUG: print("DEBUG: Looked up domain:", blocked)
81 # NOISY-DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
83 "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked]
86 if fba.cursor.fetchone() == None:
87 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
88 fba.add_instance(blocked, blocker, argv[0])
91 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
99 if fba.cursor.fetchone() == None:
100 # NOISY-DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
101 fba.block_instance(blocker, blocked, "unknown", block_level)
103 if block_level == "reject":
104 # NOISY-DEBUG: print("DEBUG: Adding to blockdict:", blocked)
111 # NOISY-DEBUG: print("DEBUG: Updating last_seen:", blocker, blocked, block_level)
112 fba.update_last_seen(blocker, blocked, block_level)
114 fba.connection.commit()
117 if "mrf_simple_info" in federation:
118 # NOISY-DEBUG: print("DEBUG: Found mrf_simple_info:", blocker)
119 for block_level, info in (
120 {**federation["mrf_simple_info"],
121 **(federation["quarantined_instances_info"]
122 if "quarantined_instances_info" in federation
125 # NOISY-DEBUG: print("DEBUG: block_level, info.items():", block_level, len(info.items()))
126 block_level = fba.tidyup(block_level)
127 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
129 if block_level == "":
130 print("WARNING: block_level is now empty!")
133 for blocked, reason in info.items():
134 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
135 blocked = fba.tidyup(blocked)
136 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
139 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
141 elif blocked.count("*") > 1:
142 # same domain guess as above, but for reasons field
144 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
146 searchres = fba.cursor.fetchone()
148 if searchres != None:
149 blocked = searchres[0]
151 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason["reason"])
152 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
154 for entry in blockdict:
155 if entry["blocked"] == blocked:
156 # NOISY-DEBUG: print("DEBUG: Updating entry reason:", blocked)
157 entry["reason"] = reason["reason"]
159 fba.connection.commit()
160 except Exception as e:
161 print("error:", e, blocker, software)
162 elif software == "mastodon":
163 print("INFO: blocker:", blocker)
165 # json endpoint for newer mastodongs
170 "followers_only": [],
174 # handling CSRF, I've saw at least one server requiring it to access the endpoint
175 # NOISY-DEBUG: print("DEBUG: Fetching meta:", blocker)
176 meta = bs4.BeautifulSoup(
177 reqto.get(f"https://{blocker}/about", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).text,
181 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
182 # NOISY-DEBUG: print("DEBUG: Adding CSRF token:", blocker, csrf)
183 reqheaders = {**fba.headers, **{"x-csrf-token": csrf}}
185 # NOISY-DEBUG: print("DEBUG: No CSRF token found, using normal headers:", blocker)
186 reqheaders = fba.headers
188 # NOISY-DEBUG: print("DEBUG: Quering API domain_blocks:", blocker)
189 blocks = reqto.get(f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
191 # NOISY-DEBUG: print("DEBUG: blocks():", len(blocks))
194 'domain': block['domain'],
195 'hash': block['digest'],
196 'reason': block['comment']
199 # NOISY-DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
200 if block['severity'] == 'suspend':
201 json['reject'].append(entry)
202 elif block['severity'] == 'silence':
203 json['followers_only'].append(entry)
204 elif block['severity'] == 'reject_media':
205 json['media_removal'].append(entry)
206 elif block['severity'] == 'reject_reports':
207 json['report_removal'].append(entry)
209 print("WARNING: Unknown severity:", block['severity'], block['domain'])
211 # NOISY-DEBUG: print("DEBUG: Failed, Trying mastodon-specific fetches:", blocker)
212 json = fba.get_mastodon_blocks(blocker)
214 # NOISY-DEBUG: print("DEBUG: json.items():", blocker, len(json.items()))
215 for block_level, blocks in json.items():
216 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
217 block_level = fba.tidyup(block_level)
218 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
219 if block_level == "":
220 print("WARNING: block_level is empty, blocker:", blocker)
223 for instance in blocks:
224 blocked, blocked_hash, reason = instance.values()
225 # NOISY-DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
226 blocked = fba.tidyup(blocked)
227 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
230 print("WARNING: blocked is empty:", blocker)
232 elif blocked.count("*") < 1:
233 # No obsfucation for this instance
235 "SELECT hash FROM instances WHERE domain = ? LIMIT 1", [blocked]
238 if fba.cursor.fetchone() == None:
239 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
240 fba.add_instance(blocked, blocker, argv[0])
242 # Doing the hash search for instance names as well to tidy up DB
244 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
246 searchres = fba.cursor.fetchone()
248 if searchres != None:
249 # NOISY-DEBUG: print("DEBUG: Updating domain: ", searchres[0])
250 blocked = searchres[0]
253 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
256 blocked if blocked.count("*") <= 1 else blocked_hash,
261 if fba.cursor.fetchone() == None:
262 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level)
264 if block_level == "reject":
271 fba.update_last_seen(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
274 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
275 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
277 fba.connection.commit()
278 except Exception as e:
279 print("error:", e, blocker, software)
280 elif software == "friendica" or software == "misskey" or software == "bookwyrm" or software == "takahe":
281 print("INFO: blocker:", blocker)
283 if software == "friendica":
284 json = fba.get_friendica_blocks(blocker)
285 elif software == "misskey":
286 json = fba.get_misskey_blocks(blocker)
287 elif software == "bookwyrm":
288 print("WARNING: bookwyrm is not fully supported for fetching blacklist!", blocker)
289 #json = fba.get_bookwyrm_blocks(blocker)
290 elif software == "takahe":
291 print("WARNING: takahe is not fully supported for fetching blacklist!", blocker)
292 #json = fba.get_takahe_blocks(blocker)
294 for block_level, blocks in json.items():
295 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
296 block_level = fba.tidyup(block_level)
297 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
298 if block_level == "":
299 print("WARNING: block_level is empty, blocker:", blocker)
302 for instance in blocks:
303 blocked, reason = instance.values()
304 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
305 blocked = fba.tidyup(blocked)
306 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
309 print("WARNING: blocked is empty:", blocker)
311 if blocked.count("*") > 0:
312 # Some friendica servers also obscure domains without hash
314 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
316 searchres = fba.cursor.fetchone()
317 if searchres != None:
318 blocked = searchres[0]
320 if blocked.count("?") > 0:
321 # Some obscure them with question marks, not sure if that's dependent on version or not
323 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("?", "_")]
325 searchres = fba.cursor.fetchone()
326 if searchres != None:
327 blocked = searchres[0]
329 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
331 "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked]
334 if fba.cursor.fetchone() == None:
335 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
336 fba.add_instance(blocked, blocker)
339 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
342 if fba.cursor.fetchone() == None:
343 fba.block_instance(blocker, blocked, reason, block_level)
345 if block_level == "reject":
352 fba.update_last_seen(blocker, blocked, block_level)
355 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
356 fba.update_block_reason(reason, blocker, blocked, block_level)
358 fba.connection.commit()
359 except Exception as e:
360 print("error:", e, blocker, software)
361 elif software == "gotosocial":
362 print("INFO: blocker:", blocker)
365 federation = reqto.get(f"https://{blocker}{get_peers_url}?filter=suspended", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
367 if (federation == None):
368 print("WARNING: No valid response:", blocker);
369 elif "error" in federation:
370 print("WARNING: API returned error:", federation["error"])
372 # NOISY-DEBUG: print("DEBUG: Checking fenderation():", len(federation))
373 for peer in federation:
374 blocked = peer["domain"].lower()
375 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
376 blocked = fba.tidyup(blocked)
377 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
380 print("WARNING: blocked is empty:", blocker)
382 elif blocked.count("*") > 0:
383 # GTS does not have hashes for obscured domains, so we have to guess it
385 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
387 searchres = fba.cursor.fetchone()
389 if searchres != None:
390 blocked = searchres[0]
393 "SELECT domain FROM instances WHERE domain = ? LIMIT 1", [blocked]
396 if fba.cursor.fetchone() == None:
397 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
398 fba.update_block_reason(reason, blocker, blocked, block_level)
401 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
409 if fba.cursor.fetchone() == None:
410 fba.block_instance(blocker, blocked, "unknown", "reject")
418 fba.update_last_seen(blocker, blocked, "reject")
420 if "public_comment" in peer:
421 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
422 fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
424 for entry in blockdict:
425 if entry["blocked"] == blocked:
426 entry["reason"] = peer["public_comment"]
428 fba.connection.commit()
429 except Exception as e:
430 print("error:", e, blocker, software)
432 print("WARNING: Unknown software:", blocker, software)
434 if fba.config["bot_enabled"] and len(blockdict) > 0:
435 send_bot_post(blocker, blockdict)
439 fba.connection.close()