9 "SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial', 'bookwyrm') AND (last_blocked IS NULL OR last_blocked < ?) ORDER BY rowid DESC", [time.time() - fba.config["recheck_block"]]
12 for blocker, software in fba.cursor.fetchall():
13 # NOISY-DEBUG: print("DEBUG: BEFORE blocker,software:", blocker, software)
15 blocker = fba.tidyup(blocker)
16 # NOISY-DEBUG: print("DEBUG: AFTER blocker,software:", blocker, software)
19 print("WARNING: blocker is now empty!")
22 fba.update_last_blocked(blocker)
24 if software == "pleroma":
25 print("INFO: blocker:", blocker)
28 json = fba.fetch_nodeinfo(blocker)
30 print("WARNING: Could not fetch nodeinfo from blocker:", blocker)
33 print("DEBUG: Updating nodeinfo:", blocker)
34 fba.update_last_nodeinfo(blocker)
36 federation = json["metadata"]["federation"]
38 if "enabled" in federation:
39 # NOISY-DEBUG: print("DEBUG: Instance has no block list to analyze:", blocker)
42 if "mrf_simple" in federation:
43 for block_level, blocks in (
44 {**federation["mrf_simple"],
45 **{"quarantined_instances": federation["quarantined_instances"]}}
47 # NOISY-DEBUG: print("DEBUG: block_level, blocks():", block_level, len(blocks))
48 block_level = fba.tidyup(block_level)
49 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
52 print("WARNING: block_level is now empty!")
55 for blocked in blocks:
56 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
57 blocked = fba.tidyup(blocked)
58 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
61 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
64 if blocked.count("*") > 1:
65 # -ACK!-oma also started obscuring domains without hash
67 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
69 searchres = fba.cursor.fetchone()
70 # NOISY-DEBUG: print("DEBUG: searchres[]:", type(searchres))
72 blocked = searchres[0]
73 # NOISY-DEBUG: print("DEBUG: Looked up domain:", blocked)
75 # NOISY-DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
77 "SELECT domain FROM instances WHERE domain = ?", [blocked]
80 if fba.cursor.fetchone() == None:
81 # NOISY-DEBUG: print("DEBUG: Domain wasn't found, adding:", blocked, blocker)
82 fba.add_instance(blocked, blocker, argv[0])
85 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
93 if fba.cursor.fetchone() == None:
94 # NOISY-DEBUG: print("DEBUG: Blocking:", blocker, blocked, block_level)
95 fba.block_instance(blocker, blocked, "unknown", block_level)
97 if block_level == "reject":
98 # NOISY-DEBUG: print("DEBUG: Adding to blockdict:", blocked)
105 # NOISY-DEBUG: print("DEBUG: Updating last_seen:", blocker, blocked, block_level)
106 fba.update_last_seen(blocker, blocked, block_level)
108 fba.connection.commit()
111 if "mrf_simple_info" in federation:
112 # NOISY-DEBUG: print("DEBUG: Found mrf_simple_info:", blocker)
113 for block_level, info in (
114 {**federation["mrf_simple_info"],
115 **(federation["quarantined_instances_info"]
116 if "quarantined_instances_info" in federation
119 # NOISY-DEBUG: print("DEBUG: block_level, info.items():", block_level, len(info.items()))
120 block_level = fba.tidyup(block_level)
121 # NOISY-DEBUG: print("DEBUG: BEFORE block_level:", block_level)
123 if block_level == "":
124 print("WARNING: block_level is now empty!")
127 for blocked, reason in info.items():
128 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
129 blocked = fba.tidyup(blocked)
130 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
133 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
135 elif blocked.count("*") > 1:
136 # same domain guess as above, but for reasons field
138 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
140 searchres = fba.cursor.fetchone()
142 if searchres != None:
143 blocked = searchres[0]
145 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason["reason"])
146 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
148 for entry in blockdict:
149 if entry["blocked"] == blocked:
150 # NOISY-DEBUG: print("DEBUG: Updating entry reason:", blocked)
151 entry["reason"] = reason["reason"]
153 fba.connection.commit()
154 except Exception as e:
155 print("error:", e, blocker, software)
156 elif software == "mastodon":
157 print("INFO: blocker:", blocker)
159 # json endpoint for newer mastodongs
164 "followers_only": [],
168 # handling CSRF, I've saw at least one server requiring it to access the endpoint
169 # NOISY-DEBUG: print("DEBUG: Fetching meta:", blocker)
170 meta = bs4.BeautifulSoup(
171 reqto.get(f"https://{blocker}/about", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).text,
175 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
176 # NOISY-DEBUG: print("DEBUG: Adding CSRF token:", blocker, csrf)
177 reqheaders = {**fba.headers, **{"x-csrf-token": csrf}}
179 # NOISY-DEBUG: print("DEBUG: No CSRF token found, using normal headers:", blocker)
180 reqheaders = fba.headers
182 # NOISY-DEBUG: print("DEBUG: Quering API domain_blocks:", blocker)
183 blocks = reqto.get(f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
185 # NOISY-DEBUG: print("DEBUG: blocks():", len(blocks))
188 'domain': block['domain'],
189 'hash': block['digest'],
190 'reason': block['comment']
193 # NOISY-DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
194 if block['severity'] == 'suspend':
195 json['reject'].append(entry)
196 elif block['severity'] == 'silence':
197 json['followers_only'].append(entry)
198 elif block['severity'] == 'reject_media':
199 json['media_removal'].append(entry)
200 elif block['severity'] == 'reject_reports':
201 json['report_removal'].append(entry)
203 print("WARNING: Unknown severity:", block['severity'], block['domain'])
205 # NOISY-DEBUG: print("DEBUG: Failed, Trying mastodon-specific fetches:", blocker)
206 json = fba.get_mastodon_blocks(blocker)
208 # NOISY-DEBUG: print("DEBUG: json.items():", blocker, len(json.items()))
209 for block_level, blocks in json.items():
210 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
211 block_level = fba.tidyup(block_level)
212 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
213 if block_level == "":
214 print("WARNING: block_level is empty, blocker:", blocker)
217 for instance in blocks:
218 blocked, blocked_hash, reason = instance.values()
219 # NOISY-DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
220 blocked = fba.tidyup(blocked)
221 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
224 print("WARNING: blocked is empty:", blocker)
226 elif blocked.count("*") < 1:
227 # No obsfucation for this instance
229 "SELECT hash FROM instances WHERE domain = ?", [blocked]
232 if fba.cursor.fetchone() == None:
233 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
234 fba.add_instance(blocked, blocker, argv[0])
236 # Doing the hash search for instance names as well to tidy up DB
238 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
240 searchres = fba.cursor.fetchone()
242 if searchres != None:
243 # NOISY-DEBUG: print("DEBUG: Updating domain: ", searchres[0])
244 blocked = searchres[0]
247 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
250 blocked if blocked.count("*") <= 1 else blocked_hash,
255 if fba.cursor.fetchone() == None:
256 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level)
258 if block_level == "reject":
265 fba.update_last_seen(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
268 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
269 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
271 fba.connection.commit()
272 except Exception as e:
273 print("error:", e, blocker, software)
274 elif software == "friendica" or software == "misskey" or software == "bookwyrm":
275 print("INFO: blocker:", blocker)
277 if software == "friendica":
278 json = fba.get_friendica_blocks(blocker)
279 elif software == "misskey":
280 json = fba.get_misskey_blocks(blocker)
281 elif software == "bookwyrm":
282 json = fba.get_bookwyrm_blocks(blocker)
284 for block_level, blocks in json.items():
285 # NOISY-DEBUG: print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
286 block_level = fba.tidyup(block_level)
287 # NOISY-DEBUG: print("DEBUG: AFTER-block_level:", block_level)
288 if block_level == "":
289 print("WARNING: block_level is empty, blocker:", blocker)
292 for instance in blocks:
293 blocked, reason = instance.values()
294 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
295 blocked = fba.tidyup(blocked)
296 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
299 print("WARNING: blocked is empty:", blocker)
301 if blocked.count("*") > 0:
302 # Some friendica servers also obscure domains without hash
304 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
306 searchres = fba.cursor.fetchone()
307 if searchres != None:
308 blocked = searchres[0]
310 if blocked.count("?") > 0:
311 # Some obscure them with question marks, not sure if that's dependent on version or not
313 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("?", "_")]
315 searchres = fba.cursor.fetchone()
316 if searchres != None:
317 blocked = searchres[0]
319 # NOISY-DEBUG: print("DEBUG: AFTER-blocked:", blocked)
321 "SELECT domain FROM instances WHERE domain = ?", [blocked]
324 if fba.cursor.fetchone() == None:
325 # NOISY-DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
326 fba.add_instance(blocked, blocker)
329 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
332 if fba.cursor.fetchone() == None:
333 fba.block_instance(blocker, blocked, reason, block_level)
335 if block_level == "reject":
342 fba.update_last_seen(blocker, blocked, block_level)
345 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
346 fba.update_block_reason(reason, blocker, blocked, block_level)
348 fba.connection.commit()
349 except Exception as e:
350 print("error:", e, blocker, software)
351 elif software == "gotosocial":
352 print("INFO: blocker:", blocker)
355 federation = reqto.get(f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=fba.headers, timeout=(fba.config["connection_timeout"], config["read_timeout"])).json()
357 if (federation == None):
358 print("WARNING: No valid response:", blocker);
359 elif "error" in federation:
360 print("WARNING: API returned error:", federation["error"])
362 # NOISY-DEBUG: print("DEBUG: Checking fenderation():", len(federation))
363 for peer in federation:
364 blocked = peer["domain"].lower()
365 # NOISY-DEBUG: print("DEBUG: BEFORE blocked:", blocked)
366 blocked = fba.tidyup(blocked)
367 # NOISY-DEBUG: print("DEBUG: AFTER blocked:", blocked)
370 print("WARNING: blocked is empty:", blocker)
372 elif blocked.count("*") > 0:
373 # GTS does not have hashes for obscured domains, so we have to guess it
375 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [blocked.replace("*", "_")]
377 searchres = fba.cursor.fetchone()
379 if searchres != None:
380 blocked = searchres[0]
383 "SELECT domain FROM instances WHERE domain = ?", [blocked]
386 if fba.cursor.fetchone() == None:
387 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, reason)
388 fba.update_block_reason(reason, blocker, blocked, block_level)
391 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
399 if fba.cursor.fetchone() == None:
400 fba.block_instance(blocker, blocked, "unknown", "reject")
408 fba.update_last_seen(blocker, blocked, "reject")
410 if "public_comment" in peer:
411 # NOISY-DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
412 fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
414 for entry in blockdict:
415 if entry["blocked"] == blocked:
416 entry["reason"] = peer["public_comment"]
418 fba.connection.commit()
419 except Exception as e:
420 print("error:", e, blocker, software)
422 print("WARNING: Unknown software:", blocker, software)
424 if fba.config["bot_enabled"] and len(blockdict) > 0:
425 send_bot_post(blocker, blockdict)
429 fba.connection.close()