1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
25 "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"]]
28 rows = fba.cursor.fetchall()
29 print(f"INFO: Checking {len(rows)} entries ...")
30 for blocker, software, origin, nodeinfo_url in rows:
31 # NOISY-DEBUG: print("DEBUG: BEFORE blocker,software,origin,nodeinfo_url:", blocker, software, origin, nodeinfo_url)
33 blocker = fba.tidyup(blocker)
34 # NOISY-DEBUG: print("DEBUG: AFTER blocker,software:", blocker, software)
37 print("WARNING: blocker is now empty!")
39 elif fba.is_blacklisted(blocker):
40 print(f"WARNING: blocker='{blocker}' is blacklisted now!")
43 # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}'")
44 fba.update_last_blocked(blocker)
46 if software == "pleroma":
47 print("INFO: blocker:", blocker)
50 json = fba.fetch_nodeinfo(blocker, nodeinfo_url)
52 print("WARNING: Could not fetch nodeinfo from blocker:", blocker)
55 print("DEBUG: Updating nodeinfo:", blocker)
56 fba.update_last_nodeinfo(blocker)
58 federation = json["metadata"]["federation"]
60 if "enabled" in federation:
61 # NOISY-DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)
64 if "mrf_simple" in federation:
65 for block_level, blocks in (
66 {**federation["mrf_simple"],
67 **{"quarantined_instances": federation["quarantined_instances"]}}
69 # NOISY-DEBUG: print("DEBUG: block_level, blocks():", block_level, len(blocks))
70 block_level = fba.tidyup(block_level)
71 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
74 print("WARNING: block_level is now empty!")
77 for blocked in blocks:
78 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
79 blocked = fba.tidyup(blocked)
80 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
83 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
86 if blocked.count("*") > 1:
87 # -ACK!-oma also started obscuring domains without hash
89 "SELECT domain, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
91 searchres = fba.cursor.fetchone()
92 # NOISY-DEBUG: print("DEBUG: searchres[]:", type(searchres))
94 blocked = searchres[0]
95 nodeinfo_url = searchres[1]
96 # NOISY-DEBUG: print("DEBUG: Looked up domain:", blocked)
98 # NOISY-DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
99 if not fba.is_instance_registered(blocked):
100 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
101 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
104 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
112 if fba.cursor.fetchone() == None:
113 # NOISY-DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
114 fba.block_instance(blocker, blocked, "unknown", block_level)
116 if block_level == "reject":
117 # NOISY-DEBUG: print("DEBUG: Adding to blockdict:", blocked)
124 # NOISY-DEBUG: print("DEBUG: Updating last_seen:", blocker, blocked, block_level)
125 fba.update_last_seen(blocker, blocked, block_level)
127 fba.connection.commit()
130 if "mrf_simple_info" in federation:
131 # NOISY-DEBUG: print("DEBUG: Found mrf_simple_info:", blocker)
132 for block_level, info in (
133 {**federation["mrf_simple_info"],
134 **(federation["quarantined_instances_info"]
135 if "quarantined_instances_info" in federation
138 # NOISY-DEBUG: print("DEBUG: block_level, info.items():", block_level, len(info.items()))
139 block_level = fba.tidyup(block_level)
140 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
142 if block_level == "":
143 print("WARNING: block_level is now empty!")
146 for blocked, reason in info.items():
147 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
148 blocked = fba.tidyup(blocked)
149 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
152 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
154 elif blocked.count("*") > 1:
155 # same domain guess as above, but for reasons field
157 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
159 searchres = fba.cursor.fetchone()
161 if searchres != None:
162 blocked = searchres[0]
164 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason["reason"])
165 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
167 for entry in blockdict:
168 if entry["blocked"] == blocked:
169 # NOISY-DEBUG: print("DEBUG: Updating entry reason:", blocked)
170 entry["reason"] = reason["reason"]
172 fba.connection.commit()
173 except Exception as e:
174 print(f"ERROR: blocker='{blocker}',software='{software}',exception='{str(e)}'")
175 elif software == "mastodon":
176 print("INFO: blocker:", blocker)
178 # json endpoint for newer mastodongs
183 "followers_only": [],
187 # handling CSRF, I've saw at least one server requiring it to access the endpoint
188 # NOISY-DEBUG: print("DEBUG: Fetching meta:", blocker)
189 meta = bs4.BeautifulSoup(
190 reqto.get(f"https://{blocker}/about", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).text,
194 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
195 # NOISY-DEBUG: print("DEBUG: Adding CSRF token:", blocker, csrf)
196 reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
198 # NOISY-DEBUG: print("DEBUG: No CSRF token found, using normal headers:", blocker)
199 reqheaders = fba.api_headers
201 # NOISY-DEBUG: print("DEBUG: Quering API domain_blocks:", blocker)
202 blocks = reqto.get(f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
204 # NOISY-DEBUG: print("DEBUG: blocks():", len(blocks))
207 'domain': block['domain'],
208 'hash': block['digest'],
209 'reason': block['comment']
212 # NOISY-DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
213 if block['severity'] == 'suspend':
214 json['reject'].append(entry)
215 elif block['severity'] == 'silence':
216 json['followers_only'].append(entry)
217 elif block['severity'] == 'reject_media':
218 json['media_removal'].append(entry)
219 elif block['severity'] == 'reject_reports':
220 json['report_removal'].append(entry)
222 print("WARNING: Unknown severity:", block['severity'], block['domain'])
224 # NOISY-DEBUG: print("DEBUG: Failed, Trying mastodon-specific fetches:", blocker)
225 json = fba.get_mastodon_blocks(blocker)
227 # NOISY-DEBUG: print("DEBUG: json.items():", blocker, len(json.items()))
228 for block_level, blocks in json.items():
229 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
230 block_level = fba.tidyup(block_level)
231 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
232 if block_level == "":
233 print("WARNING: block_level is empty, blocker:", blocker)
236 for instance in blocks:
237 blocked, blocked_hash, reason = instance.values()
238 # NOISY-DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
239 blocked = fba.tidyup(blocked)
240 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
243 print("WARNING: blocked is empty:", blocker)
245 elif blocked.count("*") < 1:
246 # No obsfucation for this instance
248 "SELECT hash FROM instances WHERE domain = ? LIMIT 1", [blocked]
251 if fba.cursor.fetchone() == None:
252 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
253 fba.add_instance(blocked, blocker, origin)
255 # Doing the hash search for instance names as well to tidy up DB
257 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
259 searchres = fba.cursor.fetchone()
261 if searchres != None:
262 # NOISY-DEBUG: print("DEBUG: Updating domain: ", searchres[0])
263 blocked = searchres[0]
266 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
269 blocked if blocked.count("*") <= 1 else blocked_hash,
274 if fba.cursor.fetchone() == None:
275 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level)
277 if block_level == "reject":
284 fba.update_last_seen(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
287 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
288 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
290 fba.connection.commit()
291 except Exception as e:
292 print(f"ERROR: blocker='{blocker}',software='{software}',exception='{str(e)}'")
293 elif software == "friendica" or software == "misskey" or software == "bookwyrm" or software == "takahe":
294 print("INFO: blocker:", blocker)
296 if software == "friendica":
297 json = fba.get_friendica_blocks(blocker)
298 elif software == "misskey":
299 json = fba.get_misskey_blocks(blocker)
300 elif software == "bookwyrm":
301 print("WARNING: bookwyrm is not fully supported for fetching blacklist!", blocker)
302 #json = fba.get_bookwyrm_blocks(blocker)
303 elif software == "takahe":
304 print("WARNING: takahe is not fully supported for fetching blacklist!", blocker)
305 #json = fba.get_takahe_blocks(blocker)
307 for block_level, blocks in json.items():
308 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
309 block_level = fba.tidyup(block_level)
310 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
311 if block_level == "":
312 print("WARNING: block_level is empty, blocker:", blocker)
315 for instance in blocks:
316 blocked, reason = instance.values()
317 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
318 blocked = fba.tidyup(blocked)
319 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
322 print("WARNING: blocked is empty:", blocker)
324 if blocked.count("*") > 0:
325 # Some friendica servers also obscure domains without hash
327 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
329 searchres = fba.cursor.fetchone()
330 if searchres != None:
331 blocked = searchres[0]
333 if blocked.count("?") > 0:
334 # Some obscure them with question marks, not sure if that's dependent on version or not
336 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("?", "_")]
338 searchres = fba.cursor.fetchone()
339 if searchres != None:
340 blocked = searchres[0]
341 origin = searchres[1]
342 nodeinfo_url = searchres[2]
344 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
345 if not fba.is_instance_registered(blocked):
346 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
347 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
350 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
354 if fba.cursor.fetchone() == None:
355 fba.block_instance(blocker, blocked, reason, block_level)
357 if block_level == "reject":
364 fba.update_last_seen(blocker, blocked, block_level)
367 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
368 fba.update_block_reason(reason, blocker, blocked, block_level)
370 fba.connection.commit()
371 except Exception as e:
372 print(f"ERROR: blocker='{blocker}',software='{software}',exception='{str(e)}'")
373 elif software == "gotosocial":
374 print("INFO: blocker:", blocker)
377 federation = reqto.get(f"https://{blocker}{get_peers_url}?filter=suspended", headers=fba.api_headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
379 if (federation == None):
380 print("WARNING: No valid response:", blocker);
381 elif "error" in federation:
382 print("WARNING: API returned error:", federation["error"])
384 # NOISY-DEBUG: print("DEBUG: Checking fenderation():", len(federation))
385 for peer in federation:
386 blocked = peer["domain"].lower()
387 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
388 blocked = fba.tidyup(blocked)
389 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
392 print("WARNING: blocked is empty:", blocker)
394 elif blocked.count("*") > 0:
395 # GTS does not have hashes for obscured domains, so we have to guess it
397 "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
399 searchres = fba.cursor.fetchone()
401 if searchres != None:
402 blocked = searchres[0]
403 origin = searchres[1]
404 nodeinfo_url = searchres[2]
406 if not fba.is_instance_registered(blocked):
407 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
408 fba.add_instance(blocked, blocker, origin, nodeinfo_url)
411 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
419 if fba.cursor.fetchone() == None:
420 # NOISY-DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point")
421 fba.block_instance(blocker, blocked, "unknown", "reject")
429 fba.update_last_seen(blocker, blocked, "reject")
431 if "public_comment" in peer:
432 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
433 fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
435 for entry in blockdict:
436 if entry["blocked"] == blocked:
437 # NOISY-DEBUG: print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'")
438 entry["reason"] = peer["public_comment"]
440 fba.connection.commit()
441 except Exception as e:
442 print(f"ERROR: blocker='{blocker}',software='{software}',exception='{str(e)}'")
444 print("WARNING: Unknown software:", blocker, software)
446 if fba.config["bot_enabled"] and len(blockdict) > 0:
447 send_bot_post(blocker, blockdict)
451 fba.connection.close()