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/>.
28 "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"]]
31 rows = fba.cursor.fetchall()
32 print(f"INFO: Checking {len(rows)} entries ...")
33 for blocker, software, origin, nodeinfo_url in rows:
34 # NOISY-DEBUG: print("DEBUG: BEFORE blocker,software,origin,nodeinfo_url:", blocker, software, origin, nodeinfo_url)
36 blocker = fba.tidyup(blocker)
37 # NOISY-DEBUG: print("DEBUG: AFTER blocker,software:", blocker, software)
40 print("WARNING: blocker is now empty!")
42 elif fba.is_blacklisted(blocker):
43 print(f"WARNING: blocker='{blocker}' is blacklisted now!")
46 # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}'")
47 fba.update_last_blocked(blocker)
49 if software == "pleroma":
50 print("INFO: blocker:", blocker)
53 json = fba.fetch_nodeinfo(blocker, nodeinfo_url)
55 print("WARNING: Could not fetch nodeinfo from blocker:", blocker)
58 print("DEBUG: Updating nodeinfo:", blocker)
59 fba.update_last_nodeinfo(blocker)
61 federation = json["metadata"]["federation"]
63 if "enabled" in federation:
64 # NOISY-DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)
67 if "mrf_simple" in federation:
68 for block_level, blocks in (
69 {**federation["mrf_simple"],
70 **{"quarantined_instances": federation["quarantined_instances"]}}
72 # NOISY-DEBUG: print("DEBUG: block_level, blocks():", block_level, len(blocks))
73 block_level = fba.tidyup(block_level)
74 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
77 print("WARNING: block_level is now empty!")
80 for blocked in blocks:
81 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
82 blocked = fba.tidyup(blocked)
83 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
86 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
89 if blocked.count("*") > 1:
90 # -ACK!-oma also started obscuring domains without hash
92 "SELECT domain, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
94 searchres = fba.cursor.fetchone()
95 # NOISY-DEBUG: print("DEBUG: searchres[]:", type(searchres))
97 blocked = searchres[0]
98 nodeinfo_url = searchres[1]
99 # NOISY-DEBUG: print("DEBUG: Looked up domain:", blocked)
101 # NOISY-DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
102 if not fba.is_instance_registered(blocked):
103 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
104 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
107 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
115 if fba.cursor.fetchone() == None:
116 # NOISY-DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
117 fba.block_instance(blocker, blocked, "unknown", block_level)
119 if block_level == "reject":
120 # NOISY-DEBUG: print("DEBUG: Adding to blockdict:", blocked)
127 # NOISY-DEBUG: print("DEBUG: Updating last_seen:", blocker, blocked, block_level)
128 fba.update_last_seen(blocker, blocked, block_level)
130 fba.connection.commit()
133 if "mrf_simple_info" in federation:
134 # NOISY-DEBUG: print("DEBUG: Found mrf_simple_info:", blocker)
135 for block_level, info in (
136 {**federation["mrf_simple_info"],
137 **(federation["quarantined_instances_info"]
138 if "quarantined_instances_info" in federation
141 # NOISY-DEBUG: print("DEBUG: block_level, info.items():", block_level, len(info.items()))
142 block_level = fba.tidyup(block_level)
143 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
145 if block_level == "":
146 print("WARNING: block_level is now empty!")
149 for blocked, reason in info.items():
150 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
151 blocked = fba.tidyup(blocked)
152 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
155 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
157 elif blocked.count("*") > 1:
158 # same domain guess as above, but for reasons field
160 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
162 searchres = fba.cursor.fetchone()
164 if searchres != None:
165 blocked = searchres[0]
167 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason["reason"])
168 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
170 for entry in blockdict:
171 if entry["blocked"] == blocked:
172 # NOISY-DEBUG: print("DEBUG: Updating entry reason:", blocked)
173 entry["reason"] = reason["reason"]
175 fba.connection.commit()
176 except Exception as e:
177 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
178 elif software == "mastodon":
179 print("INFO: blocker:", blocker)
181 # json endpoint for newer mastodongs
185 "media_removal" : [],
186 "followers_only": [],
190 # handling CSRF, I've saw at least one server requiring it to access the endpoint
191 # NOISY-DEBUG: print("DEBUG: Fetching meta:", blocker)
192 meta = bs4.BeautifulSoup(
193 reqto.get(f"https://{blocker}/about", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).text,
197 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
198 # NOISY-DEBUG: print("DEBUG: Adding CSRF token:", blocker, csrf)
199 reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
201 # NOISY-DEBUG: print("DEBUG: No CSRF token found, using normal headers:", blocker)
202 reqheaders = fba.api_headers
204 # NOISY-DEBUG: print("DEBUG: Quering API domain_blocks:", blocker)
205 blocks = reqto.get(f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
207 # NOISY-DEBUG: print("DEBUG: blocks():", len(blocks))
210 'domain': block['domain'],
211 'hash' : block['digest'],
212 'reason': block['comment']
215 # NOISY-DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
216 if block['severity'] == 'suspend':
217 json['reject'].append(entry)
218 elif block['severity'] == 'silence':
219 json['followers_only'].append(entry)
220 elif block['severity'] == 'reject_media':
221 json['media_removal'].append(entry)
222 elif block['severity'] == 'reject_reports':
223 json['report_removal'].append(entry)
225 print("WARNING: Unknown severity:", block['severity'], block['domain'])
227 # NOISY-DEBUG: print("DEBUG: Failed, Trying mastodon-specific fetches:", blocker)
228 json = fba.get_mastodon_blocks(blocker)
230 # NOISY-DEBUG: print("DEBUG: json.items():", blocker, len(json.items()))
231 for block_level, blocks in json.items():
232 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
233 block_level = fba.tidyup(block_level)
234 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
235 if block_level == "":
236 print("WARNING: block_level is empty, blocker:", blocker)
239 for instance in blocks:
240 blocked, blocked_hash, reason = instance.values()
241 # NOISY-DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
242 blocked = fba.tidyup(blocked)
243 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
246 print("WARNING: blocked is empty:", blocker)
248 elif blocked.count("*") < 1:
249 # No obsfucation for this instance
251 "SELECT hash FROM instances WHERE domain = ? LIMIT 1", [blocked]
254 if fba.cursor.fetchone() == None:
255 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
256 fba.add_instance(blocked, blocker, origin)
258 # Doing the hash search for instance names as well to tidy up DB
260 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
262 searchres = fba.cursor.fetchone()
264 if searchres != None:
265 # NOISY-DEBUG: print("DEBUG: Updating domain: ", searchres[0])
266 blocked = searchres[0]
269 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
272 blocked if blocked.count("*") <= 1 else blocked_hash,
277 if fba.cursor.fetchone() == None:
278 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level)
280 if block_level == "reject":
287 fba.update_last_seen(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
290 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
291 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
293 fba.connection.commit()
294 except Exception as e:
295 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
296 elif software == "friendica" or software == "misskey" or software == "bookwyrm" or software == "takahe":
297 print("INFO: blocker:", blocker)
299 if software == "friendica":
300 json = fba.get_friendica_blocks(blocker)
301 elif software == "misskey":
302 json = fba.get_misskey_blocks(blocker)
303 elif software == "bookwyrm":
304 print("WARNING: bookwyrm is not fully supported for fetching blacklist!", blocker)
305 #json = fba.get_bookwyrm_blocks(blocker)
306 elif software == "takahe":
307 print("WARNING: takahe is not fully supported for fetching blacklist!", blocker)
308 #json = fba.get_takahe_blocks(blocker)
310 for block_level, blocks in json.items():
311 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
312 block_level = fba.tidyup(block_level)
313 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
314 if block_level == "":
315 print("WARNING: block_level is empty, blocker:", blocker)
318 for instance in blocks:
319 blocked, reason = instance.values()
320 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
321 blocked = fba.tidyup(blocked)
322 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
325 print("WARNING: blocked is empty:", blocker)
327 if blocked.count("*") > 0:
328 # Some friendica servers also obscure domains without hash
330 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
332 searchres = fba.cursor.fetchone()
333 if searchres != None:
334 blocked = searchres[0]
336 if blocked.count("?") > 0:
337 # Some obscure them with question marks, not sure if that's dependent on version or not
339 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("?", "_")]
341 searchres = fba.cursor.fetchone()
342 if searchres != None:
343 blocked = searchres[0]
344 origin = searchres[1]
345 nodeinfo_url = searchres[2]
347 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
348 if not fba.is_instance_registered(blocked):
349 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
350 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
353 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
357 if fba.cursor.fetchone() == None:
358 fba.block_instance(blocker, blocked, reason, block_level)
360 if block_level == "reject":
367 fba.update_last_seen(blocker, blocked, block_level)
370 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
371 fba.update_block_reason(reason, blocker, blocked, block_level)
373 fba.connection.commit()
374 except Exception as e:
375 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
376 elif software == "gotosocial":
377 print("INFO: blocker:", blocker)
380 federation = reqto.get(f"https://{blocker}{get_peers_url}?filter=suspended", headers=fba.api_headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
382 if (federation == None):
383 print("WARNING: No valid response:", blocker);
384 elif "error" in federation:
385 print("WARNING: API returned error:", federation["error"])
387 # NOISY-DEBUG: print("DEBUG: Checking fenderation():", len(federation))
388 for peer in federation:
389 blocked = peer["domain"].lower()
390 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
391 blocked = fba.tidyup(blocked)
392 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
395 print("WARNING: blocked is empty:", blocker)
397 elif blocked.count("*") > 0:
398 # GTS does not have hashes for obscured domains, so we have to guess it
400 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
402 searchres = fba.cursor.fetchone()
404 if searchres != None:
405 blocked = searchres[0]
406 origin = searchres[1]
407 nodeinfo_url = searchres[2]
409 if not fba.is_instance_registered(blocked):
410 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
411 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
414 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
422 if fba.cursor.fetchone() == None:
423 # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point")
424 fba.block_instance(blocker, blocked, "unknown", "reject")
432 fba.update_last_seen(blocker, blocked, "reject")
434 if "public_comment" in peer:
435 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
436 fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
438 for entry in blockdict:
439 if entry["blocked"] == blocked:
440 # NOISY-DEBUG: print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'")
441 entry["reason"] = peer["public_comment"]
443 fba.connection.commit()
444 except Exception as e:
445 print(f"ERROR: blocker='{blocker}',software='{software}',exception[{type(e)}]:'{str(e)}'")
447 print("WARNING: Unknown software:", blocker, software)
449 if fba.config["bot_enabled"] and len(blockdict) > 0:
450 send_bot_post(blocker, blockdict)
454 fba.connection.close()