1 from bs4 import BeautifulSoup
2 from hashlib import sha256
10 with open("config.json") as f:
11 config = json.loads(f.read())
14 "activitypub-troll.cf",
17 "social.shrimpcam.pw",
18 "mastotroll.netz.org",
23 "user-agent": config["useragent"]
26 conn = sqlite3.connect("blocks.db")
29 def get_hash(domain: str) -> str:
30 # NOISY-DEBUG: print("DEBUG: Calculating hash for domain:", domain)
31 return sha256(domain.encode("utf-8")).hexdigest()
33 def get_peers(domain: str) -> str:
34 # NOISY-DEBUG: print("DEBUG: Getting peers for domain:", domain)
38 res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
41 print("WARNING: Cannot fetch peers:", domain)
43 # NOISY-DEBUG: print("DEBUG: Returning peers[]:", type(peers))
46 def post_json_api(domain: str, path: str, data: str) -> list:
47 # NOISY-DEBUG: print("DEBUG: Sending POST to domain,path,data:", domain, path, data)
48 res = reqto.post(f"https://{domain}{path}", data=data, headers=headers, timeout=5)
51 print("WARNING: Cannot query JSON API:", domain, path, data, res.status_code)
55 # NOISY-DEBUG: print("DEBUG: Returning doc():", len(doc))
58 def determine_software(domain: str) -> str:
59 # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain)
62 res = reqto.get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
64 # NOISY-DEBUG: print("DEBUG: domain,res.ok,res.status_code:", domain, res.ok, res.status_code)
65 if res.status_code == 404:
66 res = reqto.get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
68 if res.status_code == 404:
69 res = reqto.get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
71 if res.ok and "text/html" in res.headers["content-type"]:
72 res = reqto.get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
76 # NOISY-DEBUG: print("DEBUG: json():", len(json))
78 if json["software"]["name"] in ["akkoma", "rebased"]:
80 elif json["software"]["name"] in ["hometown", "ecko"]:
82 elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
85 software = json["software"]["name"]
86 elif res.status_code == 404:
87 res = reqto.get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
91 print("WARNING: Failed fetching instance meta data")
93 # NOISY-DEBUG: print("DEBUG: Returning domain,software:", domain, software)
96 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
97 # NOISY: print("--- Updating block reason:", reason, blocker, blocked, block_level)
100 "UPDATE blocks SET reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
110 print("ERROR: failed SQL query")
113 def update_last_seen(last_seen: int, blocker: str, blocked: str, block_level: str):
114 # NOISY: print("--- Updating last_seen:", last_seen, blocker, blocked, block_level)
117 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
127 print("ERROR: failed SQL query")
130 def block_instance(blocker: str, blocked: str, reason: str, block_level: str, first_added: int, last_seen: int):
131 print("--- New block:", blocker, blocked, reason, block_level, first_added, last_seen)
134 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
146 print("ERROR: failed SQL query")
149 def add_instance(domain: str):
150 print("--- Adding new instance:", domain)
153 "INSERT INTO instances SELECT ?, ?, ?",
157 determine_software(domain)
162 print("ERROR: failed SQL query")
165 def send_bot_post(instance: str, blocks: dict):
166 message = instance + " has blocked the following instances:\n\n"
171 blocks = blocks[0 : 19]
174 if block["reason"] == None or block["reason"] == '':
175 message = message + block["blocked"] + " with unspecified reason\n"
177 if len(block["reason"]) > 420:
178 block["reason"] = block["reason"][0:419] + "[…]"
180 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
183 message = message + "(the list has been truncated to the first 20 entries)"
185 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
187 req = reqto.post(f"{config['bot_instance']}/api/v1/statuses",
188 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
189 headers=botheaders, timeout=10).json()
193 def get_mastodon_blocks(domain: str) -> dict:
194 # NOISY-DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
196 "Suspended servers": [],
197 "Filtered media": [],
198 "Limited servers": [],
199 "Silenced servers": [],
203 "Silenced instances": "Silenced servers",
204 "Suspended instances": "Suspended servers",
205 "Gesperrte Server": "Suspended servers",
206 "Gefilterte Medien": "Filtered media",
207 "Stummgeschaltete Server": "Silenced servers",
208 "停止済みのサーバー": "Suspended servers",
209 "メディアを拒否しているサーバー": "Filtered media",
210 "サイレンス済みのサーバー": "Silenced servers",
211 "שרתים מושעים": "Suspended servers",
212 "מדיה מסוננת": "Filtered media",
213 "שרתים מוגבלים": "Silenced servers",
214 "Serveurs suspendus": "Suspended servers",
215 "Médias filtrés": "Filtered media",
216 "Serveurs limités": "Silenced servers",
221 reqto.get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
225 print("ERROR: Cannot fetch from domain:", domain)
228 for header in doc.find_all("h3"):
229 header_text = header.text
231 if header_text in translations:
232 header_text = translations[header_text]
234 if header_text in blocks:
235 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
236 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
237 blocks[header_text].append(
239 "domain": line.find("span").text,
240 "hash": line.find("span")["title"][9:],
241 "reason": line.find_all("td")[1].text.strip(),
245 # NOISY-DEBUG: print("DEBUG: Returning blocks for domain:", domain)
247 "reject": blocks["Suspended servers"],
248 "media_removal": blocks["Filtered media"],
249 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
252 def get_friendica_blocks(domain: str) -> dict:
253 # NOISY-DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
258 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
262 print("WARNING: Failed to fetch /friendica from domain:", domain)
265 blocklist = doc.find(id="about_blocklist")
267 # Prevents exceptions:
268 if blocklist is None:
269 # NOISY-DEBUG: print("DEBUG: Instance has no block list:", domain)
272 for line in blocklist.find("table").find_all("tr")[1:]:
274 "domain": line.find_all("td")[0].text.strip(),
275 "reason": line.find_all("td")[1].text.strip()
278 # NOISY-DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
283 def get_misskey_blocks(domain: str) -> dict:
284 # NOISY-DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
294 # iterating through all "suspended" (follow-only in its terminology)
295 # instances page-by-page, since that troonware doesn't support
296 # sending them all at once
299 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
300 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
307 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
308 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
316 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
318 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
323 if instance["isSuspended"]:
324 blocks["suspended"].append(
326 "domain": instance["host"],
327 # no reason field, nothing
333 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
336 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
337 counter = counter + step
340 print("WARNING: Caught error, exiting loop:", domain)
345 # same shit, different asshole ("blocked" aka full suspend)
348 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
349 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
356 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
357 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
365 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
367 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
371 if instance["isBlocked"]:
372 blocks["blocked"].append({
373 "domain": instance["host"],
378 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
381 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
382 counter = counter + step
388 # NOISY-DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
390 "reject": blocks["blocked"],
391 "followers_only": blocks["suspended"]
395 print("WARNING: API request failed for domain:", domain)
398 def tidyup(domain: str) -> str:
399 # some retards put their blocks in variable case
400 domain = domain.lower()
402 # other retards put the port
403 domain = re.sub("\:\d+$", "", domain)
405 # bigger retards put the schema in their blocklist, sometimes even without slashes
406 domain = re.sub("^https?\:(\/*)", "", domain)
409 domain = re.sub("\/$", "", domain)
412 domain = re.sub("^\@", "", domain)
414 # the biggest retards of them all try to block individual users
415 domain = re.sub("(.+)\@", "", domain)