3 from hashlib import sha256
4 from bs4 import BeautifulSoup
14 "SELECT domain, software FROM instances WHERE domain='tooting.intensifi.es'"
15 #"SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
18 for blocker, software in c.fetchall():
20 blocker = fba.tidyup(blocker)
21 if software == "pleroma":
26 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
27 ).json()["metadata"]["federation"]
28 if "mrf_simple" in federation:
29 for block_level, blocks in (
30 {**federation["mrf_simple"],
31 **{"quarantined_instances": federation["quarantined_instances"]}}
33 for blocked in blocks:
34 blocked = fba.tidyup(blocked)
37 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
40 if blocked.count("*") > 1:
41 # -ACK!-oma also started obscuring domains without hash
43 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
45 searchres = c.fetchone()
47 blocked = searchres[0]
50 "SELECT domain FROM instances WHERE domain = ?", (blocked)
53 if c.fetchone() == None:
56 timestamp = int(time())
58 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
65 if c.fetchone() == None:
66 block_instance(blocker, blocked, reason, block_level, timestamp, timestamp)
68 if block_level == "reject":
75 update_last_seen(timestamp, blocker, blocked, block_level)
78 if "mrf_simple_info" in federation:
79 for block_level, info in (
80 {**federation["mrf_simple_info"],
81 **(federation["quarantined_instances_info"]
82 if "quarantined_instances_info" in federation
85 for blocked, reason in info.items():
86 blocked = fba.tidyup(blocked)
89 print("WARNING: blocked is empty after fba.tidyup():", blocker, block_level)
92 if blocked.count("*") > 1:
93 # same domain guess as above, but for reasons field
95 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
97 searchres = c.fetchone()
100 blocked = searchres[0]
102 update_block_reason(reason["reason"], blocker, blocked, block_level)
104 for entry in blockdict:
105 if entry["blocked"] == blocked:
106 entry["reason"] = reason["reason"]
109 except Exception as e:
110 print("error:", e, blocker)
111 elif software == "mastodon":
114 # json endpoint for newer mastodongs
119 "followers_only": [],
123 # handling CSRF, I've saw at least one server requiring it to access the endpoint
124 meta = BeautifulSoup(
125 get(f"https://{blocker}/about", headers=headers, timeout=5).text,
129 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
130 reqheaders = {**headers, **{"x-csrf-token": csrf}}
135 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
139 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': 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 blocked = fba.tidyup(blocked)
158 if blocked.count("*") <= 1:
160 "SELECT hash FROM instances WHERE hash = ?", (blocked_hash,)
163 if c.fetchone() == None:
164 add_instance(blocked)
166 # Doing the hash search for instance names as well to tidy up DB
168 "SELECT domain FROM instances WHERE hash = ?", (blocked_hash,)
170 searchres = c.fetchone()
171 if searchres != None:
172 blocked = searchres[0]
174 timestamp = int(time())
176 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
179 blocked if blocked.count("*") <= 1 else blocked_hash,
183 if c.fetchone() == None:
184 block_instance(blocker, blocked if blocked.count("*") <= 1 else blocked_hash, reason, block_level, timestamp, timestamp)
186 if block_level == "reject":
193 update_last_seen(timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
196 update_block_reason(reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level)
199 except Exception as e:
200 print("error:", e, blocker)
201 elif software == "friendica" or software == "misskey":
204 if software == "friendica":
205 json = fba.get_friendica_blocks(blocker)
206 elif software == "misskey":
207 json = fba.get_misskey_blocks(blocker)
208 for block_level, blocks in json.items():
209 for instance in blocks:
210 blocked, reason = instance.values()
211 blocked = fba.tidyup(blocked)
213 print("BEFORE-blocked:", blocked)
214 if blocked.count("*") > 0:
215 # Some friendica servers also obscure domains without hash
217 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
219 searchres = c.fetchone()
220 if searchres != None:
221 blocked = searchres[0]
223 if blocked.count("?") > 0:
224 # Some obscure them with question marks, not sure if that's dependent on version or not
226 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("?", "_"),)
228 searchres = c.fetchone()
229 if searchres != None:
230 blocked = searchres[0]
232 print("AFTER-blocked:", blocked)
234 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
237 if c.fetchone() == None:
238 add_instance(blocked)
240 timestamp = int(time())
242 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
245 if c.fetchone() == None:
246 block_instance(blocker, blocked, reason, block_level, timestamp, timestamp)
248 if block_level == "reject":
255 update_last_seen(timestamp, blocker, blocked, block_level)
258 update_block_reason(reason, blocker, blocked, block_level)
261 except Exception as e:
262 print("error:", e, blocker)
263 elif software == "gotosocial":
268 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
271 if (federation == None):
272 print("WARNING: No valid response:", blocker);
274 for peer in federation:
275 blocked = peer["domain"].lower()
277 if blocked.count("*") > 0:
278 # GTS does not have hashes for obscured domains, so we have to guess it
280 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
282 searchres = c.fetchone()
284 if searchres != None:
285 blocked = searchres[0]
288 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
291 if c.fetchone() == None:
292 add_instance(blocked)
295 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
296 (blocker, blocked, "reject"),
298 timestamp = int(time())
300 if c.fetchone() == None:
301 block_instance(blocker, blocked, "", "reject", timestamp, timestamp)
309 update_last_seen(timestamp, blocker, blocked, "reject")
311 if "public_comment" in peer:
312 reason = peer["public_comment"]
313 update_block_reason(reason, blocker, blocked, "reject")
315 for entry in blockdict:
316 if entry["blocked"] == blocked:
317 entry["reason"] = reason
319 except Exception as e:
320 print("error:", e, blocker)
322 print("WARNING: Unknown software:", software)
324 if config["bot_enabled"] and len(blockdict) > 0:
325 send_bot_post(blocker, blockdict)