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)
181 def tidyup(domain: str) -> str:
182 # some retards put their blocks in variable case
183 domain = domain.lower()
184 # other retards put the port
185 domain = re.sub("\:\d+$", "", domain)
186 # bigger retards put the schema in their blocklist, sometimes even without slashes
187 domain = re.sub("^https?\:(\/*)", "", domain)
189 domain = re.sub("\/$", "", domain)
190 # the biggest retards of them all try to block individual users
191 domain = re.sub("(.+)\@", "", domain)
194 conn = sqlite3.connect("blocks.db")
198 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
201 for blocker, software in c.fetchall():
202 blocker = tidyup(blocker)
203 if software == "pleroma":
208 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
209 ).json()["metadata"]["federation"]
210 if "mrf_simple" in federation:
211 for block_level, blocks in (
212 {**federation["mrf_simple"],
213 **{"quarantined_instances": federation["quarantined_instances"]}}
215 for blocked in blocks:
216 blocked = tidyup(blocked)
219 if blocked.count("*") > 1:
220 # -ACK!-oma also started obscuring domains without hash
222 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
224 searchres = c.fetchone()
225 if searchres != None:
226 blocked = searchres[0]
229 "select domain from instances where domain = ?", (blocked,)
231 if c.fetchone() == None:
233 "insert into instances select ?, ?, ?",
234 (blocked, get_hash(blocked), get_type(blocked)),
237 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
238 (blocker, blocked, block_level),
240 if c.fetchone() == None:
242 "insert into blocks select ?, ?, '', ?",
243 (blocker, blocked, block_level),
247 if "mrf_simple_info" in federation:
248 for block_level, info in (
249 {**federation["mrf_simple_info"],
250 **(federation["quarantined_instances_info"]
251 if "quarantined_instances_info" in federation
254 for blocked, reason in info.items():
255 blocked = tidyup(blocked)
257 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
258 (reason["reason"], blocker, blocked, block_level),
261 except Exception as e:
262 print("error:", e, blocker)
263 elif software == "mastodon":
266 json = get_mastodon_blocks(blocker)
267 for block_level, blocks in json.items():
268 for instance in blocks:
269 blocked, blocked_hash, reason = instance.values()
270 blocked = tidyup(blocked)
271 if blocked.count("*") <= 1:
273 "select hash from instances where hash = ?", (blocked_hash,)
275 if c.fetchone() == None:
277 "insert into instances select ?, ?, ?",
278 (blocked, get_hash(blocked), get_type(blocked)),
281 # Doing the hash search for instance names as well to tidy up DB
283 "select domain from instances where hash = ?", (blocked_hash,)
285 searchres = c.fetchone()
286 if searchres != None:
287 blocked = searchres[0]
290 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
291 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
293 if c.fetchone() == None:
295 "insert into blocks select ?, ?, ?, ?",
298 blocked if blocked.count("*") <= 1 else blocked_hash,
304 except Exception as e:
305 print("error:", e, blocker)
306 elif software == "friendica" or software == "misskey":
309 if software == "friendica":
310 json = get_friendica_blocks(blocker)
311 elif software == "misskey":
312 json = get_pisskey_blocks(blocker)
313 for block_level, blocks in json.items():
314 for instance in blocks:
315 blocked, reason = instance.values()
316 blocked = tidyup(blocked)
318 if blocked.count("*") > 0:
319 # Some friendica servers also obscure domains without hash
321 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
323 searchres = c.fetchone()
324 if searchres != None:
325 blocked = searchres[0]
328 "select domain from instances where domain = ?", (blocked,)
330 if c.fetchone() == None:
332 "insert into instances select ?, ?, ?",
333 (blocked, get_hash(blocked), get_type(blocked)),
336 "select * from blocks where blocker = ? and blocked = ?",
339 if c.fetchone() == None:
341 "insert into blocks select ?, ?, ?, ?",
350 except Exception as e:
351 print("error:", e, blocker)
352 elif software == "gotosocial":
357 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
359 for peer in federation:
360 blocked = peer["domain"].lower()
362 if blocked.count("*") > 0:
363 # GTS does not have hashes for obscured domains, so we have to guess it
365 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
367 searchres = c.fetchone()
368 if searchres != None:
369 blocked = searchres[0]
372 "select domain from instances where domain = ?", (blocked,)
374 if c.fetchone() == None:
376 "insert into instances select ?, ?, ?",
377 (blocked, get_hash(blocked), get_type(blocked)),
380 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
381 (blocker, blocked, "reject"),
383 if c.fetchone() == None:
385 "insert into blocks select ?, ?, ?, ?",
386 (blocker, blocked, "", "reject"),
389 if "public_comment" in peer:
390 reason = peer["public_comment"]
392 "select * from blocks where blocker = ? and blocked = ? and reason != ? and block_level = ?",
393 (blocker, blocked, "", "reject"),
395 if c.fetchone() == None:
397 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
398 (reason, blocker, blocked, "reject"),
401 except Exception as e:
402 print("error:", e, blocker)