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 message = message + block["blocked"] + ' for "' + block["reason"] + '"\n'
31 message = message + "(the list has been truncated to the first 20 entries)"
33 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
34 req = post(f"{config['bot_instance']}/api/v1/statuses",
35 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
36 headers=botheaders, timeout=10).json()
40 def get_mastodon_blocks(domain: str) -> dict:
42 "Suspended servers": [],
44 "Limited servers": [],
45 "Silenced servers": [],
49 "Silenced instances": "Silenced servers",
50 "Suspended instances": "Suspended servers",
51 "Gesperrte Server": "Suspended servers",
52 "Gefilterte Medien": "Filtered media",
53 "Stummgeschaltete Server": "Silenced servers",
54 "停止済みのサーバー": "Suspended servers",
55 "メディアを拒否しているサーバー": "Filtered media",
56 "サイレンス済みのサーバー": "Silenced servers",
57 "שרתים מושעים": "Suspended servers",
58 "מדיה מסוננת": "Filtered media",
59 "שרתים מוגבלים": "Silenced servers",
60 "Serveurs suspendus": "Suspended servers",
61 "Médias filtrés": "Filtered media",
62 "Serveurs limités": "Silenced servers",
67 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
73 for header in doc.find_all("h3"):
74 header_text = header.text
75 if header_text in translations:
76 header_text = translations[header_text]
77 if header_text in blocks:
78 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
79 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
80 blocks[header_text].append(
82 "domain": line.find("span").text,
83 "hash": line.find("span")["title"][9:],
84 "reason": line.find_all("td")[1].text.strip(),
88 "reject": blocks["Suspended servers"],
89 "media_removal": blocks["Filtered media"],
90 "followers_only": blocks["Limited servers"]
91 + blocks["Silenced servers"],
94 def get_friendica_blocks(domain: str) -> dict:
99 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
105 blocklist = doc.find(id="about_blocklist")
106 for line in blocklist.find("table").find_all("tr")[1:]:
109 "domain": line.find_all("td")[0].text.strip(),
110 "reason": line.find_all("td")[1].text.strip()
118 def get_pisskey_blocks(domain: str) -> dict:
128 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
131 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
134 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()
138 if instance["isSuspended"]:
139 blocks["suspended"].append(
141 "domain": instance["host"],
142 # no reason field, nothing
146 counter = counter + step
152 # same shit, different asshole ("blocked" aka full suspend)
155 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
158 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()
161 if instance["isBlocked"]:
162 blocks["blocked"].append(
164 "domain": instance["host"],
168 counter = counter + step
174 "reject": blocks["blocked"],
175 "followers_only": blocks["suspended"]
181 def get_hash(domain: str) -> str:
182 return sha256(domain.encode("utf-8")).hexdigest()
185 def get_type(domain: str) -> str:
187 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
188 if res.status_code == 404:
189 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
190 if res.status_code == 404:
191 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
192 if res.ok and "text/html" in res.headers["content-type"]:
193 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
195 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
197 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
199 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
202 return res.json()["software"]["name"]
203 elif res.status_code == 404:
204 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
210 def tidyup(domain: str) -> str:
211 # some retards put their blocks in variable case
212 domain = domain.lower()
213 # other retards put the port
214 domain = re.sub("\:\d+$", "", domain)
215 # bigger retards put the schema in their blocklist, sometimes even without slashes
216 domain = re.sub("^https?\:(\/*)", "", domain)
218 domain = re.sub("\/$", "", domain)
220 domain = re.sub("^\@", "", domain)
221 # the biggest retards of them all try to block individual users
222 domain = re.sub("(.+)\@", "", domain)
225 conn = sqlite3.connect("blocks.db")
229 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
232 for blocker, software in c.fetchall():
234 blocker = tidyup(blocker)
235 if software == "pleroma":
240 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
241 ).json()["metadata"]["federation"]
242 if "mrf_simple" in federation:
243 for block_level, blocks in (
244 {**federation["mrf_simple"],
245 **{"quarantined_instances": federation["quarantined_instances"]}}
247 for blocked in blocks:
248 blocked = tidyup(blocked)
251 if blocked.count("*") > 1:
252 # -ACK!-oma also started obscuring domains without hash
254 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
256 searchres = c.fetchone()
257 if searchres != None:
258 blocked = searchres[0]
261 "select domain from instances where domain = ?", (blocked,)
263 if c.fetchone() == None:
265 "insert into instances select ?, ?, ?",
266 (blocked, get_hash(blocked), get_type(blocked)),
268 timestamp = int(time())
270 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
271 (blocker, blocked, block_level),
273 if c.fetchone() == None:
275 "insert into blocks select ?, ?, '', ?, ?, ?",
276 (blocker, blocked, block_level, timestamp, timestamp),
278 if block_level == "reject":
286 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
287 (timestamp, blocker, blocked, block_level)
291 if "mrf_simple_info" in federation:
292 for block_level, info in (
293 {**federation["mrf_simple_info"],
294 **(federation["quarantined_instances_info"]
295 if "quarantined_instances_info" in federation
298 for blocked, reason in info.items():
299 blocked = tidyup(blocked)
302 if blocked.count("*") > 1:
303 # same domain guess as above, but for reasons field
305 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
307 searchres = c.fetchone()
308 if searchres != None:
309 blocked = searchres[0]
311 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
312 (reason["reason"], blocker, blocked, block_level),
314 for entry in blockdict:
315 if entry["blocked"] == blocked:
316 entry["reason"] = reason["reason"]
319 except Exception as e:
320 print("error:", e, blocker)
321 elif software == "mastodon":
324 # json endpoint for newer mastodongs
329 "followers_only": [],
333 # handling CSRF, I've saw at least one server requiring it to access the endpoint
334 meta = BeautifulSoup(
335 get(f"https://{blocker}/about", headers=headers, timeout=5).text,
339 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
340 reqheaders = {**headers, **{"x-csrf-token": csrf}}
345 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
348 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
349 if block['severity'] == 'suspend':
350 json['reject'].append(entry)
351 elif block['severity'] == 'silence':
352 json['followers_only'].append(entry)
353 elif block['severity'] == 'reject_media':
354 json['media_removal'].append(entry)
355 elif block['severity'] == 'reject_reports':
356 json['report_removal'].append(entry)
358 json = get_mastodon_blocks(blocker)
360 for block_level, blocks in json.items():
361 for instance in blocks:
362 blocked, blocked_hash, reason = instance.values()
363 blocked = tidyup(blocked)
364 if blocked.count("*") <= 1:
366 "select hash from instances where hash = ?", (blocked_hash,)
368 if c.fetchone() == None:
370 "insert into instances select ?, ?, ?",
371 (blocked, get_hash(blocked), get_type(blocked)),
374 # Doing the hash search for instance names as well to tidy up DB
376 "select domain from instances where hash = ?", (blocked_hash,)
378 searchres = c.fetchone()
379 if searchres != None:
380 blocked = searchres[0]
382 timestamp = int(time())
384 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
385 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
387 if c.fetchone() == None:
389 "insert into blocks select ?, ?, ?, ?, ?, ?",
392 blocked if blocked.count("*") <= 1 else blocked_hash,
399 if block_level == "reject":
407 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
408 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
412 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
413 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
416 except Exception as e:
417 print("error:", e, blocker)
418 elif software == "friendica" or software == "misskey":
421 if software == "friendica":
422 json = get_friendica_blocks(blocker)
423 elif software == "misskey":
424 json = get_pisskey_blocks(blocker)
425 for block_level, blocks in json.items():
426 for instance in blocks:
427 blocked, reason = instance.values()
428 blocked = tidyup(blocked)
430 if blocked.count("*") > 0:
431 # Some friendica servers also obscure domains without hash
433 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
435 searchres = c.fetchone()
436 if searchres != None:
437 blocked = searchres[0]
439 if blocked.count("?") > 0:
440 # Some obscure them with question marks, not sure if that's dependent on version or not
442 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("?", "_"),)
444 searchres = c.fetchone()
445 if searchres != None:
446 blocked = searchres[0]
449 "select domain from instances where domain = ?", (blocked,)
451 if c.fetchone() == None:
453 "insert into instances select ?, ?, ?",
454 (blocked, get_hash(blocked), get_type(blocked)),
457 timestamp = int(time())
459 "select * from blocks where blocker = ? and blocked = ?",
462 if c.fetchone() == None:
464 "insert into blocks select ?, ?, ?, ?, ?, ?",
474 if block_level == "reject":
482 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
483 (timestamp, blocker, blocked, block_level),
487 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
488 (reason, blocker, blocked, block_level),
491 except Exception as e:
492 print("error:", e, blocker)
493 elif software == "gotosocial":
498 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
500 for peer in federation:
501 blocked = peer["domain"].lower()
503 if blocked.count("*") > 0:
504 # GTS does not have hashes for obscured domains, so we have to guess it
506 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
508 searchres = c.fetchone()
509 if searchres != None:
510 blocked = searchres[0]
513 "select domain from instances where domain = ?", (blocked,)
515 if c.fetchone() == None:
517 "insert into instances select ?, ?, ?",
518 (blocked, get_hash(blocked), get_type(blocked)),
521 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
522 (blocker, blocked, "reject"),
524 timestamp = int(time())
525 if c.fetchone() == None:
527 "insert into blocks select ?, ?, ?, ?, ?, ?",
528 (blocker, blocked, "", "reject", timestamp, timestamp),
537 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
538 (timestamp, blocker, blocked, "reject"),
540 if "public_comment" in peer:
541 reason = peer["public_comment"]
543 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
544 (reason, blocker, blocked, "reject"),
546 for entry in blockdict:
547 if entry["blocked"] == blocked:
548 entry["reason"] = reason
550 except Exception as e:
551 print("error:", e, blocker)
553 if config["bot_enabled"] and len(blockdict) > 0:
554 send_bot_post(blocker, blockdict)