2 # -*- coding: utf-8 -*-
4 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
5 # Copyright (C) 2023 Free Software Foundation
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published
9 # by the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
31 "SELECT domain, software, origin, nodeinfo_url 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"]]
34 rows = fba.cursor.fetchall()
35 print(f"INFO: Checking {len(rows)} entries ...")
36 for blocker, software, origin, nodeinfo_url in rows:
37 # DEBUG: print("DEBUG: BEFORE blocker,software,origin,nodeinfo_url:", blocker, software, origin, nodeinfo_url)
39 blocker = fba.tidyup(blocker)
40 # DEBUG: print("DEBUG: AFTER blocker,software:", blocker, software)
43 print("WARNING: blocker is now empty!")
45 elif fba.is_blacklisted(blocker):
46 print(f"WARNING: blocker='{blocker}' is blacklisted now!")
49 # DEBUG: print(f"DEBUG: blocker='{blocker}'")
50 fba.update_last_blocked(blocker)
52 if software == "pleroma":
53 print("INFO: blocker:", blocker)
56 json = fba.fetch_nodeinfo(blocker, nodeinfo_url)
58 print("WARNING: Could not fetch nodeinfo from blocker:", blocker)
61 # DEBUG: print("DEBUG: Updating nodeinfo:", blocker)
62 fba.update_last_nodeinfo(blocker)
64 federation = json["metadata"]["federation"]
66 if "enabled" in federation:
67 # DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)
70 if "mrf_simple" in federation:
71 for block_level, blocks in (
72 {**federation["mrf_simple"],
73 **{"quarantined_instances": federation["quarantined_instances"]}}
75 # DEBUG: print("DEBUG: block_level, blocks():", block_level, len(blocks))
76 block_level = fba.tidyup(block_level)
77 # DEBUG: print("DEBUG: BEFORE block_level:", block_level)
80 print("WARNING: block_level is now empty!")
83 # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from blocker='{blocker}',software='{software}',block_level='{block_level}' ...")
84 for blocked in blocks:
85 # DEBUG: print("DEBUG: BEFORE blocked:", blocked)
86 blocked = fba.tidyup(blocked)
87 # DEBUG: print("DEBUG: AFTER blocked:", blocked)
90 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
92 elif fba.is_blacklisted(blocked):
93 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
95 elif blocked.count("*") > 1:
96 # -ACK!-oma also started obscuring domains without hash
98 "SELECT domain, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
100 searchres = fba.cursor.fetchone()
101 # DEBUG: print("DEBUG: searchres[]:", type(searchres))
103 if searchres == None:
104 print(f"WARNING: Cannot deobsfucate blocked='{blocked}' - SKIPPED!")
107 blocked = searchres[0]
108 nodeinfo_url = searchres[1]
109 # DEBUG: print("DEBUG: Looked up domain:", blocked)
110 elif not validators.domain(blocked):
111 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domai name - skipped!")
114 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
115 if not validators.domain(blocked):
116 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
118 elif not fba.is_instance_registered(blocked):
119 # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., blocker='{blocker}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
120 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
123 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
131 if fba.cursor.fetchone() == None:
132 # DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
133 fba.block_instance(blocker, blocked, "unknown", block_level)
135 if block_level == "reject":
136 # DEBUG: print("DEBUG: Adding to blockdict:", blocked)
143 print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
144 fba.update_last_seen(blocker, blocked, block_level)
146 fba.connection.commit()
149 if "mrf_simple_info" in federation:
150 # DEBUG: print("DEBUG: Found mrf_simple_info:", blocker)
151 for block_level, info in (
152 {**federation["mrf_simple_info"],
153 **(federation["quarantined_instances_info"]
154 if "quarantined_instances_info" in federation
157 # DEBUG: print("DEBUG: block_level, info.items():", block_level, len(info.items()))
158 block_level = fba.tidyup(block_level)
159 # DEBUG: print("DEBUG: BEFORE block_level:", block_level)
161 if block_level == "":
162 print("WARNING: block_level is now empty!")
165 # DEBUG: print(f"DEBUG: Checking {len(info.items())} entries from blocker='{blocker}',software='{software}',block_level='{block_level}' ...")
166 for blocked, reason in info.items():
167 # DEBUG: print("DEBUG: BEFORE blocked:", blocked)
168 blocked = fba.tidyup(blocked)
169 # DEBUG: print("DEBUG: AFTER blocked:", blocked)
172 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
174 elif fba.is_blacklisted(blocked):
175 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
177 elif blocked.count("*") > 1:
178 # same domain guess as above, but for reasons field
180 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
182 searchres = fba.cursor.fetchone()
184 if searchres == None:
185 print(f"WARNING: Cannot deobsfucate blocked='{blocked}' - SKIPPED!")
188 blocked = searchres[0]
189 origin = searchres[1]
190 nodeinfo_url = searchres[2]
191 elif not validators.domain(blocked):
192 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domai name - skipped!")
195 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
196 if not validators.domain(blocked):
197 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
199 elif not fba.is_instance_registered(blocked):
200 # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., blocker='{blocker}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
201 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
203 # DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason["reason"])
204 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
206 for entry in blockdict:
207 if entry["blocked"] == blocked:
208 # DEBUG: print("DEBUG: Updating entry reason:", blocked)
209 entry["reason"] = reason["reason"]
211 fba.connection.commit()
212 except Exception as e:
213 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
214 elif software == "mastodon":
215 print("INFO: blocker:", blocker)
217 # json endpoint for newer mastodongs
221 "media_removal" : [],
222 "followers_only": [],
226 # handling CSRF, I've saw at least one server requiring it to access the endpoint
227 # DEBUG: print("DEBUG: Fetching meta:", blocker)
228 meta = bs4.BeautifulSoup(
229 reqto.get(f"https://{blocker}/", headers=fba.headers, timeout=(fba.config["connection_timeout"], fba.config["read_timeout"])).text,
233 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
234 # DEBUG: print("DEBUG: Adding CSRF token:", blocker, csrf)
235 reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
236 except BaseException as e:
237 # DEBUG: print("DEBUG: No CSRF token found, using normal headers:", blocker, e)
238 reqheaders = fba.api_headers
240 # DEBUG: print("DEBUG: Querying API domain_blocks:", blocker)
241 blocks = reqto.get(f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=(fba.config["connection_timeout"], fba.config["read_timeout"])).json()
243 print(f"INFO: Checking {len(blocks)} entries from blocker='{blocker}',software='{software}' ...")
246 'domain': block['domain'],
247 'hash' : block['digest'],
248 'reason': block['comment']
251 # DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
252 if block['severity'] == 'suspend':
253 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
254 json['reject'].append(entry)
255 elif block['severity'] == 'silence':
256 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
257 json['followers_only'].append(entry)
258 elif block['severity'] == 'reject_media':
259 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
260 json['media_removal'].append(entry)
261 elif block['severity'] == 'reject_reports':
262 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
263 json['report_removal'].append(entry)
265 print("WARNING: Unknown severity:", block['severity'], block['domain'])
266 except BaseException as e:
267 # DEBUG: print(f"DEBUG: Failed, trying mastodon-specific fetches: blocker='{blocker}',exception[{type(e)}]={str(e)}")
268 json = fba.get_mastodon_blocks(blocker)
270 print(f"INFO: Checking {len(json.items())} entries from blocker='{blocker}',software='{software}' ...")
271 for block_level, blocks in json.items():
272 # DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
273 block_level = fba.tidyup(block_level)
274 # DEBUG: print("DEBUG: AFTER-block_level:", block_level)
275 if block_level == "":
276 print("WARNING: block_level is empty, blocker:", blocker)
279 # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from blocker='{blocker}',software='{software}',block_level='{block_level}' ...")
281 blocked, blocked_hash, reason = block.values()
282 # DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
283 blocked = fba.tidyup(blocked)
284 # DEBUG: print("DEBUG: AFTER-blocked:", blocked)
287 print("WARNING: blocked is empty:", blocker)
289 elif fba.is_blacklisted(blocked):
290 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
292 elif blocked.count("*") > 0:
293 # Doing the hash search for instance names as well to tidy up DB
295 "SELECT domain, origin, nodeinfo_url FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
297 searchres = fba.cursor.fetchone()
299 if searchres == None:
300 print(f"WARNING: Cannot deobsfucate blocked='{blocked}',blocked_hash='{blocked_hash}' - SKIPPED!")
303 # DEBUG: print("DEBUG: Updating domain: ", searchres[0])
304 blocked = searchres[0]
305 origin = searchres[1]
306 nodeinfo_url = searchres[2]
308 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
309 if not validators.domain(blocked):
310 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
312 elif not fba.is_instance_registered(blocked):
313 # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., blocker='{blocker}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
314 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
315 elif not validators.domain(blocked):
316 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domai name - skipped!")
319 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
320 if not validators.domain(blocked):
321 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
323 elif not fba.is_instance_registered(blocked):
324 # DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
325 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
327 blocking = blocked if blocked.count("*") <= 1 else blocked_hash
328 # DEBUG: print(f"DEBUG: blocking='{blocking}',blocked='{blocked}',blocked_hash='{blocked_hash}'")
331 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
339 if fba.cursor.fetchone() == None:
340 # DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
341 fba.block_instance(blocker, blocking, reason, block_level)
343 if block_level == "reject":
350 # DEBUG: print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocking='{blocking}' ...")
351 fba.update_last_seen(blocker, blocking, block_level)
352 fba.update_block_reason(reason, blocker, blocking, block_level)
354 fba.connection.commit()
355 except Exception as e:
356 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
357 elif software == "friendica" or software == "misskey" or software == "bookwyrm" or software == "takahe":
358 print("INFO: blocker:", blocker)
360 if software == "friendica":
361 json = fba.get_friendica_blocks(blocker)
362 elif software == "misskey":
363 json = fba.get_misskey_blocks(blocker)
364 elif software == "bookwyrm":
365 print("WARNING: bookwyrm is not fully supported for fetching blacklist!", blocker)
366 #json = fba.get_bookwyrm_blocks(blocker)
368 elif software == "takahe":
369 print("WARNING: takahe is not fully supported for fetching blacklist!", blocker)
370 #json = fba.get_takahe_blocks(blocker)
373 print(f"INFO: Checking {len(json.items())} entries from blocker='{blocker}',software='{software}' ...")
374 for block_level, blocks in json.items():
375 # DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
376 block_level = fba.tidyup(block_level)
377 # DEBUG: print("DEBUG: AFTER-block_level:", block_level)
378 if block_level == "":
379 print("WARNING: block_level is empty, blocker:", blocker)
382 # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from blocker='{blocker}',software='{software}',block_level='{block_level}' ...")
384 blocked, reason = block.values()
385 # DEBUG: print("DEBUG: BEFORE blocked:", blocked)
386 blocked = fba.tidyup(blocked)
387 # DEBUG: print("DEBUG: AFTER blocked:", blocked)
390 print("WARNING: blocked is empty:", blocker)
392 elif fba.is_blacklisted(blocked):
393 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
395 elif blocked.count("*") > 0:
396 # Some friendica servers also obscure domains without hash
398 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
401 searchres = fba.cursor.fetchone()
403 if searchres == None:
404 print(f"WARNING: Cannot deobsfucate blocked='{blocked}' - SKIPPED!")
407 blocked = searchres[0]
408 origin = searchres[1]
409 nodeinfo_url = searchres[2]
410 elif blocked.count("?") > 0:
411 # Some obscure them with question marks, not sure if that's dependent on version or not
413 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("?", "_")]
416 searchres = fba.cursor.fetchone()
418 if searchres == None:
419 print(f"WARNING: Cannot deobsfucate blocked='{blocked}' - SKIPPED!")
422 blocked = searchres[0]
423 origin = searchres[1]
424 nodeinfo_url = searchres[2]
425 elif not validators.domain(blocked):
426 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domai name - skipped!")
429 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
430 if not validators.domain(blocked):
431 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
433 elif not fba.is_instance_registered(blocked):
434 # DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
435 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
438 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
439 (blocker, blocked, block_level),
442 if fba.cursor.fetchone() == None:
443 fba.block_instance(blocker, blocked, reason, block_level)
445 if block_level == "reject":
452 print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocked='{blocked}' ...")
453 fba.update_last_seen(blocker, blocked, block_level)
454 fba.update_block_reason(reason, blocker, blocked, block_level)
456 fba.connection.commit()
457 except Exception as e:
458 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
459 elif software == "gotosocial":
460 print("INFO: blocker:", blocker)
463 federation = reqto.get(f"https://{blocker}{fba.get_peers_url}?filter=suspended", headers=fba.api_headers, timeout=(fba.config["connection_timeout"], fba.config["read_timeout"])).json()
465 if (federation == None):
466 print("WARNING: No valid response:", blocker);
467 elif "error" in federation:
468 print("WARNING: API returned error:", federation["error"])
470 print(f"INFO: Checking {len(federation)} entries from blocker='{blocker}',software='{software}' ...")
471 for peer in federation:
472 blocked = peer["domain"].lower()
473 # DEBUG: print("DEBUG: BEFORE blocked:", blocked)
474 blocked = fba.tidyup(blocked)
475 # DEBUG: print("DEBUG: AFTER blocked:", blocked)
478 print("WARNING: blocked is empty:", blocker)
480 elif fba.is_blacklisted(blocked):
481 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
483 elif blocked.count("*") > 0:
484 # GTS does not have hashes for obscured domains, so we have to guess it
486 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
488 searchres = fba.cursor.fetchone()
490 if searchres == None:
491 print(f"WARNING: Cannot deobsfucate blocked='{blocked}' - SKIPPED!")
494 blocked = searchres[0]
495 origin = searchres[1]
496 nodeinfo_url = searchres[2]
497 elif not validators.domain(blocked):
498 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domai name - skipped!")
501 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
502 if not validators.domain(blocked):
503 print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
505 elif not fba.is_instance_registered(blocked):
506 # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., blocker='{blocker}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
507 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
510 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
518 if fba.cursor.fetchone() == None:
519 # DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point")
520 fba.block_instance(blocker, blocked, "unknown", "reject")
528 print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
529 fba.update_last_seen(blocker, blocked, "reject")
531 if "public_comment" in peer:
532 # DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
533 fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
535 for entry in blockdict:
536 if entry["blocked"] == blocked:
537 # DEBUG: print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'")
538 entry["reason"] = peer["public_comment"]
540 fba.connection.commit()
541 except Exception as e:
542 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
544 print("WARNING: Unknown software:", blocker, software)
546 if fba.config["bot_enabled"] and len(blockdict) > 0:
547 send_bot_post(blocker, blockdict)