1 from bs4 import BeautifulSoup
2 from hashlib import sha256
11 with open("config.json") as f:
12 config = json.loads(f.read())
15 "activitypub-troll.cf",
18 "social.shrimpcam.pw",
19 "mastotroll.netz.org",
24 "user-agent": config["useragent"]
27 conn = sqlite3.connect("blocks.db")
30 def get_hash(domain: str) -> str:
31 # NOISY-DEBUG: print("DEBUG: Calculating hash for domain:", domain)
32 return sha256(domain.encode("utf-8")).hexdigest()
34 def get_peers(domain: str) -> str:
35 # NOISY-DEBUG: print("DEBUG: Getting peers for domain:", domain)
39 res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
42 print("WARNING: Cannot fetch peers:", domain)
44 # NOISY-DEBUG: print("DEBUG: Returning peers[]:", type(peers))
47 def post_json_api(domain: str, path: str, data: str) -> list:
48 # NOISY-DEBUG: print("DEBUG: Sending POST to domain,path,data:", domain, path, data)
49 res = reqto.post(f"https://{domain}{path}", data=data, headers=headers, timeout=5)
52 print("WARNING: Cannot query JSON API:", domain, path, data, res.status_code)
56 # NOISY-DEBUG: print("DEBUG: Returning doc():", len(doc))
59 def fetch_nodeinfo(domain: str) -> list:
60 # NOISY-DEBUG: print("DEBUG: Fetching nodeinfo from domain:", domain)
63 f"https://{domain}/nodeinfo/2.1.json",
64 f"https://{domain}/nodeinfo/2.0",
65 f"https://{domain}/nodeinfo/2.0.json",
66 f"https://{domain}/nodeinfo/2.1",
67 f"https://{domain}/api/v1/instance"
71 for request in requests:
72 # NOISY-DEBUG: print("DEBUG: Fetching request:", request)
75 res = reqto.get(request, headers=headers, timeout=5)
77 # NOISY-DEBUG: print("DEBUG: res.ok,res.json[]:", res.ok, type(res.json()))
78 if res.ok and res.json() is not None:
79 # NOISY-DEBUG: print("DEBUG: Success:", request)
83 # NOISY-DEBUG: print("DEBUG: Failed fetching nodeinfo from domain:", domain)
90 print("WARNING: Failed fetching nodeinfo from domain:", domain)
92 # NOISY-DEBUG: print("DEBUG: Returning json():", len(json))
95 def determine_software(domain: str) -> str:
96 # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain)
98 json = fetch_nodeinfo(domain)
99 # NOISY-DEBUG: print("DEBUG: json():", len(json))
101 if json["software"]["name"] in ["akkoma", "rebased"]:
102 # NOISY-DEBUG: print("DEBUG: Setting pleroma:", domain, json["software"]["name"])
104 elif json["software"]["name"] in ["hometown", "ecko"]:
105 # NOISY-DEBUG: print("DEBUG: Setting mastodon:", domain, json["software"]["name"])
106 software = "mastodon"
107 elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
108 # NOISY-DEBUG: print("DEBUG: Setting misskey:", domain, json["software"]["name"])
111 # NOISY-DEBUG: print("DEBUG: Using name:", domain, json["software"]["name"])
112 software = json["software"]["name"]
114 # NOISY-DEBUG: print("DEBUG: Returning domain,software:", domain, software)
117 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
118 # NOISY: print("--- Updating block reason:", reason, blocker, blocked, block_level)
121 "UPDATE blocks SET reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
131 print("ERROR: failed SQL query:", reason, blocker, blocked, block_level)
134 def update_last_seen(last_seen: int, blocker: str, blocked: str, block_level: str):
135 # NOISY: print("--- Updating last_seen:", last_seen, blocker, blocked, block_level)
138 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
148 print("ERROR: failed SQL query:", last_seen, blocker, blocked, block_level)
151 def block_instance(blocker: str, blocked: str, reason: str, block_level: str, first_added: int, last_seen: int):
152 # NOISY-DEBUG: print("DEBUG: blocker,blocked,reason,block_level,first_added,last_seen:", blocker, blocked, reason, block_level, first_added, last_seen)
153 if blocker.find("@") > 0:
154 print("WARNING: Bad blocker:", blocker)
156 elif blocked.find("@") > 0:
157 print("WARNING: Bad blocked:", blocked)
160 print("--- New block:", blocker, blocked, reason, block_level, first_added, last_seen)
163 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
175 print("ERROR: failed SQL query:", blocker, blocked, reason, block_level, first_added, last_seen)
178 def add_instance(domain: str):
179 # NOISY-DEBUG: print("DEBUG: domain:", domain)
180 if domain.find("@") > 0:
181 print("WARNING: Bad domain name:", domain)
184 print("--- Adding new instance:", domain)
187 "INSERT INTO instances SELECT ?, ?, ?, ?",
191 determine_software(domain),
197 print("ERROR: failed SQL query:", domain)
200 def send_bot_post(instance: str, blocks: dict):
201 message = instance + " has blocked the following instances:\n\n"
206 blocks = blocks[0 : 19]
209 if block["reason"] == None or block["reason"] == '':
210 message = message + block["blocked"] + " with unspecified reason\n"
212 if len(block["reason"]) > 420:
213 block["reason"] = block["reason"][0:419] + "[…]"
215 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
218 message = message + "(the list has been truncated to the first 20 entries)"
220 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
222 req = reqto.post(f"{config['bot_instance']}/api/v1/statuses",
223 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
224 headers=botheaders, timeout=10).json()
228 def get_mastodon_blocks(domain: str) -> dict:
229 # NOISY-DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
231 "Suspended servers": [],
232 "Filtered media": [],
233 "Limited servers": [],
234 "Silenced servers": [],
238 "Silenced instances": "Silenced servers",
239 "Suspended instances": "Suspended servers",
240 "Gesperrte Server": "Suspended servers",
241 "Gefilterte Medien": "Filtered media",
242 "Stummgeschaltete Server": "Silenced servers",
243 "停止済みのサーバー": "Suspended servers",
244 "メディアを拒否しているサーバー": "Filtered media",
245 "サイレンス済みのサーバー": "Silenced servers",
246 "שרתים מושעים": "Suspended servers",
247 "מדיה מסוננת": "Filtered media",
248 "שרתים מוגבלים": "Silenced servers",
249 "Serveurs suspendus": "Suspended servers",
250 "Médias filtrés": "Filtered media",
251 "Serveurs limités": "Silenced servers",
256 reqto.get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
260 print("ERROR: Cannot fetch from domain:", domain)
263 for header in doc.find_all("h3"):
264 header_text = header.text
266 if header_text in translations:
267 header_text = translations[header_text]
269 if header_text in blocks:
270 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
271 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
272 blocks[header_text].append(
274 "domain": line.find("span").text,
275 "hash": line.find("span")["title"][9:],
276 "reason": line.find_all("td")[1].text.strip(),
280 # NOISY-DEBUG: print("DEBUG: Returning blocks for domain:", domain)
282 "reject": blocks["Suspended servers"],
283 "media_removal": blocks["Filtered media"],
284 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
287 def get_friendica_blocks(domain: str) -> dict:
288 # NOISY-DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
293 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
297 print("WARNING: Failed to fetch /friendica from domain:", domain)
300 blocklist = doc.find(id="about_blocklist")
302 # Prevents exceptions:
303 if blocklist is None:
304 # NOISY-DEBUG: print("DEBUG: Instance has no block list:", domain)
307 for line in blocklist.find("table").find_all("tr")[1:]:
309 "domain": line.find_all("td")[0].text.strip(),
310 "reason": line.find_all("td")[1].text.strip()
313 # NOISY-DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
318 def get_misskey_blocks(domain: str) -> dict:
319 # NOISY-DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
329 # iterating through all "suspended" (follow-only in its terminology)
330 # instances page-by-page, since that troonware doesn't support
331 # sending them all at once
334 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
335 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
342 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
343 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
351 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
353 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
358 if instance["isSuspended"]:
359 blocks["suspended"].append(
361 "domain": instance["host"],
362 # no reason field, nothing
368 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
371 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
372 counter = counter + step
375 print("WARNING: Caught error, exiting loop:", domain)
380 # same shit, different asshole ("blocked" aka full suspend)
383 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
384 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
391 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
392 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
400 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
402 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
406 if instance["isBlocked"]:
407 blocks["blocked"].append({
408 "domain": instance["host"],
413 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
416 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
417 counter = counter + step
423 # NOISY-DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
425 "reject": blocks["blocked"],
426 "followers_only": blocks["suspended"]
430 print("WARNING: API request failed for domain:", domain)
433 def tidyup(domain: str) -> str:
434 # some retards put their blocks in variable case
435 domain = domain.lower()
437 # other retards put the port
438 domain = re.sub("\:\d+$", "", domain)
440 # bigger retards put the schema in their blocklist, sometimes even without slashes
441 domain = re.sub("^https?\:(\/*)", "", domain)
444 domain = re.sub("\/$", "", domain)
447 domain = re.sub("^\@", "", domain)
449 # the biggest retards of them all try to block individual users
450 domain = re.sub("(.+)\@", "", domain)