9 "SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
12 for blocker, software in fba.c.fetchall():
13 print("DEBUG: blocker,software:", blocker, software)
15 blocker = fba.tidyup(blocker)
16 if software == "pleroma":
17 print("DEBUG: blocker:", blocker)
20 federation = reqto.get(
21 f"https://{blocker}/nodeinfo/2.1.json", headers=fba.headers, timeout=5
22 ).json()["metadata"]["federation"]
23 if "mrf_simple" in federation:
24 for block_level, blocks in (
25 {**federation["mrf_simple"],
26 **{"quarantined_instances": federation["quarantined_instances"]}}
28 for blocked in blocks:
29 blocked = fba.tidyup(blocked)
32 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
35 if blocked.count("*") > 1:
36 # -ACK!-oma also started obscuring domains without hash
38 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
40 searchres = fba.c.fetchone()
42 blocked = searchres[0]
45 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
48 if fba.c.fetchone() == None:
49 print("DEBUG: Hash wasn't found, adding:", blocked)
50 fba.add_instance(blocked)
52 timestamp = int(time.time())
54 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
61 if fba.c.fetchone() == None:
62 fba.block_instance(blocker, blocked, reason, block_level, timestamp, timestamp)
64 if block_level == "reject":
71 fba.update_last_seen(timestamp, blocker, blocked, block_level)
75 if "mrf_simple_info" in federation:
76 for block_level, info in (
77 {**federation["mrf_simple_info"],
78 **(federation["quarantined_instances_info"]
79 if "quarantined_instances_info" in federation
82 for blocked, reason in info.items():
83 blocked = fba.tidyup(blocked)
86 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
89 if blocked.count("*") > 1:
90 # same domain guess as above, but for reasons field
92 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
94 searchres = fba.c.fetchone()
97 blocked = searchres[0]
99 fba.update_block_reason(reason["reason"], blocker, blocked, block_level)
101 for entry in blockdict:
102 if entry["blocked"] == blocked:
103 entry["reason"] = reason["reason"]
106 except Exception as e:
107 print("error:", e, blocker, software)
108 elif software == "mastodon":
109 print("DEBUG: blocker:", blocker)
111 # json endpoint for newer mastodongs
116 "followers_only": [],
120 # handling CSRF, I've saw at least one server requiring it to access the endpoint
121 print("DEBUG: Fetching meta:", blocker)
122 meta = bs4.BeautifulSoup(
123 reqto.get(f"https://{blocker}/about", headers=fba.headers, timeout=5).text,
127 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
128 print("DEBUG: Adding CSRF token:", blocker, csrf)
129 reqheaders = {**fba.headers, **{"x-csrf-token": csrf}}
131 print("DEBUG: No CSRF token found, using normal headers:", blocker)
132 reqheaders = fba.headers
134 print("DEBUG: Quering API domain_blocks:", blocker)
136 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
139 print("DEBUG: blocks():", len(blocks))
141 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
143 print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
144 if block['severity'] == 'suspend':
145 json['reject'].append(entry)
146 elif block['severity'] == 'silence':
147 json['followers_only'].append(entry)
148 elif block['severity'] == 'reject_media':
149 json['media_removal'].append(entry)
150 elif block['severity'] == 'reject_reports':
151 json['report_removal'].append(entry)
153 print("WARNING: Unknown severity:", block['severity'], block['domain'])
155 print("DEBUG: Failed, Trying mastodon-specific fetches:", blocker)
156 json = fba.get_mastodon_blocks(blocker)
158 print("DEBUG: json.items():", blocker, len(json.items()))
159 for block_level, blocks in json.items():
160 print("DEBUG: blocker,block_level,blocks():", blocker, block_level, len(blocks))
161 for instance in blocks:
162 blocked, blocked_hash, reason = instance.values()
163 print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
165 blocked = fba.tidyup(blocked)
166 print("DEBUG: blocked:", blocked)
168 if blocked.count("*") < 1:
169 # No obsfucation for this instance
171 "SELECT hash FROM instances WHERE domain = ? LIMIT 1", (blocked,)
174 if fba.c.fetchone() == None:
175 print("DEBUG: Hash wasn't found, adding:", blocked)
176 fba.add_instance(blocked)
178 # Doing the hash search for instance names as well to tidy up DB
180 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", (blocked_hash,)
182 searchres = fba.c.fetchone()
184 if searchres != None:
185 print("DEBUG: Updating domain: ", searchres[0])
186 blocked = searchres[0]
188 timestamp = int(time.time())
190 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
193 blocked if blocked.count("*") <= 1 else blocked_hash,
198 if fba.c.fetchone() == None:
199 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level, timestamp, timestamp)
201 if block_level == "reject":
208 fba.update_last_seen(timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
211 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
214 except Exception as e:
215 print("error:", e, blocker, software)
216 elif software == "friendica" or software == "misskey":
217 print("DEBUG: blocker:", blocker)
219 if software == "friendica":
220 json = fba.get_friendica_blocks(blocker)
221 elif software == "misskey":
222 json = fba.get_misskey_blocks(blocker)
223 for block_level, blocks in json.items():
224 for instance in blocks:
225 blocked, reason = instance.values()
226 blocked = fba.tidyup(blocked)
228 print("BEFORE-blocked:", blocked)
229 if blocked.count("*") > 0:
230 # Some friendica servers also obscure domains without hash
232 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
234 searchres = fba.c.fetchone()
235 if searchres != None:
236 blocked = searchres[0]
238 if blocked.count("?") > 0:
239 # Some obscure them with question marks, not sure if that's dependent on version or not
241 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("?", "_"),)
243 searchres = fba.c.fetchone()
244 if searchres != None:
245 blocked = searchres[0]
247 print("AFTER-blocked:", blocked)
249 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
252 if fba.c.fetchone() == None:
253 print("DEBUG: Hash wasn't found, adding:", blocked)
254 fba.add_instance(blocked)
256 timestamp = int(time.time())
258 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
261 if fba.c.fetchone() == None:
262 fba.block_instance(blocker, blocked, reason, block_level, timestamp, timestamp)
264 if block_level == "reject":
271 fba.update_last_seen(timestamp, blocker, blocked, block_level)
274 fba.update_block_reason(reason, blocker, blocked, block_level)
277 except Exception as e:
278 print("error:", e, blocker, software)
279 elif software == "gotosocial":
280 print("DEBUG: blocker:", blocker)
283 federation = reqto.get(
284 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=fba.headers, timeout=5
287 if (federation == None):
288 print("WARNING: No valid response:", blocker);
290 for peer in federation:
291 print("DEBUG: peer(),[]:", len(peer), type(peer))
292 if (isinstance(peer, str) and peer == "error"):
293 print("WARNING: Cannot continue, maybe authentication required?", blocker)
296 blocked = peer["domain"].lower()
297 print("DEBUG: blocked:", blocked)
299 if blocked.count("*") > 0:
300 # GTS does not have hashes for obscured domains, so we have to guess it
302 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
304 searchres = fba.c.fetchone()
306 if searchres != None:
307 blocked = searchres[0]
310 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
313 if fba.c.fetchone() == None:
314 print("DEBUG: Hash wasn't found, adding:", blocked)
315 fba.add_instance(blocked)
318 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
325 timestamp = int(time.time())
327 if fba.c.fetchone() == None:
328 fba.block_instance(blocker, blocked, "", "reject", timestamp, timestamp)
336 fba.update_last_seen(timestamp, blocker, blocked, "reject")
338 if "public_comment" in peer:
339 reason = peer["public_comment"]
340 fba.update_block_reason(reason, blocker, blocked, "reject")
342 for entry in blockdict:
343 if entry["blocked"] == blocked:
344 entry["reason"] = reason
346 except Exception as e:
347 print("error:", e, blocker, software)
349 print("WARNING: Unknown software:", software)
351 if fba.config["bot_enabled"] and len(blockdict) > 0:
352 send_bot_post(blocker, blockdict)