1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
28 with open("config.json") as f:
29 config = json.loads(f.read())
31 # Don't check these, known trolls/flooders/testing/developing
33 # Floods network with fake nodes as "research" project
34 "activitypub-troll.cf",
40 "social.shrimpcam.pw",
42 "mastotroll.netz.org",
43 # Testing/developing installations
46 "misskeytest.chn.moe",
49 # Array with pending errors needed to be written to database
53 # "rel" identifiers (no real URLs)
54 nodeinfo_identifier = [
55 "https://nodeinfo.diaspora.software/ns/schema/2.1",
56 "https://nodeinfo.diaspora.software/ns/schema/2.0",
57 "https://nodeinfo.diaspora.software/ns/schema/1.1",
58 "https://nodeinfo.diaspora.software/ns/schema/1.0",
59 "http://nodeinfo.diaspora.software/ns/schema/2.1",
60 "http://nodeinfo.diaspora.software/ns/schema/2.0",
61 "http://nodeinfo.diaspora.software/ns/schema/1.1",
62 "http://nodeinfo.diaspora.software/ns/schema/1.0",
65 # HTTP headers for non-API requests
67 "User-Agent": config["useragent"],
69 # HTTP headers for API requests
71 "User-Agent": config["useragent"],
72 "Content-Type": "application/json",
75 # Found info from node, such as nodeinfo URL, detection mode that needs to be
76 # written to database. Both arrays must be filled at the same time or else
77 # update_instance_data() will fail
79 # Detection mode: 'AUTO_DISCOVERY', 'STATIC_CHECKS' or 'GENERATOR'
80 # NULL means all detection methods have failed (maybe still reachable instance)
81 "detection_mode" : {},
86 # Last fetched instances
87 "last_instance_fetch": {},
92 # Last nodeinfo (fetched)
95 "last_status_code" : {},
97 "last_error_details" : {},
102 "Silenced instances" : "Silenced servers",
103 "Suspended instances" : "Suspended servers",
104 "Limited instances" : "Limited servers",
105 # Mappuing German -> English
106 "Gesperrte Server" : "Suspended servers",
107 "Gefilterte Medien" : "Filtered media",
108 "Stummgeschaltete Server" : "Silenced servers",
109 # Japanese -> English
110 "停止済みのサーバー" : "Suspended servers",
111 "制限中のサーバー" : "Limited servers",
112 "メディアを拒否しているサーバー": "Filtered media",
113 "サイレンス済みのサーバー" : "Silenced servers",
115 "שרתים מושעים" : "Suspended servers",
116 "מדיה מסוננת" : "Filtered media",
117 "שרתים מוגבלים" : "Silenced servers",
119 "Serveurs suspendus" : "Suspended servers",
120 "Médias filtrés" : "Filtered media",
121 "Serveurs limités" : "Limited servers",
122 "Serveurs modérés" : "Limited servers",
125 # URL for fetching peers
126 get_peers_url = "/api/v1/instance/peers"
128 # Connect to database
129 connection = sqlite3.connect("blocks.db")
130 cursor = connection.cursor()
132 # Pattern instance for version numbers
134 # semantic version number (with v|V) prefix)
135 re.compile("^(?P<version>v|V{0,1})(\.{0,1})(?P<major>0|[1-9]\d*)\.(?P<minor>0+|[1-9]\d*)(\.(?P<patch>0+|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)?$"),
136 # non-sematic, e.g. 1.2.3.4
137 re.compile("^(?P<version>v|V{0,1})(\.{0,1})(?P<major>0|[1-9]\d*)\.(?P<minor>0+|[1-9]\d*)(\.(?P<patch>0+|[1-9]\d*)(\.(?P<subpatch>0|[1-9]\d*))?)$"),
138 # non-sematic, e.g. 2023-05[-dev]
139 re.compile("^(?P<year>[1-9]{1}[0-9]{3})\.(?P<month>[0-9]{2})(-dev){0,1}$"),
140 # non-semantic, e.g. abcdef0
141 re.compile("^[a-f0-9]{7}$"),
144 ##### Other functions #####
146 def is_primitive(var: any) -> bool:
147 # NOISY-DEBUG: print(f"DEBUG: var[]='{type(var)}' - CALLED!")
148 return type(var) in {int, str, float, bool} or var == None
150 def fetch_instances(domain: str, origin: str, software: str, script: str, path: str = None):
151 if type(domain) != str:
152 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
154 raise ValueError(f"Parameter 'domain' cannot be empty")
155 elif type(origin) != str and origin != None:
156 raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
157 elif type(script) != str:
158 raise ValueError(f"Parameter script[]={type(script)} is not 'str'")
160 raise ValueError(f"Parameter 'domain' cannot be empty")
162 # DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
163 if not is_instance_registered(domain):
164 # DEBUG: print("DEBUG: Adding new domain:", domain, origin)
165 add_instance(domain, origin, script, path)
167 # DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
168 peerlist = get_peers(domain, software)
170 if (peerlist is None):
171 print("ERROR: Cannot fetch peers:", domain)
173 elif has_pending_instance_data(domain):
174 # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
175 update_instance_data(domain)
177 print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
178 for instance in peerlist:
180 # Skip "None" types as tidup() cannot parse them
183 # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - BEFORE")
184 instance = tidyup(instance)
185 # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - AFTER")
188 print("WARNING: Empty instance after tidyup(), domain:", domain)
190 elif not validators.domain(instance.split("/")[0]):
191 print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
193 elif is_blacklisted(instance):
194 # DEBUG: print("DEBUG: instance is blacklisted:", instance)
197 # DEBUG: print("DEBUG: Handling instance:", instance)
199 if not is_instance_registered(instance):
200 # DEBUG: print("DEBUG: Adding new instance:", instance, domain)
201 add_instance(instance, domain, sys.argv[0])
202 except BaseException as e:
203 print(f"ERROR: instance='{instance}',exception[{type(e)}]:'{str(e)}'")
206 # DEBUG: print("DEBUG: EXIT!")
208 def set_instance_data(key: str, domain: str, value: any):
209 # NOISY-DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',value[]='{type(value)}' - CALLED!")
211 raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
213 raise ValueError(f"Parameter 'key' cannot be empty")
214 elif type(domain) != str:
215 raise ValueError("Parameter domain[]='{type(domain)}' is not 'str'")
217 raise ValueError(f"Parameter 'domain' cannot be empty")
218 elif not key in instance_data:
219 raise ValueError(f"key='{key}' not found in instance_data")
220 elif not is_primitive(value):
221 raise ValueError(f"value[]='{type(value)}' is not a primitive type")
224 instance_data[key][domain] = value
226 # DEBUG: print("DEBUG: EXIT!")
228 def add_peers(rows: dict) -> list:
229 # DEBUG: print(f"DEBUG: rows()={len(rows)} - CALLED!")
231 for element in ["linked", "allowed", "blocked"]:
232 # DEBUG: print(f"DEBUG: Checking element='{element}'")
233 if element in rows and rows[element] != None:
234 # DEBUG: print(f"DEBUG: Adding {len(rows[element])} peer(s) to peers list ...")
235 for peer in rows[element]:
236 # DEBUG: print(f"DEBUG: peer='{peer}' - BEFORE!")
239 # DEBUG: print(f"DEBUG: peer='{peer}' - AFTER!")
240 if is_blacklisted(peer):
241 # DEBUG: print(f"DEBUG: peer='{peer}' is blacklisted, skipped!")
244 # DEBUG: print(f"DEBUG: Adding peer='{peer}' ...")
247 # DEBUG: print(f"DEBUG: peers()={len(peers)} - EXIT!")
250 def remove_version(software: str) -> str:
251 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
252 if not "." in software and " " not in software:
253 print(f"WARNING: software='{software}' does not contain a version number.")
258 temp = software.split(";")[0]
259 elif "," in software:
260 temp = software.split(",")[0]
261 elif " - " in software:
262 temp = software.split(" - ")[0]
264 # DEBUG: print(f"DEBUG: software='{software}'")
267 version = temp.split(" ")[-1]
268 elif "/" in software:
269 version = temp.split("/")[-1]
270 elif "-" in software:
271 version = temp.split("-")[-1]
273 # DEBUG: print(f"DEBUG: Was not able to find common seperator, returning untouched software='{software}'")
278 # DEBUG: print(f"DEBUG: Checking {len(patterns)} patterns ...")
279 for pattern in patterns:
281 match = pattern.match(version)
283 # DEBUG: print(f"DEBUG: match[]={type(match)}")
284 if type(match) is re.Match:
287 # DEBUG: print(f"DEBUG: version[{type(version)}]='{version}',match='{match}'")
288 if type(match) is not re.Match:
289 print(f"WARNING: version='{version}' does not match regex, leaving software='{software}' untouched.")
292 # DEBUG: print(f"DEBUG: Found valid version number: '{version}', removing it ...")
293 end = len(temp) - len(version) - 1
295 # DEBUG: print(f"DEBUG: end[{type(end)}]={end}")
296 software = temp[0:end].strip()
297 if " version" in software:
298 # DEBUG: print(f"DEBUG: software='{software}' contains word ' version'")
299 software = strip_until(software, " version")
301 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
304 def strip_powered_by(software: str) -> str:
305 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
307 print(f"ERROR: Bad method call, 'software' is empty")
308 raise Exception("Parameter 'software' is empty")
309 elif not "powered by" in software:
310 print(f"WARNING: Cannot find 'powered by' in '{software}'!")
313 start = software.find("powered by ")
314 # DEBUG: print(f"DEBUG: start[{type(start)}]='{start}'")
316 software = software[start + 11:].strip()
317 # DEBUG: print(f"DEBUG: software='{software}'")
319 software = strip_until(software, " - ")
321 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
324 def strip_hosted_on(software: str) -> str:
325 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
327 print(f"ERROR: Bad method call, 'software' is empty")
328 raise Exception("Parameter 'software' is empty")
329 elif not "hosted on" in software:
330 print(f"WARNING: Cannot find 'hosted on' in '{software}'!")
333 end = software.find("hosted on ")
334 # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'")
336 software = software[0, start].strip()
337 # DEBUG: print(f"DEBUG: software='{software}'")
339 software = strip_until(software, " - ")
341 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
344 def strip_until(software: str, until: str) -> str:
345 # DEBUG: print(f"DEBUG: software='{software}',until='{until}' - CALLED!")
347 print(f"ERROR: Bad method call, 'software' is empty")
348 raise Exception("Parameter 'software' is empty")
350 print(f"ERROR: Bad method call, 'until' is empty")
351 raise Exception("Parameter 'until' is empty")
352 elif not until in software:
353 print(f"WARNING: Cannot find '{until}' in '{software}'!")
356 # Next, strip until part
357 end = software.find(until)
359 # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'")
361 software = software[0:end].strip()
363 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
366 def is_blacklisted(domain: str) -> bool:
367 if type(domain) != str:
368 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
370 raise ValueError(f"Parameter 'domain' cannot be empty")
373 for peer in blacklist:
379 def remove_pending_error(domain: str):
380 if type(domain) != str:
381 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
383 raise ValueError(f"Parameter 'domain' cannot be empty")
386 # Prevent updating any pending errors, nodeinfo was found
387 del pending_errors[domain]
392 # DEBUG: print("DEBUG: EXIT!")
394 def get_hash(domain: str) -> str:
395 if type(domain) != str:
396 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
398 raise ValueError(f"Parameter 'domain' cannot be empty")
400 return hashlib.sha256(domain.encode("utf-8")).hexdigest()
402 def update_last_blocked(domain: str):
403 if type(domain) != str:
404 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
406 raise ValueError(f"Parameter 'domain' cannot be empty")
408 # DEBUG: print("DEBUG: Updating last_blocked for domain", domain)
409 set_instance_data("last_blocked", domain, time.time())
411 # Running pending updated
412 # DEBUG: print(f"DEBUG: Invoking update_instance_data({domain}) ...")
413 update_instance_data(domain)
415 # DEBUG: print("DEBUG: EXIT!")
417 def has_pending_instance_data(domain: str) -> bool:
418 # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
419 if type(domain) != str:
420 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
422 raise ValueError(f"Parameter 'domain' cannot be empty")
425 for key in instance_data:
426 # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',instance_data[key]()='{len(instance_data[key])}'")
427 if domain in instance_data[key]:
431 # DEBUG: print(f"DEBUG: has_pending='{has_pending}' - EXIT!")
434 def update_instance_data(domain: str):
435 if type(domain) != str:
436 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
438 raise ValueError(f"Parameter 'domain' cannot be empty")
440 # DEBUG: print(f"DEBUG: Updating nodeinfo for domain='{domain}' ...")
443 for key in instance_data:
444 # DEBUG: print("DEBUG: key:", key)
445 if domain in instance_data[key]:
446 # DEBUG: print(f"DEBUG: Adding '{instance_data[key][domain]}' for key='{key}' ...")
447 fields.append(instance_data[key][domain])
448 sql_string += f" {key} = ?,"
450 fields.append(domain)
453 raise ValueError(f"No fields have been set, but method invoked, domain='{domain}'")
455 # DEBUG: print(f"DEBUG: sql_string='{sql_string}',fields()={len(fields)}")
456 sql_string = "UPDATE instances SET" + sql_string + " last_updated = TIME() WHERE domain = ? LIMIT 1"
457 # DEBUG: print("DEBUG: sql_string:", sql_string)
460 # DEBUG: print("DEBUG: Executing SQL:", sql_string)
461 cursor.execute(sql_string, fields)
463 # DEBUG: print(f"DEBUG: Success! (rowcount={cursor.rowcount })")
464 if cursor.rowcount == 0:
465 print(f"WARNING: Did not update any rows: domain='{domain}',fields()={len(fields)} - EXIT!")
470 # DEBUG: print("DEBUG: Deleting instance_data for domain:", domain)
471 for key in instance_data:
473 # DEBUG: print("DEBUG: Deleting key:", key)
474 del instance_data[key][domain]
478 except BaseException as e:
479 print(f"ERROR: failed SQL query: domain='{domain}',sql_string='{sql_string}',exception[{type(e)}]:'{str(e)}'")
482 # DEBUG: print("DEBUG: EXIT!")
484 def log_error(domain: str, res: any):
485 # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
486 if type(domain) != str:
487 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
489 raise ValueError(f"Parameter 'domain' cannot be empty")
492 # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
493 if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
496 # DEBUG: print("DEBUG: AFTER res[]:", type(res))
498 cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
504 cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
511 # Cleanup old entries
512 # DEBUG: print(f"DEBUG: Purging old records (distance: {config['error_log_cleanup']})")
513 cursor.execute("DELETE FROM error_log WHERE created < ?", [time.time() - config["error_log_cleanup"]])
514 except BaseException as e:
515 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
518 # DEBUG: print("DEBUG: EXIT!")
520 def update_last_error(domain: str, res: any):
521 # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
522 if type(domain) != str:
523 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
525 raise ValueError(f"Parameter 'domain' cannot be empty")
527 # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
528 if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
531 # DEBUG: print("DEBUG: AFTER res[]:", type(res))
533 # DEBUG: print(f"DEBUG: Setting last_error_details='{res}'");
534 set_instance_data("last_status_code" , domain, 999)
535 set_instance_data("last_error_details", domain, res)
537 # DEBUG: print(f"DEBUG: Setting last_error_details='{res.reason}'");
538 set_instance_data("last_status_code" , domain, res.status_code)
539 set_instance_data("last_error_details", domain, res.reason)
541 # Running pending updated
542 # DEBUG: print(f"DEBUG: Invoking update_instance_data({domain}) ...")
543 update_instance_data(domain)
545 log_error(domain, res)
547 # DEBUG: print("DEBUG: EXIT!")
549 def update_last_instance_fetch(domain: str):
550 if type(domain) != str:
551 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
553 raise ValueError(f"Parameter 'domain' cannot be empty")
555 # DEBUG: print("DEBUG: Updating last_instance_fetch for domain:", domain)
556 set_instance_data("last_instance_fetch", domain, time.time())
558 # Running pending updated
559 # DEBUG: print(f"DEBUG: Invoking update_instance_data({domain}) ...")
560 update_instance_data(domain)
562 # DEBUG: print("DEBUG: EXIT!")
564 def update_last_nodeinfo(domain: str):
565 if type(domain) != str:
566 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
568 raise ValueError(f"Parameter 'domain' cannot be empty")
570 # DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
571 set_instance_data("last_nodeinfo", domain, time.time())
572 set_instance_data("last_updated" , domain, time.time())
574 # Running pending updated
575 # DEBUG: print(f"DEBUG: Invoking update_instance_data({domain}) ...")
576 update_instance_data(domain)
578 # DEBUG: print("DEBUG: EXIT!")
580 def get_peers(domain: str, software: str) -> list:
581 if type(domain) != str:
582 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
584 raise ValueError(f"Parameter 'domain' cannot be empty")
585 elif type(software) != str and software != None:
586 raise ValueError(f"software[]={type(software)} is not 'str'")
588 # DEBUG: print(f"DEBUG: domain='{domain}',software='{software}' - CALLED!")
591 if software == "misskey":
592 # DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
594 step = config["misskey_offset"]
596 # iterating through all "suspended" (follow-only in its terminology)
597 # instances page-by-page, since that troonware doesn't support
598 # sending them all at once
600 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
602 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
606 }), {"Origin": domain})
608 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
613 }), {"Origin": domain})
615 # DEBUG: print("DEBUG: fetched():", len(fetched))
616 if len(fetched) == 0:
617 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
619 elif len(fetched) != config["misskey_offset"]:
620 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
621 offset = offset + (config["misskey_offset"] - len(fetched))
623 # DEBUG: print("DEBUG: Raising offset by step:", step)
624 offset = offset + step
627 # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]={type(fetched)}")
628 if isinstance(fetched, dict) and "error" in fetched and "message" in fetched["error"]:
629 print(f"WARNING: post_json_api() returned error: {fetched['error']['message']}")
630 update_last_error(domain, fetched["error"]["message"])
634 # DEBUG: print(f"DEBUG: row():{len(row)}")
635 if not "host" in row:
636 print(f"WARNING: row()={len(row)} does not contain element 'host': {row},domain='{domain}'")
638 elif type(row["host"]) != str:
639 print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
641 elif is_blacklisted(row["host"]):
642 # DEBUG: print(f"DEBUG: row[host]='{row['host']}' is blacklisted. domain='{domain}'")
645 # DEBUG: print(f"DEBUG: Adding peer: '{row['host']}'")
646 peers.append(row["host"])
648 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
649 set_instance_data("total_peers", domain, len(peers))
651 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
652 update_last_instance_fetch(domain)
654 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
656 elif software == "lemmy":
657 # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
659 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
662 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
663 if not res.ok or res.status_code >= 400:
664 print("WARNING: Could not reach any JSON API:", domain)
665 update_last_error(domain, res)
666 elif res.ok and isinstance(data, list):
667 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
669 elif "federated_instances" in data:
670 # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
671 peers = peers + add_peers(data["federated_instances"])
672 # DEBUG: print("DEBUG: Added instance(s) to peers")
674 print("WARNING: JSON response does not contain 'federated_instances':", domain)
675 update_last_error(domain, res)
677 except BaseException as e:
678 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
680 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
681 set_instance_data("total_peers", domain, len(peers))
683 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
684 update_last_instance_fetch(domain)
686 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
688 elif software == "peertube":
689 # DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
692 for mode in ["followers", "following"]:
693 # DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
696 res = reqto.get(f"https://{domain}/api/v1/server/{mode}?start={start}&count=100", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
699 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
700 if res.ok and isinstance(data, dict):
701 # DEBUG: print("DEBUG: Success, data:", len(data))
703 # DEBUG: print(f"DEBUG: Found {len(data['data'])} record(s).")
704 for record in data["data"]:
705 # DEBUG: print(f"DEBUG: record()={len(record)}")
706 if mode in record and "host" in record[mode]:
707 # DEBUG: print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
708 peers.append(record[mode]["host"])
710 print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
712 if len(data["data"]) < 100:
713 # DEBUG: print("DEBUG: Reached end of JSON response:", domain)
716 # Continue with next row
719 except BaseException as e:
720 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
722 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
723 set_instance_data("total_peers", domain, len(peers))
725 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
726 update_last_instance_fetch(domain)
728 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
731 # DEBUG: print(f"DEBUG: Fetching get_peers_url='{get_peers_url}' from '{domain}' ...")
733 res = reqto.get(f"https://{domain}{get_peers_url}", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
736 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
737 if not res.ok or res.status_code >= 400:
738 # DEBUG: print(f"DEBUG: Was not able to fetch '{get_peers_url}', trying alternative ...")
739 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
742 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
743 if not res.ok or res.status_code >= 400:
744 print("WARNING: Could not reach any JSON API:", domain)
745 update_last_error(domain, res)
746 elif res.ok and isinstance(data, list):
747 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
749 elif "federated_instances" in data:
750 # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
751 peers = peers + add_peers(data["federated_instances"])
752 # DEBUG: print("DEBUG: Added instance(s) to peers")
754 print("WARNING: JSON response does not contain 'federated_instances':", domain)
755 update_last_error(domain, res)
757 # DEBUG: print("DEBUG: Querying API was successful:", domain, len(data))
760 except BaseException as e:
761 print("WARNING: Some error during get():", domain, e)
762 update_last_error(domain, e)
764 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
765 set_instance_data("total_peers", domain, len(peers))
767 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
768 update_last_instance_fetch(domain)
770 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
773 def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict = {}) -> dict:
774 if type(domain) != str:
775 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
777 raise ValueError(f"Parameter 'domain' cannot be empty")
778 elif type(path) != str:
779 raise ValueError(f"path[]={type(path)} is not 'str'")
781 raise ValueError(f"path cannot be empty")
782 elif type(parameter) != str:
783 raise ValueError(f"parameter[]={type(parameter)} is not 'str'")
785 # DEBUG: print("DEBUG: Sending POST to domain,path,parameter:", domain, path, parameter, extra_headers)
788 res = reqto.post(f"https://{domain}{path}", data=parameter, headers={**api_headers, **extra_headers}, timeout=(config["connection_timeout"], config["read_timeout"]))
791 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
792 if not res.ok or res.status_code >= 400:
793 print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},res.status_code='{res.status_code}',data[]='{type(data)}'")
794 update_last_error(domain, res)
796 except BaseException as e:
797 print(f"WARNING: Some error during post(): domain='{domain}',path='{path}',parameter()={len(parameter)},exception[{type(e)}]:'{str(e)}'")
799 # DEBUG: print(f"DEBUG: Returning data({len(data)})=[]:{type(data)}")
802 def fetch_nodeinfo(domain: str, path: str = None) -> list:
803 if type(domain) != str:
804 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
806 raise ValueError(f"Parameter 'domain' cannot be empty")
808 # DEBUG: print("DEBUG: Fetching nodeinfo from domain,path:", domain, path)
810 nodeinfo = fetch_wellknown_nodeinfo(domain)
811 # DEBUG: print("DEBUG: nodeinfo:", nodeinfo)
813 if len(nodeinfo) > 0:
814 # DEBUG: print("DEBUG: Returning auto-discovered nodeinfo:", len(nodeinfo))
818 f"https://{domain}/nodeinfo/2.1.json",
819 f"https://{domain}/nodeinfo/2.1",
820 f"https://{domain}/nodeinfo/2.0.json",
821 f"https://{domain}/nodeinfo/2.0",
822 f"https://{domain}/nodeinfo/1.0",
823 f"https://{domain}/api/v1/instance"
827 for request in requests:
828 if path != None and path != "" and request != path:
829 # DEBUG: print(f"DEBUG: path='{path}' does not match request='{request}' - SKIPPED!")
833 # DEBUG: print("DEBUG: Fetching request:", request)
834 res = reqto.get(request, headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
837 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
838 if res.ok and isinstance(data, dict):
839 # DEBUG: print("DEBUG: Success:", request)
840 set_instance_data("detection_mode", domain, "STATIC_CHECK")
841 set_instance_data("nodeinfo_url" , domain, request)
843 elif res.ok and isinstance(data, list):
844 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
846 elif not res.ok or res.status_code >= 400:
847 print("WARNING: Failed fetching nodeinfo from domain:", domain)
848 update_last_error(domain, res)
851 except BaseException as e:
852 # DEBUG: print("DEBUG: Cannot fetch API request:", request)
853 update_last_error(domain, e)
856 # DEBUG: print("DEBUG: Returning data[]:", type(data))
859 def fetch_wellknown_nodeinfo(domain: str) -> list:
860 if type(domain) != str:
861 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
863 raise ValueError(f"Parameter 'domain' cannot be empty")
865 # DEBUG: print("DEBUG: Fetching .well-known info for domain:", domain)
869 res = reqto.get(f"https://{domain}/.well-known/nodeinfo", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
872 # DEBUG: print("DEBUG: domain,res.ok,data[]:", domain, res.ok, type(data))
873 if res.ok and isinstance(data, dict):
875 # DEBUG: print("DEBUG: Found entries:", len(nodeinfo), domain)
876 if "links" in nodeinfo:
877 # DEBUG: print("DEBUG: Found links in nodeinfo():", len(nodeinfo["links"]))
878 for link in nodeinfo["links"]:
879 # DEBUG: print("DEBUG: rel,href:", link["rel"], link["href"])
880 if link["rel"] in nodeinfo_identifier:
881 # DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
882 res = reqto.get(link["href"])
885 # DEBUG: print("DEBUG: href,res.ok,res.status_code:", link["href"], res.ok, res.status_code)
886 if res.ok and isinstance(data, dict):
887 # DEBUG: print("DEBUG: Found JSON nodeinfo():", len(data))
888 set_instance_data("detection_mode", domain, "AUTO_DISCOVERY")
889 set_instance_data("nodeinfo_url" , domain, link["href"])
892 print("WARNING: Unknown 'rel' value:", domain, link["rel"])
894 print("WARNING: nodeinfo does not contain 'links':", domain)
896 except BaseException as e:
897 print("WARNING: Failed fetching .well-known info:", domain)
898 update_last_error(domain, e)
901 # DEBUG: print("DEBUG: Returning data[]:", type(data))
904 def fetch_generator_from_path(domain: str, path: str = "/") -> str:
905 if type(domain) != str:
906 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
908 raise ValueError(f"Parameter 'domain' cannot be empty")
909 elif type(path) != str:
910 raise ValueError(f"path[]={type(path)} is not 'str'")
912 raise ValueError(f"Parameter 'domain' cannot be empty")
914 # DEBUG: print(f"DEBUG: domain='{domain}',path='{path}' - CALLED!")
918 # DEBUG: print(f"DEBUG: Fetching path='{path}' from '{domain}' ...")
919 res = reqto.get(f"https://{domain}{path}", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
921 # DEBUG: print("DEBUG: domain,res.ok,res.status_code,res.text[]:", domain, res.ok, res.status_code, type(res.text))
922 if res.ok and res.status_code < 300 and len(res.text) > 0:
923 # DEBUG: print("DEBUG: Search for <meta name='generator'>:", domain)
924 doc = bs4.BeautifulSoup(res.text, "html.parser")
926 # DEBUG: print("DEBUG: doc[]:", type(doc))
927 generator = doc.find("meta", {"name": "generator"})
928 site_name = doc.find("meta", {"property": "og:site_name"})
930 # DEBUG: print(f"DEBUG: generator='{generator}',site_name='{site_name}'")
931 if isinstance(generator, bs4.element.Tag):
932 # DEBUG: print("DEBUG: Found generator meta tag:", domain)
933 software = tidyup(generator.get("content"))
934 print(f"INFO: domain='{domain}' is generated by '{software}'")
935 set_instance_data("detection_mode", domain, "GENERATOR")
936 remove_pending_error(domain)
937 elif isinstance(site_name, bs4.element.Tag):
938 # DEBUG: print("DEBUG: Found property=og:site_name:", domain)
939 sofware = tidyup(site_name.get("content"))
940 print(f"INFO: domain='{domain}' has og:site_name='{software}'")
941 set_instance_data("detection_mode", domain, "SITE_NAME")
942 remove_pending_error(domain)
944 except BaseException as e:
945 # DEBUG: print(f"DEBUG: Cannot fetch / from '{domain}':", e)
946 update_last_error(domain, e)
949 # DEBUG: print(f"DEBUG: software[]={type(software)}")
950 if type(software) is str and software == "":
951 # DEBUG: print(f"DEBUG: Corrected empty string to None for software of domain='{domain}'")
953 elif type(software) is str and ("." in software or " " in software):
954 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
955 software = remove_version(software)
957 # DEBUG: print(f"DEBUG: software[]={type(software)}")
958 if type(software) is str and " powered by " in software:
959 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
960 software = remove_version(strip_powered_by(software))
961 elif type(software) is str and " hosted on " in software:
962 # DEBUG: print(f"DEBUG: software='{software}' has 'hosted on' in it")
963 software = remove_version(strip_hosted_on(software))
964 elif type(software) is str and " by " in software:
965 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
966 software = strip_until(software, " by ")
967 elif type(software) is str and " see " in software:
968 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
969 software = strip_until(software, " see ")
971 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
974 def determine_software(domain: str, path: str = None) -> str:
975 if type(domain) != str:
976 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
978 raise ValueError(f"Parameter 'domain' cannot be empty")
980 # DEBUG: print("DEBUG: Determining software for domain,path:", domain, path)
983 # DEBUG: print(f"DEBUG: Fetching nodeinfo from '{domain}' ...")
984 data = fetch_nodeinfo(domain, path)
986 # DEBUG: print("DEBUG: data[]:", type(data))
987 if not isinstance(data, dict) or len(data) == 0:
988 # DEBUG: print("DEBUG: Could not determine software type:", domain)
989 return fetch_generator_from_path(domain)
991 # DEBUG: print("DEBUG: data():", len(data), data)
992 if "status" in data and data["status"] == "error" and "message" in data:
993 print("WARNING: JSON response is an error:", data["message"])
994 update_last_error(domain, data["message"])
995 return fetch_generator_from_path(domain)
996 elif "message" in data:
997 print("WARNING: JSON response contains only a message:", data["message"])
998 update_last_error(domain, data["message"])
999 return fetch_generator_from_path(domain)
1000 elif "software" not in data or "name" not in data["software"]:
1001 # DEBUG: print(f"DEBUG: JSON response from domain='{domain}' does not include [software][name], fetching / ...")
1002 software = fetch_generator_from_path(domain)
1004 # DEBUG: print(f"DEBUG: Generator for domain='{domain}' is: {software}, EXIT!")
1007 software = tidyup(data["software"]["name"])
1009 # DEBUG: print("DEBUG: sofware after tidyup():", software)
1010 if software in ["akkoma", "rebased"]:
1011 # DEBUG: print("DEBUG: Setting pleroma:", domain, software)
1012 software = "pleroma"
1013 elif software in ["hometown", "ecko"]:
1014 # DEBUG: print("DEBUG: Setting mastodon:", domain, software)
1015 software = "mastodon"
1016 elif software in ["calckey", "groundpolis", "foundkey", "cherrypick", "meisskey"]:
1017 # DEBUG: print("DEBUG: Setting misskey:", domain, software)
1018 software = "misskey"
1019 elif software.find("/") > 0:
1020 print("WARNING: Spliting of slash:", software)
1021 software = software.split("/")[-1];
1022 elif software.find("|") > 0:
1023 print("WARNING: Spliting of pipe:", software)
1024 software = tidyup(software.split("|")[0]);
1025 elif "powered by" in software:
1026 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
1027 software = strip_powered_by(software)
1028 elif type(software) is str and " by " in software:
1029 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
1030 software = strip_until(software, " by ")
1031 elif type(software) is str and " see " in software:
1032 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
1033 software = strip_until(software, " see ")
1035 # DEBUG: print(f"DEBUG: software[]={type(software)}")
1037 print("WARNING: tidyup() left no software name behind:", domain)
1040 # DEBUG: print(f"DEBUG: software[]={type(software)}")
1041 if str(software) == "":
1042 # DEBUG: print(f"DEBUG: software for '{domain}' was not detected, trying generator ...")
1043 software = fetch_generator_from_path(domain)
1044 elif len(str(software)) > 0 and ("." in software or " " in software):
1045 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
1046 software = remove_version(software)
1048 # DEBUG: print(f"DEBUG: software[]={type(software)}")
1049 if type(software) is str and "powered by" in software:
1050 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
1051 software = remove_version(strip_powered_by(software))
1053 # DEBUG: print("DEBUG: Returning domain,software:", domain, software)
1056 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
1057 if type(reason) != str and reason != None:
1058 raise ValueError(f"Parameter reason[]='{type(reason)}' is not 'str'")
1059 elif type(blocker) != str:
1060 raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
1061 elif type(blocked) != str:
1062 raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
1063 elif type(block_level) != str:
1064 raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'")
1066 # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
1069 "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason IN ('','unknown') LIMIT 1",
1079 # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
1080 if cursor.rowcount == 0:
1081 # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!")
1084 except BaseException as e:
1085 print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
1088 # DEBUG: print("DEBUG: EXIT!")
1090 def update_last_seen(blocker: str, blocked: str, block_level: str):
1091 # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
1094 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
1103 # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
1104 if cursor.rowcount == 0:
1105 # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!")
1108 except BaseException as e:
1109 print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
1112 # DEBUG: print("DEBUG: EXIT!")
1114 def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
1115 # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
1116 if type(blocker) != str:
1117 raise ValueError(f"Parameter blocker[]={type(blocker)} is not 'str'")
1119 raise ValueError(f"Parameter 'blocker' cannot be empty")
1120 elif not validators.domain(blocker.split("/")[0]):
1121 raise ValueError(f"Bad blocker='{blocker}'")
1122 elif type(blocked) != str:
1123 raise ValueError(f"Parameter blocked[]={type(blocked)} is not 'str'")
1125 raise ValueError(f"Parameter 'blocked' cannot be empty")
1126 elif not validators.domain(blocked.split("/")[0]):
1127 raise ValueError(f"Bad blocked='{blocked}'")
1128 elif is_blacklisted(blocker):
1129 raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
1130 elif is_blacklisted(blocked):
1131 raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
1133 print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}', reason='{reason}', block_level='{block_level}'")
1136 "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
1146 except BaseException as e:
1147 print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
1150 # DEBUG: print("DEBUG: EXIT!")
1152 def is_instance_registered(domain: str) -> bool:
1153 if type(domain) != str:
1154 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1156 raise ValueError(f"Parameter 'domain' cannot be empty")
1158 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
1159 if not cache.is_cache_initialized("is_registered"):
1160 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
1162 cursor.execute("SELECT domain FROM instances")
1165 cache.set_all_cache_key("is_registered", cursor.fetchall(), True)
1166 except BaseException as e:
1167 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
1171 registered = cache.is_cache_key_set("is_registered", domain)
1173 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
1176 def add_instance(domain: str, origin: str, originator: str, path: str = None):
1177 if type(domain) != str:
1178 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1180 raise ValueError(f"Parameter 'domain' cannot be empty")
1181 elif type(origin) != str and origin != None:
1182 raise ValueError(f"origin[]={type(origin)} is not 'str'")
1183 elif type(originator) != str:
1184 raise ValueError(f"originator[]={type(originator)} is not 'str'")
1185 elif originator == "":
1186 raise ValueError(f"originator cannot be empty")
1187 elif not validators.domain(domain.split("/")[0]):
1188 raise ValueError(f"Bad domain name='{domain}'")
1189 elif origin is not None and not validators.domain(origin.split("/")[0]):
1190 raise ValueError(f"Bad origin name='{origin}'")
1191 elif is_blacklisted(domain):
1192 raise Exception(f"domain='{domain}' is blacklisted, but method invoked")
1194 # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
1195 software = determine_software(domain, path)
1196 # DEBUG: print("DEBUG: Determined software:", software)
1198 print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')")
1201 "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
1212 cache.set_cache_key("is_registered", domain, True)
1214 if has_pending_instance_data(domain):
1215 # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
1216 set_instance_data("last_status_code" , domain, None)
1217 set_instance_data("last_error_details", domain, None)
1218 update_instance_data(domain)
1219 remove_pending_error(domain)
1221 if domain in pending_errors:
1222 # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
1223 update_last_error(domain, pending_errors[domain])
1224 remove_pending_error(domain)
1226 except BaseException as e:
1227 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
1230 # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
1231 update_last_nodeinfo(domain)
1233 # DEBUG: print("DEBUG: EXIT!")
1235 def send_bot_post(instance: str, blocks: dict):
1236 message = instance + " has blocked the following instances:\n\n"
1239 if len(blocks) > 20:
1241 blocks = blocks[0 : 19]
1243 for block in blocks:
1244 if block["reason"] == None or block["reason"] == '':
1245 message = message + block["blocked"] + " with unspecified reason\n"
1247 if len(block["reason"]) > 420:
1248 block["reason"] = block["reason"][0:419] + "[…]"
1250 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
1253 message = message + "(the list has been truncated to the first 20 entries)"
1255 botheaders = {**api_headers, **{"Authorization": "Bearer " + config["bot_token"]}}
1258 f"{config['bot_instance']}/api/v1/statuses",
1261 "visibility" : config['bot_visibility'],
1262 "content_type": "text/plain"
1270 def get_mastodon_blocks(domain: str) -> dict:
1271 if type(domain) != str:
1272 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1274 raise ValueError(f"Parameter 'domain' cannot be empty")
1276 # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
1278 "Suspended servers": [],
1279 "Filtered media" : [],
1280 "Limited servers" : [],
1281 "Silenced servers" : [],
1285 doc = bs4.BeautifulSoup(
1286 reqto.get(f"https://{domain}/about", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
1289 except BaseException as e:
1290 print("ERROR: Cannot fetch from domain:", domain, e)
1291 update_last_error(domain, e)
1294 for header in doc.find_all("h3"):
1295 header_text = tidyup(header.text)
1297 if header_text in language_mapping:
1298 # DEBUG: print(f"DEBUG: header_text='{header_text}'")
1299 header_text = language_mapping[header_text]
1301 if header_text in blocks or header_text.lower() in blocks:
1302 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
1303 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
1304 blocks[header_text].append(
1306 "domain": tidyup(line.find("span").text),
1307 "hash" : tidyup(line.find("span")["title"][9:]),
1308 "reason": tidyup(line.find_all("td")[1].text),
1312 # DEBUG: print("DEBUG: Returning blocks for domain:", domain)
1314 "reject" : blocks["Suspended servers"],
1315 "media_removal" : blocks["Filtered media"],
1316 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
1319 def get_friendica_blocks(domain: str) -> dict:
1320 if type(domain) != str:
1321 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1323 raise ValueError(f"Parameter 'domain' cannot be empty")
1325 # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
1329 doc = bs4.BeautifulSoup(
1330 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
1333 except BaseException as e:
1334 print("WARNING: Failed to fetch /friendica from domain:", domain, e)
1335 update_last_error(domain, e)
1338 blocklist = doc.find(id="about_blocklist")
1340 # Prevents exceptions:
1341 if blocklist is None:
1342 # DEBUG: print("DEBUG: Instance has no block list:", domain)
1345 for line in blocklist.find("table").find_all("tr")[1:]:
1346 # DEBUG: print(f"DEBUG: line='{line}'")
1348 "domain": tidyup(line.find_all("td")[0].text),
1349 "reason": tidyup(line.find_all("td")[1].text)
1352 # DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
1357 def get_misskey_blocks(domain: str) -> dict:
1358 if type(domain) != str:
1359 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1361 raise ValueError(f"Parameter 'domain' cannot be empty")
1363 # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
1370 step = config["misskey_offset"]
1372 # iterating through all "suspended" (follow-only in its terminology)
1373 # instances page-by-page, since that troonware doesn't support
1374 # sending them all at once
1376 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
1378 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1379 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1384 }), {"Origin": domain})
1386 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1387 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1392 "offset" : offset - 1
1393 }), {"Origin": domain})
1395 # DEBUG: print("DEBUG: fetched():", len(fetched))
1396 if len(fetched) == 0:
1397 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1399 elif len(fetched) != config["misskey_offset"]:
1400 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1401 offset = offset + (config["misskey_offset"] - len(fetched))
1403 # DEBUG: print("DEBUG: Raising offset by step:", step)
1404 offset = offset + step
1406 for instance in fetched:
1408 if instance["isSuspended"]:
1409 blocks["suspended"].append(
1411 "domain": tidyup(instance["host"]),
1412 # no reason field, nothing
1417 except BaseException as e:
1418 print("WARNING: Caught error, exiting loop:", domain, e)
1419 update_last_error(domain, e)
1424 # same shit, different asshole ("blocked" aka full suspend)
1427 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1428 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1433 }), {"Origin": domain})
1435 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1436 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1442 }), {"Origin": domain})
1444 # DEBUG: print("DEBUG: fetched():", len(fetched))
1445 if len(fetched) == 0:
1446 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1448 elif len(fetched) != config["misskey_offset"]:
1449 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1450 offset = offset + (config["misskey_offset"] - len(fetched))
1452 # DEBUG: print("DEBUG: Raising offset by step:", step)
1453 offset = offset + step
1455 for instance in fetched:
1456 if instance["isBlocked"]:
1457 blocks["blocked"].append({
1458 "domain": tidyup(instance["host"]),
1462 except BaseException as e:
1463 print("ERROR: Exception during POST:", domain, e)
1464 update_last_error(domain, e)
1468 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
1469 update_last_instance_fetch(domain)
1471 # DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
1473 "reject" : blocks["blocked"],
1474 "followers_only": blocks["suspended"]
1477 def tidyup(string: str) -> str:
1478 if type(string) != str:
1479 raise ValueError(f"Parameter string[]={type(string)} is not expected")
1481 # some retards put their blocks in variable case
1482 string = string.lower().strip()
1484 # other retards put the port
1485 string = re.sub("\:\d+$", "", string)
1487 # bigger retards put the schema in their blocklist, sometimes even without slashes
1488 string = re.sub("^https?\:(\/*)", "", string)
1490 # and trailing slash
1491 string = re.sub("\/$", "", string)
1494 string = re.sub("^\@", "", string)
1496 # the biggest retards of them all try to block individual users
1497 string = re.sub("(.+)\@", "", string)