1 from requests import get
2 from requests import post
3 from hashlib import sha256
5 from bs4 import BeautifulSoup
10 "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"
14 def get_mastodon_blocks(domain: str) -> dict:
16 "Suspended servers": [],
18 "Limited servers": [],
19 "Silenced servers": [],
23 "Silenced instances": "Silenced servers",
24 "Suspended instances": "Suspended servers",
25 "Gesperrte Server": "Suspended servers",
26 "Gefilterte Medien": "Filtered media",
27 "Stummgeschaltete Server": "Silenced servers",
28 "停止済みのサーバー": "Suspended servers",
29 "メディアを拒否しているサーバー": "Filtered media",
30 "サイレンス済みのサーバー": "Silenced servers",
31 "Serveurs suspendus": "Suspended servers",
32 "Médias filtrés": "Filtered media",
33 "Serveurs limités": "Silenced servers",
38 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
44 for header in doc.find_all("h3"):
45 header_text = header.text
46 if header_text in translations:
47 header_text = translations[header_text]
48 if header_text in blocks:
49 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
50 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
51 blocks[header_text].append(
53 "domain": line.find("span").text,
54 "hash": line.find("span")["title"][9:],
55 "reason": line.find_all("td")[1].text.strip(),
59 "reject": blocks["Suspended servers"],
60 "media_removal": blocks["Filtered media"],
61 "federated_timeline_removal": blocks["Limited servers"]
62 + blocks["Silenced servers"],
65 def get_friendica_blocks(domain: str) -> dict:
70 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
76 blocklist = doc.find(id="about_blocklist")
77 for line in blocklist.find("table").find_all("tr")[1:]:
80 "domain": line.find_all("td")[0].text.strip(),
81 "reason": line.find_all("td")[1].text.strip()
89 def get_pisskey_blocks(domain: str) -> dict:
99 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
102 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
105 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step,"offset":counter-1}), headers=headers, timeout=5).json()
109 if instance["isSuspended"]:
110 blocks["suspended"].append(
112 "domain": instance["host"],
113 # no reason field, nothing
117 counter = counter + step
123 # same shit, different asshole ("blocked" aka full suspend)
126 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
129 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step,"offset":counter-1}), headers=headers, timeout=5).json()
132 if instance["isBlocked"]:
133 blocks["blocked"].append(
135 "domain": instance["host"],
139 counter = counter + step
145 "reject": blocks["blocked"],
146 "followers_only": blocks["suspended"]
152 def get_hash(domain: str) -> str:
153 return sha256(domain.encode("utf-8")).hexdigest()
156 def get_type(domain: str) -> str:
158 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
159 if res.status_code == 404:
160 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
161 if res.status_code == 404:
162 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
163 if res.ok and "text/html" in res.headers["content-type"]:
164 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
166 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
168 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
170 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey"]:
173 return res.json()["software"]["name"]
174 elif res.status_code == 404:
175 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
182 conn = sqlite3.connect("blocks.db")
186 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
189 for blocker, software in c.fetchall():
190 if software == "pleroma":
195 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
196 ).json()["metadata"]["federation"]
197 if "mrf_simple" in federation:
198 for block_level, blocks in (
199 {**federation["mrf_simple"],
200 **{"quarantined_instances": federation["quarantined_instances"]}}
202 for blocked in blocks:
205 blocked = blocked.lower()
206 blocker = blocker.lower()
208 "select domain from instances where domain = ?", (blocked,)
210 if c.fetchone() == None:
212 "insert into instances select ?, ?, ?",
213 (blocked, get_hash(blocked), get_type(blocked)),
216 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
217 (blocker, blocked, block_level),
219 if c.fetchone() == None:
221 "insert into blocks select ?, ?, '', ?",
222 (blocker, blocked, block_level),
226 if "mrf_simple_info" in federation:
227 for block_level, info in (
228 {**federation["mrf_simple_info"],
229 **(federation["quarantined_instances_info"]
230 if "quarantined_instances_info" in federation
233 for blocked, reason in info.items():
234 blocker = blocker.lower()
235 blocked = blocked.lower()
237 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
238 (reason["reason"], blocker, blocked, block_level),
241 except Exception as e:
242 print("error:", e, blocker)
243 elif software == "mastodon":
246 json = get_mastodon_blocks(blocker)
247 for block_level, blocks in json.items():
248 for instance in blocks:
249 blocked, blocked_hash, reason = instance.values()
250 blocked = blocked.lower()
251 blocker = blocker.lower()
252 if blocked.count("*") <= 1:
254 "select hash from instances where hash = ?", (blocked_hash,)
256 if c.fetchone() == None:
258 "insert into instances select ?, ?, ?",
259 (blocked, get_hash(blocked), get_type(blocked)),
262 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
263 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
265 if c.fetchone() == None:
267 "insert into blocks select ?, ?, ?, ?",
270 blocked if blocked.count("*") <= 1 else blocked_hash,
276 except Exception as e:
277 print("error:", e, blocker)
278 elif software == "friendica" or software == "misskey":
281 if software == "friendica":
282 json = get_friendica_blocks(blocker)
283 elif software == "misskey":
284 json = get_pisskey_blocks(blocker)
285 for block_level, blocks in json.items():
286 for instance in blocks:
287 blocked, reason = instance.values()
288 blocked = blocked.lower()
289 blocker = blocker.lower()
291 "select domain from instances where domain = ?", (blocked,)
293 if c.fetchone() == None:
295 "insert into instances select ?, ?, ?",
296 (blocked, get_hash(blocked), get_type(blocked)),
299 "select * from blocks where blocker = ? and blocked = ?",
302 if c.fetchone() == None:
304 "insert into blocks select ?, ?, ?, ?",
313 except Exception as e:
314 print("error:", e, blocker)
315 elif software == "gotosocial":
320 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
323 for peer in federation:
324 blocked = peer["domain"].lower()
325 blocker = blocker.lower()
327 if blocked.count("*") > 0:
328 # GTS does not have hashes for obscured domains, so we have to guess it
330 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
332 searchres = c.fetchone()
333 if searchres != None:
334 blocked = searchres[0]
337 "select domain from instances where domain = ?", (blocked,)
339 if c.fetchone() == None:
341 "insert into instances select ?, ?, ?",
342 (blocked, get_hash(blocked), get_type(blocked)),
345 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
346 (blocker, blocked, "reject"),
348 if c.fetchone() == None:
350 "insert into blocks select ?, ?, ?, ?",
351 (blocker, blocked, "", "reject"),
354 if "public_comment" in peer:
355 reason = peer["public_comment"]
357 "select * from blocks where blocker = ? and blocked = ? and reason != ? and block_level = ?",
358 (blocker, blocked, "", "reject"),
360 if c.fetchone() == None:
362 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
363 (reason, blocker, blocked, "reject"),
366 except Exception as e:
367 print("error:", e, blocker)