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 update_last_nodeinfo(domain: str):
35 # NOISY-DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
38 c.execute("UPDATE instances SET last_nodeinfo = ? WHERE domain = ?", [
44 print("ERROR: failed SQL query:", domain)
47 def get_peers(domain: str) -> str:
48 # NOISY-DEBUG: print("DEBUG: Getting peers for domain:", domain)
52 res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
55 print("WARNING: Cannot fetch peers:", domain)
58 update_last_nodeinfo(domain)
60 # NOISY-DEBUG: print("DEBUG: Returning peers[]:", type(peers))
63 def post_json_api(domain: str, path: str, data: str) -> list:
64 # NOISY-DEBUG: print("DEBUG: Sending POST to domain,path,data:", domain, path, data)
65 res = reqto.post(f"https://{domain}{path}", data=data, headers=headers, timeout=5)
68 print("WARNING: Cannot query JSON API:", domain, path, data, res.status_code)
71 update_last_nodeinfo(domain)
74 # NOISY-DEBUG: print("DEBUG: Returning doc():", len(doc))
77 def fetch_nodeinfo(domain: str) -> list:
78 # NOISY-DEBUG: print("DEBUG: Fetching nodeinfo from domain:", domain)
81 f"https://{domain}/nodeinfo/2.1.json",
82 f"https://{domain}/nodeinfo/2.0",
83 f"https://{domain}/nodeinfo/2.0.json",
84 f"https://{domain}/nodeinfo/2.1",
85 f"https://{domain}/api/v1/instance"
89 for request in requests:
90 # NOISY-DEBUG: print("DEBUG: Fetching request:", request)
93 res = reqto.get(request, headers=headers, timeout=5)
95 # NOISY-DEBUG: print("DEBUG: res.ok,res.json[]:", res.ok, type(res.json()))
96 if res.ok and res.json() is not None:
97 # NOISY-DEBUG: print("DEBUG: Success:", request)
101 # NOISY-DEBUG: print("DEBUG: Failed fetching nodeinfo from domain:", domain)
108 print("WARNING: Failed fetching nodeinfo from domain:", domain)
110 update_last_nodeinfo(domain)
112 # NOISY-DEBUG: print("DEBUG: Returning json():", len(json))
115 def determine_software(domain: str) -> str:
116 # NOISY-DEBUG: print("DEBUG: Determining software for domain:", domain)
118 json = fetch_nodeinfo(domain)
119 # NOISY-DEBUG: print("DEBUG: json():", len(json))
121 if json["software"]["name"] in ["akkoma", "rebased"]:
122 # NOISY-DEBUG: print("DEBUG: Setting pleroma:", domain, json["software"]["name"])
124 elif json["software"]["name"] in ["hometown", "ecko"]:
125 # NOISY-DEBUG: print("DEBUG: Setting mastodon:", domain, json["software"]["name"])
126 software = "mastodon"
127 elif json["software"]["name"] in ["calckey", "groundpolis", "foundkey", "cherrypick"]:
128 # NOISY-DEBUG: print("DEBUG: Setting misskey:", domain, json["software"]["name"])
131 # NOISY-DEBUG: print("DEBUG: Using name:", domain, json["software"]["name"])
132 software = json["software"]["name"]
134 # NOISY-DEBUG: print("DEBUG: Returning domain,software:", domain, software)
137 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
138 # NOISY: print("--- Updating block reason:", reason, blocker, blocked, block_level)
141 "UPDATE blocks SET reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
151 print("ERROR: failed SQL query:", reason, blocker, blocked, block_level)
154 def update_last_seen(last_seen: int, blocker: str, blocked: str, block_level: str):
155 # NOISY: print("--- Updating last_seen:", last_seen, blocker, blocked, block_level)
158 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
168 print("ERROR: failed SQL query:", last_seen, blocker, blocked, block_level)
171 def block_instance(blocker: str, blocked: str, reason: str, block_level: str, first_added: int, last_seen: int):
172 # NOISY-DEBUG: print("DEBUG: blocker,blocked,reason,block_level,first_added,last_seen:", blocker, blocked, reason, block_level, first_added, last_seen)
173 if blocker.find("@") > 0:
174 print("WARNING: Bad blocker:", blocker)
176 elif blocked.find("@") > 0:
177 print("WARNING: Bad blocked:", blocked)
180 print("--- New block:", blocker, blocked, reason, block_level, first_added, last_seen)
183 "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
195 print("ERROR: failed SQL query:", blocker, blocked, reason, block_level, first_added, last_seen)
198 def add_instance(domain: str):
199 # NOISY-DEBUG: print("DEBUG: domain:", domain)
200 if domain.find("@") > 0:
201 print("WARNING: Bad domain name:", domain)
204 print("--- Adding new instance:", domain)
207 "INSERT INTO instances SELECT ?, ?, ?, NULL",
211 determine_software(domain)
216 print("ERROR: failed SQL query:", domain)
219 def send_bot_post(instance: str, blocks: dict):
220 message = instance + " has blocked the following instances:\n\n"
225 blocks = blocks[0 : 19]
228 if block["reason"] == None or block["reason"] == '':
229 message = message + block["blocked"] + " with unspecified reason\n"
231 if len(block["reason"]) > 420:
232 block["reason"] = block["reason"][0:419] + "[…]"
234 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
237 message = message + "(the list has been truncated to the first 20 entries)"
239 botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
241 req = reqto.post(f"{config['bot_instance']}/api/v1/statuses",
242 data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
243 headers=botheaders, timeout=10).json()
247 def get_mastodon_blocks(domain: str) -> dict:
248 # NOISY-DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
250 "Suspended servers": [],
251 "Filtered media": [],
252 "Limited servers": [],
253 "Silenced servers": [],
257 "Silenced instances": "Silenced servers",
258 "Suspended instances": "Suspended servers",
259 "Gesperrte Server": "Suspended servers",
260 "Gefilterte Medien": "Filtered media",
261 "Stummgeschaltete Server": "Silenced servers",
262 "停止済みのサーバー": "Suspended servers",
263 "メディアを拒否しているサーバー": "Filtered media",
264 "サイレンス済みのサーバー": "Silenced servers",
265 "שרתים מושעים": "Suspended servers",
266 "מדיה מסוננת": "Filtered media",
267 "שרתים מוגבלים": "Silenced servers",
268 "Serveurs suspendus": "Suspended servers",
269 "Médias filtrés": "Filtered media",
270 "Serveurs limités": "Silenced servers",
275 reqto.get(f"https://{domain}/about/more", headers=headers, timeout=5).text,
279 print("ERROR: Cannot fetch from domain:", domain)
282 for header in doc.find_all("h3"):
283 header_text = header.text
285 if header_text in translations:
286 header_text = translations[header_text]
288 if header_text in blocks:
289 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
290 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
291 blocks[header_text].append(
293 "domain": line.find("span").text,
294 "hash": line.find("span")["title"][9:],
295 "reason": line.find_all("td")[1].text.strip(),
299 # NOISY-DEBUG: print("DEBUG: Returning blocks for domain:", domain)
301 "reject": blocks["Suspended servers"],
302 "media_removal": blocks["Filtered media"],
303 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
306 def get_friendica_blocks(domain: str) -> dict:
307 # NOISY-DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
312 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=5).text,
316 print("WARNING: Failed to fetch /friendica from domain:", domain)
319 blocklist = doc.find(id="about_blocklist")
321 # Prevents exceptions:
322 if blocklist is None:
323 # NOISY-DEBUG: print("DEBUG: Instance has no block list:", domain)
326 for line in blocklist.find("table").find_all("tr")[1:]:
328 "domain": line.find_all("td")[0].text.strip(),
329 "reason": line.find_all("td")[1].text.strip()
332 # NOISY-DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
337 def get_misskey_blocks(domain: str) -> dict:
338 # NOISY-DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
348 # iterating through all "suspended" (follow-only in its terminology)
349 # instances page-by-page, since that troonware doesn't support
350 # sending them all at once
353 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
354 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
361 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
362 doc = post_json_api(domain, "/api/federation/instances/", json.dumps({
370 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
372 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
377 if instance["isSuspended"]:
378 blocks["suspended"].append(
380 "domain": instance["host"],
381 # no reason field, nothing
387 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
390 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
391 counter = counter + step
394 print("WARNING: Caught error, exiting loop:", domain)
399 # same shit, different asshole ("blocked" aka full suspend)
402 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
403 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
410 # NOISY-DEBUG: print("DEBUG: Sending JSON API request to domain,step,counter:", domain, step, counter)
411 doc = post_json_api(domain,"/api/federation/instances", json.dumps({
419 # NOISY-DEBUG: print("DEBUG: doc():", len(doc))
421 # NOISY-DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
425 if instance["isBlocked"]:
426 blocks["blocked"].append({
427 "domain": instance["host"],
432 # NOISY-DEBUG: print("DEBUG: End of request:", len(doc), step)
435 # NOISY-DEBUG: print("DEBUG: Raising counter by step:", step)
436 counter = counter + step
442 # NOISY-DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
444 "reject": blocks["blocked"],
445 "followers_only": blocks["suspended"]
449 print("WARNING: API request failed for domain:", domain)
452 def tidyup(domain: str) -> str:
453 # some retards put their blocks in variable case
454 domain = domain.lower()
456 # other retards put the port
457 domain = re.sub("\:\d+$", "", domain)
459 # bigger retards put the schema in their blocklist, sometimes even without slashes
460 domain = re.sub("^https?\:(\/*)", "", domain)
463 domain = re.sub("\/$", "", domain)
466 domain = re.sub("^\@", "", domain)
468 # the biggest retards of them all try to block individual users
469 domain = re.sub("(.+)\@", "", domain)