1 from requests import get
2 from requests import post
3 from hashlib import sha256
5 from bs4 import BeautifulSoup
11 "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"
15 def get_mastodon_blocks(domain: str) -> dict:
17 "Suspended servers": [],
19 "Limited servers": [],
20 "Silenced servers": [],
24 "Silenced instances": "Silenced servers",
25 "Suspended instances": "Suspended servers",
26 "Gesperrte Server": "Suspended servers",
27 "Gefilterte Medien": "Filtered media",
28 "Stummgeschaltete Server": "Silenced servers",
29 "停止済みのサーバー": "Suspended servers",
30 "メディアを拒否しているサーバー": "Filtered media",
31 "サイレンス済みのサーバー": "Silenced servers",
32 "Serveurs suspendus": "Suspended servers",
33 "Médias filtrés": "Filtered media",
34 "Serveurs limités": "Silenced servers",
39 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
45 for header in doc.find_all("h3"):
46 header_text = header.text
47 if header_text in translations:
48 header_text = translations[header_text]
49 if header_text in blocks:
50 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
51 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
52 blocks[header_text].append(
54 "domain": line.find("span").text,
55 "hash": line.find("span")["title"][9:],
56 "reason": line.find_all("td")[1].text.strip(),
60 "reject": blocks["Suspended servers"],
61 "media_removal": blocks["Filtered media"],
62 "followers_only": blocks["Limited servers"]
63 + blocks["Silenced servers"],
66 def get_friendica_blocks(domain: str) -> dict:
71 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
77 blocklist = doc.find(id="about_blocklist")
78 for line in blocklist.find("table").find_all("tr")[1:]:
81 "domain": line.find_all("td")[0].text.strip(),
82 "reason": line.find_all("td")[1].text.strip()
90 def get_pisskey_blocks(domain: str) -> dict:
100 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
103 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
106 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()
110 if instance["isSuspended"]:
111 blocks["suspended"].append(
113 "domain": instance["host"],
114 # no reason field, nothing
118 counter = counter + step
124 # same shit, different asshole ("blocked" aka full suspend)
127 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
130 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()
133 if instance["isBlocked"]:
134 blocks["blocked"].append(
136 "domain": instance["host"],
140 counter = counter + step
146 "reject": blocks["blocked"],
147 "followers_only": blocks["suspended"]
153 def get_hash(domain: str) -> str:
154 return sha256(domain.encode("utf-8")).hexdigest()
157 def get_type(domain: str) -> str:
159 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
160 if res.status_code == 404:
161 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
162 if res.status_code == 404:
163 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
164 if res.ok and "text/html" in res.headers["content-type"]:
165 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
167 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
169 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
171 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
174 return res.json()["software"]["name"]
175 elif res.status_code == 404:
176 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
182 def tidyup(domain: str) -> str:
183 # some retards put their blocks in variable case
184 domain = domain.lower()
185 # other retards put the port
186 domain = re.sub("\:\d+$", "", domain)
187 # bigger retards put the schema in their blocklist, sometimes even without slashes
188 domain = re.sub("^https?\:(\/*)", "", domain)
190 domain = re.sub("\/$", "", domain)
191 # the biggest retards of them all try to block individual users
192 domain = re.sub("(.+)\@", "", domain)
195 conn = sqlite3.connect("blocks.db")
199 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
202 for blocker, software in c.fetchall():
203 blocker = tidyup(blocker)
204 if software == "pleroma":
209 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
210 ).json()["metadata"]["federation"]
211 if "mrf_simple" in federation:
212 for block_level, blocks in (
213 {**federation["mrf_simple"],
214 **{"quarantined_instances": federation["quarantined_instances"]}}
216 for blocked in blocks:
217 blocked = tidyup(blocked)
220 if blocked.count("*") > 1:
221 # -ACK!-oma also started obscuring domains without hash
223 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
225 searchres = c.fetchone()
226 if searchres != None:
227 blocked = searchres[0]
230 "select domain from instances where domain = ?", (blocked,)
232 if c.fetchone() == None:
234 "insert into instances select ?, ?, ?",
235 (blocked, get_hash(blocked), get_type(blocked)),
237 timestamp = int(time())
239 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
240 (blocker, blocked, block_level),
242 if c.fetchone() == None:
244 "insert into blocks select ?, ?, '', ?, ?, ?",
245 (blocker, blocked, block_level, timestamp, timestamp),
249 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
250 (timestamp, blocker, blocked, block_level)
254 if "mrf_simple_info" in federation:
255 for block_level, info in (
256 {**federation["mrf_simple_info"],
257 **(federation["quarantined_instances_info"]
258 if "quarantined_instances_info" in federation
261 for blocked, reason in info.items():
262 blocked = tidyup(blocked)
265 if blocked.count("*") > 1:
266 # same domain guess as above, but for reasons field
268 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
270 searchres = c.fetchone()
271 if searchres != None:
272 blocked = searchres[0]
274 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
275 (reason["reason"], blocker, blocked, block_level),
278 except Exception as e:
279 print("error:", e, blocker)
280 elif software == "mastodon":
283 # json endpoint for newer mastodongs
288 "followers_only": [],
292 f"https://{blocker}/api/v1/instance/domain_blocks", headers=headers, timeout=5
295 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
296 if block['severity'] == 'suspend':
297 json['reject'].append(entry)
298 elif block['severity'] == 'silence':
299 json['followers_only'].append(entry)
300 elif block['severity'] == 'reject_media':
301 json['media_removal'].append(entry)
302 elif block['severity'] == 'reject_reports':
303 json['report_removal'].append(entry)
305 json = get_mastodon_blocks(blocker)
307 for block_level, blocks in json.items():
308 for instance in blocks:
309 blocked, blocked_hash, reason = instance.values()
310 blocked = tidyup(blocked)
311 if blocked.count("*") <= 1:
313 "select hash from instances where hash = ?", (blocked_hash,)
315 if c.fetchone() == None:
317 "insert into instances select ?, ?, ?",
318 (blocked, get_hash(blocked), get_type(blocked)),
321 # Doing the hash search for instance names as well to tidy up DB
323 "select domain from instances where hash = ?", (blocked_hash,)
325 searchres = c.fetchone()
326 if searchres != None:
327 blocked = searchres[0]
329 timestamp = int(time())
331 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
332 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
334 if c.fetchone() == None:
336 "insert into blocks select ?, ?, ?, ?, ?, ?",
339 blocked if blocked.count("*") <= 1 else blocked_hash,
348 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
349 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
353 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
354 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
357 except Exception as e:
358 print("error:", e, blocker)
359 elif software == "friendica" or software == "misskey":
362 if software == "friendica":
363 json = get_friendica_blocks(blocker)
364 elif software == "misskey":
365 json = get_pisskey_blocks(blocker)
366 for block_level, blocks in json.items():
367 for instance in blocks:
368 blocked, reason = instance.values()
369 blocked = tidyup(blocked)
371 if blocked.count("*") > 0:
372 # Some friendica servers also obscure domains without hash
374 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
376 searchres = c.fetchone()
377 if searchres != None:
378 blocked = searchres[0]
380 if blocked.count("?") > 0:
381 # Some obscure them with question marks, not sure if that's dependent on version or not
383 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("?", "_"),)
385 searchres = c.fetchone()
386 if searchres != None:
387 blocked = searchres[0]
390 "select domain from instances where domain = ?", (blocked,)
392 if c.fetchone() == None:
394 "insert into instances select ?, ?, ?",
395 (blocked, get_hash(blocked), get_type(blocked)),
398 timestamp = int(time())
400 "select * from blocks where blocker = ? and blocked = ? and reason = ?",
401 (blocker, blocked, reason),
403 if c.fetchone() == None:
405 "insert into blocks select ?, ?, ?, ?, ?, ?",
417 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
418 (timestamp, blocker, blocked, block_level),
422 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
423 (reason, blocker, blocked, block_level),
426 except Exception as e:
427 print("error:", e, blocker)
428 elif software == "gotosocial":
433 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
435 for peer in federation:
436 blocked = peer["domain"].lower()
438 if blocked.count("*") > 0:
439 # GTS does not have hashes for obscured domains, so we have to guess it
441 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
443 searchres = c.fetchone()
444 if searchres != None:
445 blocked = searchres[0]
448 "select domain from instances where domain = ?", (blocked,)
450 if c.fetchone() == None:
452 "insert into instances select ?, ?, ?",
453 (blocked, get_hash(blocked), get_type(blocked)),
456 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
457 (blocker, blocked, "reject"),
459 timestamp = int(time())
460 if c.fetchone() == None:
462 "insert into blocks select ?, ?, ?, ?, ?, ?",
463 (blocker, blocked, "", "reject", timestamp, timestamp),
467 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
468 (timestamp, blocker, blocked, "reject"),
470 if "public_comment" in peer:
471 reason = peer["public_comment"]
473 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
474 (reason, blocker, blocked, "reject"),
477 except Exception as e:
478 print("error:", e, blocker)