1 from requests import get
2 from requests import post
3 from hashlib import sha256
5 from bs4 import BeautifulSoup
11 with open("config.json") as f:
12 config = loads(f.read())
15 "user-agent": config["useragent"]
19 def get_mastodon_blocks(domain: str) -> dict:
21 "Suspended servers": [],
23 "Limited servers": [],
24 "Silenced servers": [],
28 "Silenced instances": "Silenced servers",
29 "Suspended instances": "Suspended servers",
30 "Gesperrte Server": "Suspended servers",
31 "Gefilterte Medien": "Filtered media",
32 "Stummgeschaltete Server": "Silenced servers",
33 "停止済みのサーバー": "Suspended servers",
34 "メディアを拒否しているサーバー": "Filtered media",
35 "サイレンス済みのサーバー": "Silenced servers",
36 "שרתים מושעים": "Suspended servers",
37 "מדיה מסוננת": "Filtered media",
38 "שרתים מוגבלים": "Silenced servers",
39 "Serveurs suspendus": "Suspended servers",
40 "Médias filtrés": "Filtered media",
41 "Serveurs limités": "Silenced servers",
46 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
52 for header in doc.find_all("h3"):
53 header_text = header.text
54 if header_text in translations:
55 header_text = translations[header_text]
56 if header_text in blocks:
57 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
58 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
59 blocks[header_text].append(
61 "domain": line.find("span").text,
62 "hash": line.find("span")["title"][9:],
63 "reason": line.find_all("td")[1].text.strip(),
67 "reject": blocks["Suspended servers"],
68 "media_removal": blocks["Filtered media"],
69 "followers_only": blocks["Limited servers"]
70 + blocks["Silenced servers"],
73 def get_friendica_blocks(domain: str) -> dict:
78 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
84 blocklist = doc.find(id="about_blocklist")
85 for line in blocklist.find("table").find_all("tr")[1:]:
88 "domain": line.find_all("td")[0].text.strip(),
89 "reason": line.find_all("td")[1].text.strip()
97 def get_pisskey_blocks(domain: str) -> dict:
107 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
110 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
113 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()
117 if instance["isSuspended"]:
118 blocks["suspended"].append(
120 "domain": instance["host"],
121 # no reason field, nothing
125 counter = counter + step
131 # same shit, different asshole ("blocked" aka full suspend)
134 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
137 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()
140 if instance["isBlocked"]:
141 blocks["blocked"].append(
143 "domain": instance["host"],
147 counter = counter + step
153 "reject": blocks["blocked"],
154 "followers_only": blocks["suspended"]
160 def get_hash(domain: str) -> str:
161 return sha256(domain.encode("utf-8")).hexdigest()
164 def get_type(domain: str) -> str:
166 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
167 if res.status_code == 404:
168 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
169 if res.status_code == 404:
170 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
171 if res.ok and "text/html" in res.headers["content-type"]:
172 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
174 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
176 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
178 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
181 return res.json()["software"]["name"]
182 elif res.status_code == 404:
183 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
189 def tidyup(domain: str) -> str:
190 # some retards put their blocks in variable case
191 domain = domain.lower()
192 # other retards put the port
193 domain = re.sub("\:\d+$", "", domain)
194 # bigger retards put the schema in their blocklist, sometimes even without slashes
195 domain = re.sub("^https?\:(\/*)", "", domain)
197 domain = re.sub("\/$", "", domain)
199 domain = re.sub("^\@", "", domain)
200 # the biggest retards of them all try to block individual users
201 domain = re.sub("(.+)\@", "", domain)
204 conn = sqlite3.connect("blocks.db")
208 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
211 for blocker, software in c.fetchall():
212 blocker = tidyup(blocker)
213 if software == "pleroma":
218 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
219 ).json()["metadata"]["federation"]
220 if "mrf_simple" in federation:
221 for block_level, blocks in (
222 {**federation["mrf_simple"],
223 **{"quarantined_instances": federation["quarantined_instances"]}}
225 for blocked in blocks:
226 blocked = tidyup(blocked)
229 if blocked.count("*") > 1:
230 # -ACK!-oma also started obscuring domains without hash
232 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
234 searchres = c.fetchone()
235 if searchres != None:
236 blocked = searchres[0]
239 "select domain from instances where domain = ?", (blocked,)
241 if c.fetchone() == None:
243 "insert into instances select ?, ?, ?",
244 (blocked, get_hash(blocked), get_type(blocked)),
246 timestamp = int(time())
248 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
249 (blocker, blocked, block_level),
251 if c.fetchone() == None:
253 "insert into blocks select ?, ?, '', ?, ?, ?",
254 (blocker, blocked, block_level, timestamp, timestamp),
258 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
259 (timestamp, blocker, blocked, block_level)
263 if "mrf_simple_info" in federation:
264 for block_level, info in (
265 {**federation["mrf_simple_info"],
266 **(federation["quarantined_instances_info"]
267 if "quarantined_instances_info" in federation
270 for blocked, reason in info.items():
271 blocked = tidyup(blocked)
274 if blocked.count("*") > 1:
275 # same domain guess as above, but for reasons field
277 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
279 searchres = c.fetchone()
280 if searchres != None:
281 blocked = searchres[0]
283 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
284 (reason["reason"], blocker, blocked, block_level),
287 except Exception as e:
288 print("error:", e, blocker)
289 elif software == "mastodon":
292 # json endpoint for newer mastodongs
297 "followers_only": [],
301 f"https://{blocker}/api/v1/instance/domain_blocks", headers=headers, timeout=5
304 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
305 if block['severity'] == 'suspend':
306 json['reject'].append(entry)
307 elif block['severity'] == 'silence':
308 json['followers_only'].append(entry)
309 elif block['severity'] == 'reject_media':
310 json['media_removal'].append(entry)
311 elif block['severity'] == 'reject_reports':
312 json['report_removal'].append(entry)
314 json = get_mastodon_blocks(blocker)
316 for block_level, blocks in json.items():
317 for instance in blocks:
318 blocked, blocked_hash, reason = instance.values()
319 blocked = tidyup(blocked)
320 if blocked.count("*") <= 1:
322 "select hash from instances where hash = ?", (blocked_hash,)
324 if c.fetchone() == None:
326 "insert into instances select ?, ?, ?",
327 (blocked, get_hash(blocked), get_type(blocked)),
330 # Doing the hash search for instance names as well to tidy up DB
332 "select domain from instances where hash = ?", (blocked_hash,)
334 searchres = c.fetchone()
335 if searchres != None:
336 blocked = searchres[0]
338 timestamp = int(time())
340 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
341 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
343 if c.fetchone() == None:
345 "insert into blocks select ?, ?, ?, ?, ?, ?",
348 blocked if blocked.count("*") <= 1 else blocked_hash,
357 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
358 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
362 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
363 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
366 except Exception as e:
367 print("error:", e, blocker)
368 elif software == "friendica" or software == "misskey":
371 if software == "friendica":
372 json = get_friendica_blocks(blocker)
373 elif software == "misskey":
374 json = get_pisskey_blocks(blocker)
375 for block_level, blocks in json.items():
376 for instance in blocks:
377 blocked, reason = instance.values()
378 blocked = tidyup(blocked)
380 if blocked.count("*") > 0:
381 # Some friendica servers also obscure domains without hash
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]
389 if blocked.count("?") > 0:
390 # Some obscure them with question marks, not sure if that's dependent on version or not
392 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("?", "_"),)
394 searchres = c.fetchone()
395 if searchres != None:
396 blocked = searchres[0]
399 "select domain from instances where domain = ?", (blocked,)
401 if c.fetchone() == None:
403 "insert into instances select ?, ?, ?",
404 (blocked, get_hash(blocked), get_type(blocked)),
407 timestamp = int(time())
409 "select * from blocks where blocker = ? and blocked = ? and reason = ?",
410 (blocker, blocked, reason),
412 if c.fetchone() == None:
414 "insert into blocks select ?, ?, ?, ?, ?, ?",
426 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
427 (timestamp, blocker, blocked, block_level),
431 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
432 (reason, blocker, blocked, block_level),
435 except Exception as e:
436 print("error:", e, blocker)
437 elif software == "gotosocial":
442 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
444 for peer in federation:
445 blocked = peer["domain"].lower()
447 if blocked.count("*") > 0:
448 # GTS does not have hashes for obscured domains, so we have to guess it
450 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
452 searchres = c.fetchone()
453 if searchres != None:
454 blocked = searchres[0]
457 "select domain from instances where domain = ?", (blocked,)
459 if c.fetchone() == None:
461 "insert into instances select ?, ?, ?",
462 (blocked, get_hash(blocked), get_type(blocked)),
465 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
466 (blocker, blocked, "reject"),
468 timestamp = int(time())
469 if c.fetchone() == None:
471 "insert into blocks select ?, ?, ?, ?, ?, ?",
472 (blocker, blocked, "", "reject", timestamp, timestamp),
476 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
477 (timestamp, blocker, blocked, "reject"),
479 if "public_comment" in peer:
480 reason = peer["public_comment"]
482 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
483 (reason, blocker, blocked, "reject"),
486 except Exception as e:
487 print("error:", e, blocker)