8 "SELECT domain, software FROM instances WHERE domain='tooting.intensifi.es'"
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=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 meta = bs4.BeautifulSoup(
122 reqto.get(f"https://{blocker}/about", headers=headers, timeout=5).text,
126 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
127 reqheaders = {**headers, **{"x-csrf-token": csrf}}
132 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
135 print("DEBUG: blocks():", len(blocks))
137 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
139 print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
140 if block['severity'] == 'suspend':
141 json['reject'].append(entry)
142 elif block['severity'] == 'silence':
143 json['followers_only'].append(entry)
144 elif block['severity'] == 'reject_media':
145 json['media_removal'].append(entry)
146 elif block['severity'] == 'reject_reports':
147 json['report_removal'].append(entry)
149 print("WARNING: Unknown severity:", block['severity'], block['domain'])
151 json = fba.get_mastodon_blocks(blocker)
153 for block_level, blocks in json.items():
154 for instance in blocks:
155 blocked, blocked_hash, reason = instance.values()
156 print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
158 blocked = fba.tidyup(blocked)
159 print("DEBUG: blocked:", blocked)
161 if blocked.count("*") < 1:
162 # No obsfucation for this instance
164 "SELECT hash FROM instances WHERE domain = ? LIMIT 1", (blocked,)
167 if fba.c.fetchone() == None:
168 print("DEBUG: Hash wasn't found, adding:", blocked)
169 fba.add_instance(blocked)
171 # Doing the hash search for instance names as well to tidy up DB
173 "SELECT domain FROM instances WHERE hash = ? LIMIT 1", (blocked_hash,)
175 searchres = fba.c.fetchone()
177 if searchres != None:
178 print("DEBUG: Updating domain: ", searchres[0])
179 blocked = searchres[0]
181 timestamp = int(time.time())
183 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
186 blocked if blocked.count("*") <= 1 else blocked_hash,
191 if fba.c.fetchone() == None:
192 fba.block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level, timestamp, timestamp)
194 if block_level == "reject":
201 fba.update_last_seen(timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
204 fba.update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
207 except Exception as e:
208 print("error:", e, blocker, software)
209 elif software == "friendica" or software == "misskey":
210 print("DEBUG: blocker:", blocker)
212 if software == "friendica":
213 json = fba.get_friendica_blocks(blocker)
214 elif software == "misskey":
215 json = fba.get_misskey_blocks(blocker)
216 for block_level, blocks in json.items():
217 for instance in blocks:
218 blocked, reason = instance.values()
219 blocked = fba.tidyup(blocked)
221 print("BEFORE-blocked:", blocked)
222 if blocked.count("*") > 0:
223 # Some friendica servers also obscure domains without hash
225 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
227 searchres = fba.c.fetchone()
228 if searchres != None:
229 blocked = searchres[0]
231 if blocked.count("?") > 0:
232 # Some obscure them with question marks, not sure if that's dependent on version or not
234 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("?", "_"),)
236 searchres = fba.c.fetchone()
237 if searchres != None:
238 blocked = searchres[0]
240 print("AFTER-blocked:", blocked)
242 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
245 if fba.c.fetchone() == None:
246 print("DEBUG: Hash wasn't found, adding:", blocked)
247 fba.add_instance(blocked)
249 timestamp = int(time.time())
251 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
254 if fba.c.fetchone() == None:
255 fba.block_instance(blocker, blocked, reason, block_level, timestamp, timestamp)
257 if block_level == "reject":
264 fba.update_last_seen(timestamp, blocker, blocked, block_level)
267 fba.update_block_reason(reason, blocker, blocked, block_level)
270 except Exception as e:
271 print("error:", e, blocker, software)
272 elif software == "gotosocial":
273 print("DEBUG: blocker:", blocker)
276 federation = reqto.get(
277 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
280 if (federation == None):
281 print("WARNING: No valid response:", blocker);
283 for peer in federation:
284 blocked = peer["domain"].lower()
286 if blocked.count("*") > 0:
287 # GTS does not have hashes for obscured domains, so we have to guess it
289 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
291 searchres = fba.c.fetchone()
293 if searchres != None:
294 blocked = searchres[0]
297 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
300 if fba.c.fetchone() == None:
301 print("DEBUG: Hash wasn't found, adding:", blocked)
302 fba.add_instance(blocked)
305 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
312 timestamp = int(time.time())
314 if fba.c.fetchone() == None:
315 fba.block_instance(blocker, blocked, "", "reject", timestamp, timestamp)
323 fba.update_last_seen(timestamp, blocker, blocked, "reject")
325 if "public_comment" in peer:
326 reason = peer["public_comment"]
327 fba.update_block_reason(reason, blocker, blocked, "reject")
329 for entry in blockdict:
330 if entry["blocked"] == blocked:
331 entry["reason"] = reason
333 except Exception as e:
334 print("error:", e, blocker, software)
336 print("WARNING: Unknown software:", software)
338 if fba.config["bot_enabled"] and len(blockdict) > 0:
339 send_bot_post(blocker, blockdict)