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 fetch_nodeinfo(domain: str) -> list:
59 print("DEBUG: Fetching nodeinfo from domain:", domain)
62 print("DEBUG: Fetching 2.1.json from domain:", domain)
63 res = reqto.get(f"https://{domain}/nodeinfo/2.1.json", headers=headers, timeout=5)
65 if res.status_code == 404 or "text/html" in res.headers["content-type"]:
66 print("DEBUG: Fetching 2.0 from domain:", domain)
67 res = reqto.get(f"https://{domain}/nodeinfo/2.0", headers=headers, timeout=5)
69 if res.status_code == 404 or "text/html" in res.headers["content-type"]:
70 print("DEBUG: Fetching 2.0.json from domain:", domain)
71 res = reqto.get(f"https://{domain}/nodeinfo/2.0.json", headers=headers, timeout=5)
73 if res.ok and "text/html" in res.headers["content-type"]:
74 print("DEBUG: Fetching 2.1 from domain:", domain)
75 res = reqto.get(f"https://{domain}/nodeinfo/2.1", headers=headers, timeout=5)
77 if res.status_code == 404 or "text/html" in res.headers["content-type"]:
78 print("DEBUG: Fetching /api/v1/instance from domain:", domain)
79 res = reqto.get(f"https://{domain}/api/v1/instance", headers=headers, timeout=5)
81 print("DEBUG: domain,res.ok,res.status_code:", domain, res.ok, res.status_code)
86 print("WARNING: Failed fetching nodeinfo from domain:", domain)
88 print("DEBUG: Returning json():", len(json))
91 def determine_software(domain: str) -> str:
92 # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain)
94 json = fetch_nodeinfo(domain)
95 # NOISY-DEBUG: print("DEBUG: json():", len(json))
97 if json["software"]["name"] in ["akkoma", "rebased"]:
99 elif json["software"]["name"] in ["hometown", "ecko"]:
100 software = "mastodon"
101 elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
104 software = json["software"]["name"]
106 # NOISY-DEBUG: print("DEBUG: Returning domain,software:", domain, software)
109 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
110 # NOISY: print("--- Updating block reason:", reason, blocker, blocked, block_level)
113 "UPDATE blocks SET reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
123 print("ERROR: failed SQL query")
126 def update_last_seen(last_seen: int, blocker: str, blocked: str, block_level: str):
127 # NOISY: print("--- Updating last_seen:", last_seen, blocker, blocked, block_level)
130 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
140 print("ERROR: failed SQL query")
143 def block_instance(blocker: str, blocked: str, reason: str, block_level: str, first_added: int, last_seen: int):
144 if blocker.find("@") > 0:
145 print("WARNING: Bad blocker:", blocker)
147 elif blocked.find("@") > 0:
148 print("WARNING: Bad blocked:", blocked)
151 print("--- New block:", blocker, blocked, reason, block_level, first_added, last_seen)
154 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
166 print("ERROR: failed SQL query")
169 def add_instance(domain: str):
170 if domain.find("@") > 0:
171 print("WARNING: Bad domain name:", domain)
174 print("--- Adding new instance:", domain)
177 "INSERT INTO instances SELECT ?, ?, ?",
181 determine_software(domain)
186 print("ERROR: failed SQL query")
189 def send_bot_post(instance: str, blocks: dict):
190 message = instance + " has blocked the following instances:\n\n"
195 blocks = blocks[0 : 19]
198 if block["reason"] == None or block["reason"] == '':
199 message = message + block["blocked"] + " with unspecified reason\n"
201 if len(block["reason"]) > 420:
202 block["reason"] = block["reason"][0:419] + "[…]"
204 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
207 message = message + "(the list has been truncated to the first 20 entries)"
209 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
211 req = reqto.post(f"{config['bot_instance']}/api/v1/statuses",
212 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
213 headers=botheaders, timeout=10).json()
217 def get_mastodon_blocks(domain: str) -> dict:
218 # NOISY-DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
220 "Suspended servers": [],
221 "Filtered media": [],
222 "Limited servers": [],
223 "Silenced servers": [],
227 "Silenced instances": "Silenced servers",
228 "Suspended instances": "Suspended servers",
229 "Gesperrte Server": "Suspended servers",
230 "Gefilterte Medien": "Filtered media",
231 "Stummgeschaltete Server": "Silenced servers",
232 "停止済みのサーバー": "Suspended servers",
233 "メディアを拒否しているサーバー": "Filtered media",
234 "サイレンス済みのサーバー": "Silenced servers",
235 "שרתים מושעים": "Suspended servers",
236 "מדיה מסוננת": "Filtered media",
237 "שרתים מוגבלים": "Silenced servers",
238 "Serveurs suspendus": "Suspended servers",
239 "Médias filtrés": "Filtered media",
240 "Serveurs limités": "Silenced servers",
245 reqto.get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
249 print("ERROR: Cannot fetch from domain:", domain)
252 for header in doc.find_all("h3"):
253 header_text = header.text
255 if header_text in translations:
256 header_text = translations[header_text]
258 if header_text in blocks:
259 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
260 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
261 blocks[header_text].append(
263 "domain": line.find("span").text,
264 "hash": line.find("span")["title"][9:],
265 "reason": line.find_all("td")[1].text.strip(),
269 # NOISY-DEBUG: print("DEBUG: Returning blocks for domain:", domain)
271 "reject": blocks["Suspended servers"],
272 "media_removal": blocks["Filtered media"],
273 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
276 def get_friendica_blocks(domain: str) -> dict:
277 # NOISY-DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
282 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
286 print("WARNING: Failed to fetch /friendica from domain:", domain)
289 blocklist = doc.find(id="about_blocklist")
291 # Prevents exceptions:
292 if blocklist is None:
293 # NOISY-DEBUG: print("DEBUG: Instance has no block list:", domain)
296 for line in blocklist.find("table").find_all("tr")[1:]:
298 "domain": line.find_all("td")[0].text.strip(),
299 "reason": line.find_all("td")[1].text.strip()
302 # NOISY-DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
307 def get_misskey_blocks(domain: str) -> dict:
308 # NOISY-DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
318 # iterating through all "suspended" (follow-only in its terminology)
319 # instances page-by-page, since that troonware doesn't support
320 # sending them all at once
323 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
324 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
331 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
332 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
340 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
342 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
347 if instance["isSuspended"]:
348 blocks["suspended"].append(
350 "domain": instance["host"],
351 # no reason field, nothing
357 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
360 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
361 counter = counter + step
364 print("WARNING: Caught error, exiting loop:", domain)
369 # same shit, different asshole ("blocked" aka full suspend)
372 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
373 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
380 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
381 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
389 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
391 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
395 if instance["isBlocked"]:
396 blocks["blocked"].append({
397 "domain": instance["host"],
402 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
405 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
406 counter = counter + step
412 # NOISY-DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
414 "reject": blocks["blocked"],
415 "followers_only": blocks["suspended"]
419 print("WARNING: API request failed for domain:", domain)
422 def tidyup(domain: str) -> str:
423 # some retards put their blocks in variable case
424 domain = domain.lower()
426 # other retards put the port
427 domain = re.sub("\:\d+$", "", domain)
429 # bigger retards put the schema in their blocklist, sometimes even without slashes
430 domain = re.sub("^https?\:(\/*)", "", domain)
433 domain = re.sub("\/$", "", domain)
436 domain = re.sub("^\@", "", domain)
438 # the biggest retards of them all try to block individual users
439 domain = re.sub("(.+)\@", "", domain)