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')"
200 "select domain, software from instances where domain = 'glaceon.social'"
203 for blocker, software in c.fetchall():
204 blocker = tidyup(blocker)
205 if software == "pleroma":
210 f"https://{blocker}/nodeinfo/2.1.json", headers=headers, timeout=5
211 ).json()["metadata"]["federation"]
212 if "mrf_simple" in federation:
213 for block_level, blocks in (
214 {**federation["mrf_simple"],
215 **{"quarantined_instances": federation["quarantined_instances"]}}
217 for blocked in blocks:
218 blocked = tidyup(blocked)
221 if blocked.count("*") > 1:
222 # -ACK!-oma also started obscuring domains without hash
224 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
226 searchres = c.fetchone()
227 if searchres != None:
228 blocked = searchres[0]
231 "select domain from instances where domain = ?", (blocked,)
233 if c.fetchone() == None:
235 "insert into instances select ?, ?, ?",
236 (blocked, get_hash(blocked), get_type(blocked)),
238 timestamp = int(time())
240 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
241 (blocker, blocked, block_level),
243 if c.fetchone() == None:
245 "insert into blocks select ?, ?, '', ?, ?, ?",
246 (blocker, blocked, block_level, timestamp, timestamp),
250 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
251 (timestamp, blocker, blocked, block_level)
255 if "mrf_simple_info" in federation:
256 for block_level, info in (
257 {**federation["mrf_simple_info"],
258 **(federation["quarantined_instances_info"]
259 if "quarantined_instances_info" in federation
262 for blocked, reason in info.items():
263 blocked = tidyup(blocked)
266 if blocked.count("*") > 1:
267 # same domain guess as above, but for reasons field
269 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
271 searchres = c.fetchone()
272 if searchres != None:
273 blocked = searchres[0]
275 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
276 (reason["reason"], blocker, blocked, block_level),
279 except Exception as e:
280 print("error:", e, blocker)
281 elif software == "mastodon":
284 # json endpoint for newer mastodongs
289 "followers_only": [],
293 f"https://{blocker}/api/v1/instance/domain_blocks", headers=headers, timeout=5
296 entry = {'domain': block['domain'], 'hash': block['digest'], 'reason': block['comment']}
297 if block['severity'] == 'suspend':
298 json['reject'].append(entry)
299 elif block['severity'] == 'silence':
300 json['followers_only'].append(entry)
301 elif block['severity'] == 'reject_media':
302 json['media_removal'].append(entry)
303 elif block['severity'] == 'reject_reports':
304 json['report_removal'].append(entry)
306 json = get_mastodon_blocks(blocker)
308 for block_level, blocks in json.items():
309 for instance in blocks:
310 blocked, blocked_hash, reason = instance.values()
311 blocked = tidyup(blocked)
312 if blocked.count("*") <= 1:
314 "select hash from instances where hash = ?", (blocked_hash,)
316 if c.fetchone() == None:
318 "insert into instances select ?, ?, ?",
319 (blocked, get_hash(blocked), get_type(blocked)),
322 # Doing the hash search for instance names as well to tidy up DB
324 "select domain from instances where hash = ?", (blocked_hash,)
326 searchres = c.fetchone()
327 if searchres != None:
328 blocked = searchres[0]
330 timestamp = int(time())
332 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
333 (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
335 if c.fetchone() == None:
337 "insert into blocks select ?, ?, ?, ?, ?, ?",
340 blocked if blocked.count("*") <= 1 else blocked_hash,
349 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
350 (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
354 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
355 (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
358 except Exception as e:
359 print("error:", e, blocker)
360 elif software == "friendica" or software == "misskey":
363 if software == "friendica":
364 json = get_friendica_blocks(blocker)
365 elif software == "misskey":
366 json = get_pisskey_blocks(blocker)
367 for block_level, blocks in json.items():
368 for instance in blocks:
369 blocked, reason = instance.values()
370 blocked = tidyup(blocked)
372 if blocked.count("*") > 0:
373 # Some friendica servers also obscure domains without hash
375 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
377 searchres = c.fetchone()
378 if searchres != None:
379 blocked = searchres[0]
381 if blocked.count("?") > 0:
382 # Some obscure them with question marks, not sure if that's dependent on version or not
384 "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("?", "_"),)
386 searchres = c.fetchone()
387 if searchres != None:
388 blocked = searchres[0]
391 "select domain from instances where domain = ?", (blocked,)
393 if c.fetchone() == None:
395 "insert into instances select ?, ?, ?",
396 (blocked, get_hash(blocked), get_type(blocked)),
399 timestamp = int(time())
401 "select * from blocks where blocker = ? and blocked = ? and reason = ?",
402 (blocker, blocked, reason),
404 if c.fetchone() == None:
406 "insert into blocks select ?, ?, ?, ?, ?, ?",
418 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
419 (timestamp, blocker, blocked, block_level),
423 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
424 (reason, blocker, blocked, block_level),
427 except Exception as e:
428 print("error:", e, blocker)
429 elif software == "gotosocial":
434 f"https://{blocker}/api/v1/instance/peers?filter=suspended", headers=headers, timeout=5
436 for peer in federation:
437 blocked = peer["domain"].lower()
439 if blocked.count("*") > 0:
440 # GTS does not have hashes for obscured domains, so we have to guess it
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 "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
458 (blocker, blocked, "reject"),
460 timestamp = int(time())
461 if c.fetchone() == None:
463 "insert into blocks select ?, ?, ?, ?, ?, ?",
464 (blocker, blocked, "", "reject", timestamp, timestamp),
468 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
469 (timestamp, blocker, blocked, "reject"),
471 if "public_comment" in peer:
472 reason = peer["public_comment"]
474 "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
475 (reason, blocker, blocked, "reject"),
478 except Exception as e:
479 print("error:", e, blocker)