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)
188 # the biggest retards of them all try to block individual users
189 domain = re.sub("(.+)\@", "", domain)
192 conn = sqlite3.connect("blocks.db")
196 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
199 for blocker, software in c.fetchall():
200 blocker = tidyup(blocker)
201 if software == "pleroma":
206 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
207 ).json()["metadata"]["federation"]
208 if "mrf_simple" in federation:
209 for block_level, blocks in (
210 {**federation["mrf_simple"],
211 **{"quarantined_instances": federation["quarantined_instances"]}}
213 for blocked in blocks:
214 blocked = tidyup(blocked)
217 if blocked.count("*") > 1:
218 # -ACK!-oma also started obscuring domains without hash
220 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
222 searchres = c.fetchone()
223 if searchres != None:
224 blocked = searchres[0]
227 "select domain from instances where domain = ?", (blocked,)
229 if c.fetchone() == None:
231 "insert into instances select ?, ?, ?",
232 (blocked, get_hash(blocked), get_type(blocked)),
235 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
236 (blocker, blocked, block_level),
238 if c.fetchone() == None:
240 "insert into blocks select ?, ?, '', ?",
241 (blocker, blocked, block_level),
245 if "mrf_simple_info" in federation:
246 for block_level, info in (
247 {**federation["mrf_simple_info"],
248 **(federation["quarantined_instances_info"]
249 if "quarantined_instances_info" in federation
252 for blocked, reason in info.items():
253 blocked = tidyup(blocked)
255 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
256 (reason["reason"], blocker, blocked, block_level),
259 except Exception as e:
260 print("error:", e, blocker)
261 elif software == "mastodon":
264 json = get_mastodon_blocks(blocker)
265 for block_level, blocks in json.items():
266 for instance in blocks:
267 blocked, blocked_hash, reason = instance.values()
268 blocked = tidyup(blocked)
269 if blocked.count("*") <= 1:
271 "select hash from instances where hash = ?", (blocked_hash,)
273 if c.fetchone() == None:
275 "insert into instances select ?, ?, ?",
276 (blocked, get_hash(blocked), get_type(blocked)),
279 # Doing the hash search for instance names as well to tidy up DB
281 "select domain from instances where hash = ?", (blocked_hash,)
283 searchres = c.fetchone()
284 if searchres != None:
285 blocked = searchres[0]
288 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
289 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
291 if c.fetchone() == None:
293 "insert into blocks select ?, ?, ?, ?",
296 blocked if blocked.count("*") <= 1 else blocked_hash,
302 except Exception as e:
303 print("error:", e, blocker)
304 elif software == "friendica" or software == "misskey":
307 if software == "friendica":
308 json = get_friendica_blocks(blocker)
309 elif software == "misskey":
310 json = get_pisskey_blocks(blocker)
311 for block_level, blocks in json.items():
312 for instance in blocks:
313 blocked, reason = instance.values()
314 blocked = tidyup(blocked)
316 if blocked.count("*") > 0:
317 # Some friendica servers also obscure domains without hash
319 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
321 searchres = c.fetchone()
322 if searchres != None:
323 blocked = searchres[0]
326 "select domain from instances where domain = ?", (blocked,)
328 if c.fetchone() == None:
330 "insert into instances select ?, ?, ?",
331 (blocked, get_hash(blocked), get_type(blocked)),
334 "select * from blocks where blocker = ? and blocked = ?",
337 if c.fetchone() == None:
339 "insert into blocks select ?, ?, ?, ?",
348 except Exception as e:
349 print("error:", e, blocker)
350 elif software == "gotosocial":
355 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
357 for peer in federation:
358 blocked = peer["domain"].lower()
360 if blocked.count("*") > 0:
361 # GTS does not have hashes for obscured domains, so we have to guess it
363 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
365 searchres = c.fetchone()
366 if searchres != None:
367 blocked = searchres[0]
370 "select domain from instances where domain = ?", (blocked,)
372 if c.fetchone() == None:
374 "insert into instances select ?, ?, ?",
375 (blocked, get_hash(blocked), get_type(blocked)),
378 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
379 (blocker, blocked, "reject"),
381 if c.fetchone() == None:
383 "insert into blocks select ?, ?, ?, ?",
384 (blocker, blocked, "", "reject"),
387 if "public_comment" in peer:
388 reason = peer["public_comment"]
390 "select * from blocks where blocker = ? and blocked = ? and reason != ? and block_level = ?",
391 (blocker, blocked, "", "reject"),
393 if c.fetchone() == None:
395 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ?",
396 (reason, blocker, blocked, "reject"),
399 except Exception as e:
400 print("error:", e, blocker)