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()
39 def get_mastodon_blocks(domain: str) -> dict:
41 "Suspended servers": [],
43 "Limited servers": [],
44 "Silenced servers": [],
48 "Silenced instances": "Silenced servers",
49 "Suspended instances": "Suspended servers",
50 "Gesperrte Server": "Suspended servers",
51 "Gefilterte Medien": "Filtered media",
52 "Stummgeschaltete Server": "Silenced servers",
53 "停止済みのサーバー": "Suspended servers",
54 "メディアを拒否しているサーバー": "Filtered media",
55 "サイレンス済みのサーバー": "Silenced servers",
56 "שרתים מושעים": "Suspended servers",
57 "מדיה מסוננת": "Filtered media",
58 "שרתים מוגבלים": "Silenced servers",
59 "Serveurs suspendus": "Suspended servers",
60 "Médias filtrés": "Filtered media",
61 "Serveurs limités": "Silenced servers",
66 get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
72 for header in doc.find_all("h3"):
73 header_text = header.text
74 if header_text in translations:
75 header_text = translations[header_text]
76 if header_text in blocks:
77 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
78 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
79 blocks[header_text].append(
81 "domain": line.find("span").text,
82 "hash": line.find("span")["title"][9:],
83 "reason": line.find_all("td")[1].text.strip(),
87 "reject": blocks["Suspended servers"],
88 "media_removal": blocks["Filtered media"],
89 "followers_only": blocks["Limited servers"]
90 + blocks["Silenced servers"],
93 def get_friendica_blocks(domain: str) -> dict:
98 get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
104 blocklist = doc.find(id="about_blocklist")
105 for line in blocklist.find("table").find_all("tr")[1:]:
108 "domain": line.find_all("td")[0].text.strip(),
109 "reason": line.find_all("td")[1].text.strip()
117 def get_pisskey_blocks(domain: str) -> dict:
127 # iterating through all "suspended" (follow-only in its terminology) instances page-by-page, since that troonware doesn't support sending them all at once
130 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"suspended":True,"limit":step}), headers=headers, timeout=5).json()
133 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()
137 if instance["isSuspended"]:
138 blocks["suspended"].append(
140 "domain": instance["host"],
141 # no reason field, nothing
145 counter = counter + step
151 # same shit, different asshole ("blocked" aka full suspend)
154 doc = post(f"https://{domain}/api/federation/instances", data=dumps({"sort":"+caughtAt","host":None,"blocked":True,"limit":step}), headers=headers, timeout=5).json()
157 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()
160 if instance["isBlocked"]:
161 blocks["blocked"].append(
163 "domain": instance["host"],
167 counter = counter + step
173 "reject": blocks["blocked"],
174 "followers_only": blocks["suspended"]
180 def get_hash(domain: str) -> str:
181 return sha256(domain.encode("utf-8")).hexdigest()
184 def get_type(domain: str) -> str:
186 res = get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
187 if res.status_code == 404:
188 res = get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
189 if res.status_code == 404:
190 res = get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
191 if res.ok and "text/html" in res.headers["content-type"]:
192 res = get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
194 if res.json()["software"]["name"] in ["akkoma", "rebased"]:
196 elif res.json()["software"]["name"] in ["hometown", "ecko"]:
198 elif res.json()["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
201 return res.json()["software"]["name"]
202 elif res.status_code == 404:
203 res = get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
209 def tidyup(domain: str) -> str:
210 # some retards put their blocks in variable case
211 domain = domain.lower()
212 # other retards put the port
213 domain = re.sub("\:\d+$", "", domain)
214 # bigger retards put the schema in their blocklist, sometimes even without slashes
215 domain = re.sub("^https?\:(\/*)", "", domain)
217 domain = re.sub("\/$", "", domain)
219 domain = re.sub("^\@", "", domain)
220 # the biggest retards of them all try to block individual users
221 domain = re.sub("(.+)\@", "", domain)
224 conn = sqlite3.connect("blocks.db")
228 "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
231 for blocker, software in c.fetchall():
233 blocker = tidyup(blocker)
234 if software == "pleroma":
239 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
240 ).json()["metadata"]["federation"]
241 if "mrf_simple" in federation:
242 for block_level, blocks in (
243 {**federation["mrf_simple"],
244 **{"quarantined_instances": federation["quarantined_instances"]}}
246 for blocked in blocks:
247 blocked = tidyup(blocked)
250 if blocked.count("*") > 1:
251 # -ACK!-oma also started obscuring domains without hash
253 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
255 searchres = c.fetchone()
256 if searchres != None:
257 blocked = searchres[0]
260 "select domain from instances where domain = ?", (blocked,)
262 if c.fetchone() == None:
264 "insert into instances select ?, ?, ?",
265 (blocked, get_hash(blocked), get_type(blocked)),
267 timestamp = int(time())
269 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
270 (blocker, blocked, block_level),
272 if c.fetchone() == None:
274 "insert into blocks select ?, ?, '', ?, ?, ?",
275 (blocker, blocked, block_level, timestamp, timestamp),
277 if block_level == "reject":
285 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
286 (timestamp, blocker, blocked, block_level)
290 if "mrf_simple_info" in federation:
291 for block_level, info in (
292 {**federation["mrf_simple_info"],
293 **(federation["quarantined_instances_info"]
294 if "quarantined_instances_info" in federation
297 for blocked, reason in info.items():
298 blocked = tidyup(blocked)
301 if blocked.count("*") > 1:
302 # same domain guess as above, but for reasons field
304 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
306 searchres = c.fetchone()
307 if searchres != None:
308 blocked = searchres[0]
310 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
311 (reason["reason"], blocker, blocked, block_level),
313 for entry in blockdict:
314 if entry["blocked"] == blocked:
315 entry["reason"] = reason["reason"]
318 except Exception as e:
319 print("error:", e, blocker)
320 elif software == "mastodon":
323 # json endpoint for newer mastodongs
328 "followers_only": [],
332 # handling CSRF, I've saw at least one server requiring it to access the endpoint
333 meta = BeautifulSoup(
334 get(f"https://{blocker}/about", headers=headers, timeout=5).text,
338 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
339 reqheaders = {**headers, **{"x-csrf-token": csrf}}
344 f"https://{blocker}/api/v1/instance/domain_blocks", headers=reqheaders, timeout=5
347 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
348 if block['severity'] == 'suspend':
349 json['reject'].append(entry)
350 elif block['severity'] == 'silence':
351 json['followers_only'].append(entry)
352 elif block['severity'] == 'reject_media':
353 json['media_removal'].append(entry)
354 elif block['severity'] == 'reject_reports':
355 json['report_removal'].append(entry)
357 json = get_mastodon_blocks(blocker)
359 for block_level, blocks in json.items():
360 for instance in blocks:
361 blocked, blocked_hash, reason = instance.values()
362 blocked = tidyup(blocked)
363 if blocked.count("*") <= 1:
365 "select hash from instances where hash = ?", (blocked_hash,)
367 if c.fetchone() == None:
369 "insert into instances select ?, ?, ?",
370 (blocked, get_hash(blocked), get_type(blocked)),
373 # Doing the hash search for instance names as well to tidy up DB
375 "select domain from instances where hash = ?", (blocked_hash,)
377 searchres = c.fetchone()
378 if searchres != None:
379 blocked = searchres[0]
381 timestamp = int(time())
383 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
384 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
386 if c.fetchone() == None:
388 "insert into blocks select ?, ?, ?, ?, ?, ?",
391 blocked if blocked.count("*") <= 1 else blocked_hash,
398 if block_level == "reject":
406 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
407 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
411 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
412 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
415 except Exception as e:
416 print("error:", e, blocker)
417 elif software == "friendica" or software == "misskey":
420 if software == "friendica":
421 json = get_friendica_blocks(blocker)
422 elif software == "misskey":
423 json = get_pisskey_blocks(blocker)
424 for block_level, blocks in json.items():
425 for instance in blocks:
426 blocked, reason = instance.values()
427 blocked = tidyup(blocked)
429 if blocked.count("*") > 0:
430 # Some friendica servers also obscure domains without hash
432 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
434 searchres = c.fetchone()
435 if searchres != None:
436 blocked = searchres[0]
438 if blocked.count("?") > 0:
439 # Some obscure them with question marks, not sure if that's dependent on version or not
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 timestamp = int(time())
458 "select * from blocks where blocker = ? and blocked = ?",
461 if c.fetchone() == None:
463 "insert into blocks select ?, ?, ?, ?, ?, ?",
473 if block_level == "reject":
481 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
482 (timestamp, blocker, blocked, block_level),
486 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
487 (reason, blocker, blocked, block_level),
490 except Exception as e:
491 print("error:", e, blocker)
492 elif software == "gotosocial":
497 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
499 for peer in federation:
500 blocked = peer["domain"].lower()
502 if blocked.count("*") > 0:
503 # GTS does not have hashes for obscured domains, so we have to guess it
505 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
507 searchres = c.fetchone()
508 if searchres != None:
509 blocked = searchres[0]
512 "select domain from instances where domain = ?", (blocked,)
514 if c.fetchone() == None:
516 "insert into instances select ?, ?, ?",
517 (blocked, get_hash(blocked), get_type(blocked)),
520 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
521 (blocker, blocked, "reject"),
523 timestamp = int(time())
524 if c.fetchone() == None:
526 "insert into blocks select ?, ?, ?, ?, ?, ?",
527 (blocker, blocked, "", "reject", timestamp, timestamp),
536 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
537 (timestamp, blocker, blocked, "reject"),
539 if "public_comment" in peer:
540 reason = peer["public_comment"]
542 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
543 (reason, blocker, blocked, "reject"),
545 for entry in blockdict:
546 if entry["blocked"] == blocked:
547 entry["reason"] = reason
549 except Exception as e:
550 print("error:", e, blocker)
552 if config["bot_enabled"] and len(blockdict) > 0:
553 send_bot_post(blocker, blockdict)