3 from hashlib import sha256
5 from bs4 import BeautifulSoup
12 with open("config.json") as f:
13 config = loads(f.read())
16 "user-agent": config["useragent"]
19 def send_bot_post(instance: str, blocks: dict):
20 message = instance + " has blocked the following instances:\n\n"
24 blocks = blocks[0 : 19]
26 if block["reason"] == None or block["reason"] == '':
27 message = message + block["blocked"] + " with unspecified reason\n"
29 if len(block["reason"]) > 420:
30 block["reason"] = block["reason"][0:419] + "[…]"
31 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
33 message = message + "(the list has been truncated to the first 20 entries)"
35 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
36 req = post(f"{config['bot_instance']}/api/v1/statuses",
37 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
38 headers=botheaders, timeout=10).json()
41 def get_mastodon_blocks(domain: str) -> dict:
43 "Suspended servers": [],
45 "Limited servers": [],
46 "Silenced servers": [],
50 "Silenced instances": "Silenced servers",
51 "Suspended instances": "Suspended servers",
52 "Gesperrte Server": "Suspended servers",
53 "Gefilterte Medien": "Filtered media",
54 "Stummgeschaltete Server": "Silenced servers",
55 "停止済みのサーバー": "Suspended servers",
56 "メディアを拒否しているサーバー": "Filtered media",
57 "サイレンス済みのサーバー": "Silenced servers",
58 "שרתים מושעים": "Suspended servers",
59 "מדיה מסוננת": "Filtered media",
60 "שרתים מוגבלים": "Silenced servers",
61 "Serveurs suspendus": "Suspended servers",
62 "Médias filtrés": "Filtered media",
63 "Serveurs limités": "Silenced servers",
68 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
74 for header in doc.find_all("h3"):
75 header_text = header.text
76 if header_text in translations:
77 header_text = translations[header_text]
78 if header_text in blocks:
79 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
80 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
81 blocks[header_text].append(
83 "domain": line.find("span").text,
84 "hash": line.find("span")["title"][9:],
85 "reason": line.find_all("td")[1].text.strip(),
89 "reject": blocks["Suspended servers"],
90 "media_removal": blocks["Filtered media"],
91 "followers_only": blocks["Limited servers"]
92 + blocks["Silenced servers"],
95 def get_friendica_blocks(domain: str) -> dict:
100 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
106 blocklist = doc.find(id="about_blocklist")
107 for line in blocklist.find("table").find_all("tr")[1:]:
110 "domain": line.find_all("td")[0].text.strip(),
111 "reason": line.find_all("td")[1].text.strip()
119 def get_pisskey_blocks(domain: str) -> dict:
129 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
132 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
135 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()
139 if instance["isSuspended"]:
140 blocks["suspended"].append(
142 "domain": instance["host"],
143 # no reason field, nothing
147 counter = counter + step
153 # same shit, different asshole ("blocked" aka full suspend)
156 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
159 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()
162 if instance["isBlocked"]:
163 blocks["blocked"].append(
165 "domain": instance["host"],
169 counter = counter + step
175 "reject": blocks["blocked"],
176 "followers_only": blocks["suspended"]
182 def get_hash(domain: str) -> str:
183 return sha256(domain.encode("utf-8")).hexdigest()
186 def get_type(domain: str) -> str:
188 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
189 if res.status_code == 404:
190 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
191 if res.status_code == 404:
192 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
193 if res.ok and "text/html" in res.headers["content-type"]:
194 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
196 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
198 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
200 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
203 return res.json()["software"]["name"]
204 elif res.status_code == 404:
205 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
211 def tidyup(domain: str) -> str:
212 # some retards put their blocks in variable case
213 domain = domain.lower()
214 # other retards put the port
215 domain = re.sub("\:\d+$", "", domain)
216 # bigger retards put the schema in their blocklist, sometimes even without slashes
217 domain = re.sub("^https?\:(\/*)", "", domain)
219 domain = re.sub("\/$", "", domain)
221 domain = re.sub("^\@", "", domain)
222 # the biggest retards of them all try to block individual users
223 domain = re.sub("(.+)\@", "", domain)
226 conn = sqlite3.connect("blocks.db")
230 "SELECT domain, software FROM instances WHERE software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
233 for blocker, software in c.fetchall():
235 blocker = tidyup(blocker)
236 if software == "pleroma":
241 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
242 ).json()["metadata"]["federation"]
243 if "mrf_simple" in federation:
244 for block_level, blocks in (
245 {**federation["mrf_simple"],
246 **{"quarantined_instances": federation["quarantined_instances"]}}
248 for blocked in blocks:
249 blocked = tidyup(blocked)
252 if blocked.count("*") > 1:
253 # -ACK!-oma also started obscuring domains without hash
255 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
257 searchres = c.fetchone()
258 if searchres != None:
259 blocked = searchres[0]
262 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
264 if c.fetchone() == None:
266 "INSERT INTO instances SELECT ?, ?, ?",
267 (blocked, get_hash(blocked), get_type(blocked)),
269 timestamp = int(time())
271 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
272 (blocker, blocked, block_level),
274 if c.fetchone() == None:
276 "INSERT INTO blocks SELECT ?, ?, '', ?, ?, ?",
277 (blocker, blocked, block_level, timestamp, timestamp),
279 if block_level == "reject":
287 "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
288 (timestamp, blocker, blocked, block_level)
292 if "mrf_simple_info" in federation:
293 for block_level, info in (
294 {**federation["mrf_simple_info"],
295 **(federation["quarantined_instances_info"]
296 if "quarantined_instances_info" in federation
299 for blocked, reason in info.items():
300 blocked = tidyup(blocked)
303 if blocked.count("*") > 1:
304 # same domain guess as above, but for reasons field
306 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
308 searchres = c.fetchone()
309 if searchres != None:
310 blocked = searchres[0]
312 "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
313 (reason["reason"], blocker, blocked, block_level),
315 for entry in blockdict:
316 if entry["blocked"] == blocked:
317 entry["reason"] = reason["reason"]
320 except Exception as e:
321 print("error:", e, blocker)
322 elif software == "mastodon":
325 # json endpoint for newer mastodongs
330 "followers_only": [],
334 # handling CSRF, I've saw at least one server requiring it to access the endpoint
335 meta = BeautifulSoup(
336 get(f"https://{blocker}/about", headers=headers, timeout=5).text,
340 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
341 reqheaders = {**headers, **{"x-csrf-token": csrf}}
346 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
349 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
350 if block['severity'] == 'suspend':
351 json['reject'].append(entry)
352 elif block['severity'] == 'silence':
353 json['followers_only'].append(entry)
354 elif block['severity'] == 'reject_media':
355 json['media_removal'].append(entry)
356 elif block['severity'] == 'reject_reports':
357 json['report_removal'].append(entry)
359 json = get_mastodon_blocks(blocker)
361 for block_level, blocks in json.items():
362 for instance in blocks:
363 blocked, blocked_hash, reason = instance.values()
364 blocked = tidyup(blocked)
365 if blocked.count("*") <= 1:
367 "SELECT hash FROM instances WHERE hash = ?", (blocked_hash,)
369 if c.fetchone() == None:
371 "INSERT INTO instances SELECT ?, ?, ?",
372 (blocked, get_hash(blocked), get_type(blocked)),
375 # Doing the hash search for instance names as well to tidy up DB
377 "SELECT domain FROM instances WHERE hash = ?", (blocked_hash,)
379 searchres = c.fetchone()
380 if searchres != None:
381 blocked = searchres[0]
383 timestamp = int(time())
385 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
386 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
388 if c.fetchone() == None:
390 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
393 blocked if blocked.count("*") <= 1 else blocked_hash,
400 if block_level == "reject":
408 "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
409 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
413 "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
414 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
417 except Exception as e:
418 print("error:", e, blocker)
419 elif software == "friendica" or software == "misskey":
422 if software == "friendica":
423 json = get_friendica_blocks(blocker)
424 elif software == "misskey":
425 json = get_pisskey_blocks(blocker)
426 for block_level, blocks in json.items():
427 for instance in blocks:
428 blocked, reason = instance.values()
429 blocked = tidyup(blocked)
431 if blocked.count("*") > 0:
432 # Some friendica servers also obscure domains without hash
434 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
436 searchres = c.fetchone()
437 if searchres != None:
438 blocked = searchres[0]
440 if blocked.count("?") > 0:
441 # Some obscure them with question marks, not sure if that's dependent on version or not
443 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("?", "_"),)
445 searchres = c.fetchone()
446 if searchres != None:
447 blocked = searchres[0]
450 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
452 if c.fetchone() == None:
454 "INSERT INTO instances SELECT ?, ?, ?",
455 (blocked, get_hash(blocked), get_type(blocked)),
458 timestamp = int(time())
460 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ?",
463 if c.fetchone() == None:
465 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
475 if block_level == "reject":
483 "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
484 (timestamp, blocker, blocked, block_level),
488 "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
489 (reason, blocker, blocked, block_level),
492 except Exception as e:
493 print("error:", e, blocker)
494 elif software == "gotosocial":
499 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
501 for peer in federation:
502 blocked = peer["domain"].lower()
504 if blocked.count("*") > 0:
505 # GTS does not have hashes for obscured domains, so we have to guess it
507 "SELECT domain FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
509 searchres = c.fetchone()
510 if searchres != None:
511 blocked = searchres[0]
514 "SELECT domain FROM instances WHERE domain = ?", (blocked,)
516 if c.fetchone() == None:
518 "INSERT INTO instances SELECT ?, ?, ?",
519 (blocked, get_hash(blocked), get_type(blocked)),
522 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
523 (blocker, blocked, "reject"),
525 timestamp = int(time())
526 if c.fetchone() == None:
528 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
529 (blocker, blocked, "", "reject", timestamp, timestamp),
538 "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
539 (timestamp, blocker, blocked, "reject"),
541 if "public_comment" in peer:
542 reason = peer["public_comment"]
544 "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
545 (reason, blocker, blocked, "reject"),
547 for entry in blockdict:
548 if entry["blocked"] == blocked:
549 entry["reason"] = reason
551 except Exception as e:
552 print("error:", e, blocker)
554 if config["bot_enabled"] and len(blockdict) > 0:
555 send_bot_post(blocker, blockdict)