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 from fba import instances
30 with open("config.json") as f:
31 config = json.loads(f.read())
33 # Don't check these, known trolls/flooders/testing/developing
35 # Floods network with fake nodes as "research" project
36 "activitypub-troll.cf",
42 "social.shrimpcam.pw",
44 "mastotroll.netz.org",
45 # Testing/developing installations
48 "misskeytest.chn.moe",
51 # Array with pending errors needed to be written to database
55 # "rel" identifiers (no real URLs)
56 nodeinfo_identifier = [
57 "https://nodeinfo.diaspora.software/ns/schema/2.1",
58 "https://nodeinfo.diaspora.software/ns/schema/2.0",
59 "https://nodeinfo.diaspora.software/ns/schema/1.1",
60 "https://nodeinfo.diaspora.software/ns/schema/1.0",
61 "http://nodeinfo.diaspora.software/ns/schema/2.1",
62 "http://nodeinfo.diaspora.software/ns/schema/2.0",
63 "http://nodeinfo.diaspora.software/ns/schema/1.1",
64 "http://nodeinfo.diaspora.software/ns/schema/1.0",
67 # HTTP headers for non-API requests
69 "User-Agent": config["useragent"],
72 # HTTP headers for API requests
74 "User-Agent": config["useragent"],
75 "Content-Type": "application/json",
80 "Silenced instances" : "Silenced servers",
81 "Suspended instances" : "Suspended servers",
82 "Limited instances" : "Limited servers",
83 # Mappuing German -> English
84 "Gesperrte Server" : "Suspended servers",
85 "Gefilterte Medien" : "Filtered media",
86 "Stummgeschaltete Server" : "Silenced servers",
88 "停止済みのサーバー" : "Suspended servers",
89 "制限中のサーバー" : "Limited servers",
90 "メディアを拒否しているサーバー": "Filtered media",
91 "サイレンス済みのサーバー" : "Silenced servers",
93 "שרתים מושעים" : "Suspended servers",
94 "מדיה מסוננת" : "Filtered media",
95 "שרתים מוגבלים" : "Silenced servers",
97 "Serveurs suspendus" : "Suspended servers",
98 "Médias filtrés" : "Filtered media",
99 "Serveurs limités" : "Limited servers",
100 "Serveurs modérés" : "Limited servers",
103 # URL for fetching peers
104 get_peers_url = "/api/v1/instance/peers"
106 # Connect to database
107 connection = sqlite3.connect("blocks.db")
108 cursor = connection.cursor()
110 # Pattern instance for version numbers
112 # semantic version number (with v|V) prefix)
113 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-]+)*))?)?$"),
114 # non-sematic, e.g. 1.2.3.4
115 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*))?)$"),
116 # non-sematic, e.g. 2023-05[-dev]
117 re.compile("^(?P<year>[1-9]{1}[0-9]{3})\.(?P<month>[0-9]{2})(-dev){0,1}$"),
118 # non-semantic, e.g. abcdef0
119 re.compile("^[a-f0-9]{7}$"),
122 ##### Other functions #####
124 def is_primitive(var: any) -> bool:
125 # NOISY-DEBUG: print(f"DEBUG: var[]='{type(var)}' - CALLED!")
126 return type(var) in {int, str, float, bool} or var == None
128 def fetch_instances(domain: str, origin: str, software: str, script: str, path: str = None):
129 if type(domain) != str:
130 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
132 raise ValueError(f"Parameter 'domain' cannot be empty")
133 elif type(origin) != str and origin != None:
134 raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
135 elif type(script) != str:
136 raise ValueError(f"Parameter script[]={type(script)} is not 'str'")
138 raise ValueError(f"Parameter 'domain' cannot be empty")
140 # DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
141 if not is_instance_registered(domain):
142 # DEBUG: print("DEBUG: Adding new domain:", domain, origin)
143 add_instance(domain, origin, script, path)
145 # DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
146 peerlist = get_peers(domain, software)
148 if (peerlist is None):
149 print("ERROR: Cannot fetch peers:", domain)
151 elif instances.has_pending_instance_data(domain):
152 # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
153 instances.update_instance_data(domain)
155 print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
156 for instance in peerlist:
158 # Skip "None" types as tidup() cannot parse them
161 # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - BEFORE")
162 instance = tidyup_domain(instance)
163 # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - AFTER")
166 print("WARNING: Empty instance after tidyup_domain(), domain:", domain)
168 elif not validators.domain(instance.split("/")[0]):
169 print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
171 elif is_blacklisted(instance):
172 # DEBUG: print("DEBUG: instance is blacklisted:", instance)
175 # DEBUG: print("DEBUG: Handling instance:", instance)
177 if not is_instance_registered(instance):
178 # DEBUG: print("DEBUG: Adding new instance:", instance, domain)
179 add_instance(instance, domain, sys.argv[0])
180 except BaseException as e:
181 print(f"ERROR: instance='{instance}',exception[{type(e)}]:'{str(e)}'")
184 # DEBUG: print("DEBUG: EXIT!")
186 def add_peers(rows: dict) -> list:
187 # DEBUG: print(f"DEBUG: rows()={len(rows)} - CALLED!")
189 for element in ["linked", "allowed", "blocked"]:
190 # DEBUG: print(f"DEBUG: Checking element='{element}'")
191 if element in rows and rows[element] != None:
192 # DEBUG: print(f"DEBUG: Adding {len(rows[element])} peer(s) to peers list ...")
193 for peer in rows[element]:
194 # DEBUG: print(f"DEBUG: peer='{peer}' - BEFORE!")
195 peer = tidyup_domain(peer)
197 # DEBUG: print(f"DEBUG: peer='{peer}' - AFTER!")
198 if is_blacklisted(peer):
199 # DEBUG: print(f"DEBUG: peer='{peer}' is blacklisted, skipped!")
202 # DEBUG: print(f"DEBUG: Adding peer='{peer}' ...")
205 # DEBUG: print(f"DEBUG: peers()={len(peers)} - EXIT!")
208 def remove_version(software: str) -> str:
209 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
210 if not "." in software and " " not in software:
211 print(f"WARNING: software='{software}' does not contain a version number.")
216 temp = software.split(";")[0]
217 elif "," in software:
218 temp = software.split(",")[0]
219 elif " - " in software:
220 temp = software.split(" - ")[0]
222 # DEBUG: print(f"DEBUG: software='{software}'")
225 version = temp.split(" ")[-1]
226 elif "/" in software:
227 version = temp.split("/")[-1]
228 elif "-" in software:
229 version = temp.split("-")[-1]
231 # DEBUG: print(f"DEBUG: Was not able to find common seperator, returning untouched software='{software}'")
236 # DEBUG: print(f"DEBUG: Checking {len(patterns)} patterns ...")
237 for pattern in patterns:
239 match = pattern.match(version)
241 # DEBUG: print(f"DEBUG: match[]={type(match)}")
242 if type(match) is re.Match:
245 # DEBUG: print(f"DEBUG: version[{type(version)}]='{version}',match='{match}'")
246 if type(match) is not re.Match:
247 print(f"WARNING: version='{version}' does not match regex, leaving software='{software}' untouched.")
250 # DEBUG: print(f"DEBUG: Found valid version number: '{version}', removing it ...")
251 end = len(temp) - len(version) - 1
253 # DEBUG: print(f"DEBUG: end[{type(end)}]={end}")
254 software = temp[0:end].strip()
255 if " version" in software:
256 # DEBUG: print(f"DEBUG: software='{software}' contains word ' version'")
257 software = strip_until(software, " version")
259 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
262 def strip_powered_by(software: str) -> str:
263 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
265 print(f"ERROR: Bad method call, 'software' is empty")
266 raise Exception("Parameter 'software' is empty")
267 elif not "powered by" in software:
268 print(f"WARNING: Cannot find 'powered by' in '{software}'!")
271 start = software.find("powered by ")
272 # DEBUG: print(f"DEBUG: start[{type(start)}]='{start}'")
274 software = software[start + 11:].strip()
275 # DEBUG: print(f"DEBUG: software='{software}'")
277 software = strip_until(software, " - ")
279 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
282 def strip_hosted_on(software: str) -> str:
283 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
285 print(f"ERROR: Bad method call, 'software' is empty")
286 raise Exception("Parameter 'software' is empty")
287 elif not "hosted on" in software:
288 print(f"WARNING: Cannot find 'hosted on' in '{software}'!")
291 end = software.find("hosted on ")
292 # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'")
294 software = software[0, start].strip()
295 # DEBUG: print(f"DEBUG: software='{software}'")
297 software = strip_until(software, " - ")
299 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
302 def strip_until(software: str, until: str) -> str:
303 # DEBUG: print(f"DEBUG: software='{software}',until='{until}' - CALLED!")
305 print(f"ERROR: Bad method call, 'software' is empty")
306 raise Exception("Parameter 'software' is empty")
308 print(f"ERROR: Bad method call, 'until' is empty")
309 raise Exception("Parameter 'until' is empty")
310 elif not until in software:
311 print(f"WARNING: Cannot find '{until}' in '{software}'!")
314 # Next, strip until part
315 end = software.find(until)
317 # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'")
319 software = software[0:end].strip()
321 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
324 def is_blacklisted(domain: str) -> bool:
325 if type(domain) != str:
326 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
328 raise ValueError(f"Parameter 'domain' cannot be empty")
331 for peer in blacklist:
337 def remove_pending_error(domain: str):
338 if type(domain) != str:
339 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
341 raise ValueError(f"Parameter 'domain' cannot be empty")
344 # Prevent updating any pending errors, nodeinfo was found
345 del pending_errors[domain]
350 # DEBUG: print("DEBUG: EXIT!")
352 def get_hash(domain: str) -> str:
353 if type(domain) != str:
354 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
356 raise ValueError(f"Parameter 'domain' cannot be empty")
358 return hashlib.sha256(domain.encode("utf-8")).hexdigest()
360 def update_last_blocked(domain: str):
361 if type(domain) != str:
362 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
364 raise ValueError(f"Parameter 'domain' cannot be empty")
366 # DEBUG: print("DEBUG: Updating last_blocked for domain", domain)
367 instances.set_instance_data("last_blocked", domain, time.time())
369 # Running pending updated
370 # DEBUG: print(f"DEBUG: Invoking instances.update_instance_data({domain}) ...")
371 instances.update_instance_data(domain)
373 # DEBUG: print("DEBUG: EXIT!")
375 def log_error(domain: str, res: any):
376 # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
377 if type(domain) != str:
378 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
380 raise ValueError(f"Parameter 'domain' cannot be empty")
383 # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
384 if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
387 # DEBUG: print("DEBUG: AFTER res[]:", type(res))
389 cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, 999, ?, ?)",[
395 cursor.execute("INSERT INTO error_log (domain, error_code, error_message, created) VALUES (?, ?, ?, ?)",[
402 # Cleanup old entries
403 # DEBUG: print(f"DEBUG: Purging old records (distance: {config['error_log_cleanup']})")
404 cursor.execute("DELETE FROM error_log WHERE created < ?", [time.time() - config["error_log_cleanup"]])
405 except BaseException as e:
406 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
409 # DEBUG: print("DEBUG: EXIT!")
411 def update_last_error(domain: str, res: any):
412 # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
413 if type(domain) != str:
414 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
416 raise ValueError(f"Parameter 'domain' cannot be empty")
418 # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
419 if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
422 # DEBUG: print("DEBUG: AFTER res[]:", type(res))
424 # DEBUG: print(f"DEBUG: Setting last_error_details='{res}'");
425 instances.set_instance_data("last_status_code" , domain, 999)
426 instances.set_instance_data("last_error_details", domain, res)
428 # DEBUG: print(f"DEBUG: Setting last_error_details='{res.reason}'");
429 instances.set_instance_data("last_status_code" , domain, res.status_code)
430 instances.set_instance_data("last_error_details", domain, res.reason)
432 # Running pending updated
433 # DEBUG: print(f"DEBUG: Invoking instances.update_instance_data({domain}) ...")
434 instances.update_instance_data(domain)
436 log_error(domain, res)
438 # DEBUG: print("DEBUG: EXIT!")
440 def update_last_instance_fetch(domain: str):
441 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
442 if type(domain) != str:
443 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
445 raise ValueError(f"Parameter 'domain' cannot be empty")
447 # DEBUG: print("DEBUG: Updating last_instance_fetch for domain:", domain)
448 instances.set_instance_data("last_instance_fetch", domain, time.time())
450 # Running pending updated
451 # DEBUG: print(f"DEBUG: Invoking instances.update_instance_data({domain}) ...")
452 instances.update_instance_data(domain)
454 # DEBUG: print("DEBUG: EXIT!")
456 def update_last_nodeinfo(domain: str):
457 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
458 if type(domain) != str:
459 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
461 raise ValueError(f"Parameter 'domain' cannot be empty")
463 # DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
464 instances.set_instance_data("last_nodeinfo", domain, time.time())
465 instances.set_instance_data("last_updated" , domain, time.time())
467 # Running pending updated
468 # DEBUG: print(f"DEBUG: Invoking instances.update_instance_data({domain}) ...")
469 instances.update_instance_data(domain)
471 # DEBUG: print("DEBUG: EXIT!")
473 def get_peers(domain: str, software: str) -> list:
474 # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software={software} - CALLED!")
475 if type(domain) != str:
476 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
478 raise ValueError(f"Parameter 'domain' cannot be empty")
479 elif type(software) != str and software != None:
480 raise ValueError(f"software[]={type(software)} is not 'str'")
482 # DEBUG: print(f"DEBUG: domain='{domain}',software='{software}' - CALLED!")
485 if software == "misskey":
486 # DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
488 step = config["misskey_offset"]
490 # iterating through all "suspended" (follow-only in its terminology)
491 # instances page-by-page, since that troonware doesn't support
492 # sending them all at once
494 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
496 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
500 }), {"Origin": domain})
502 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
507 }), {"Origin": domain})
509 # DEBUG: print("DEBUG: fetched():", len(fetched))
510 if len(fetched) == 0:
511 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
513 elif len(fetched) != config["misskey_offset"]:
514 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
515 offset = offset + (config["misskey_offset"] - len(fetched))
517 # DEBUG: print("DEBUG: Raising offset by step:", step)
518 offset = offset + step
521 # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]={type(fetched)}")
522 if isinstance(fetched, dict) and "error" in fetched and "message" in fetched["error"]:
523 print(f"WARNING: post_json_api() returned error: {fetched['error']['message']}")
524 update_last_error(domain, fetched["error"]["message"])
528 # DEBUG: print(f"DEBUG: row():{len(row)}")
529 if not "host" in row:
530 print(f"WARNING: row()={len(row)} does not contain element 'host': {row},domain='{domain}'")
532 elif type(row["host"]) != str:
533 print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
535 elif is_blacklisted(row["host"]):
536 # DEBUG: print(f"DEBUG: row[host]='{row['host']}' is blacklisted. domain='{domain}'")
539 # DEBUG: print(f"DEBUG: Adding peer: '{row['host']}'")
540 peers.append(row["host"])
542 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
543 instances.set_instance_data("total_peers", domain, len(peers))
545 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
546 update_last_instance_fetch(domain)
548 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
550 elif software == "lemmy":
551 # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
553 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
556 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
557 if not res.ok or res.status_code >= 400:
558 print("WARNING: Could not reach any JSON API:", domain)
559 update_last_error(domain, res)
560 elif res.ok and isinstance(data, list):
561 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
563 elif "federated_instances" in data:
564 # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
565 peers = peers + add_peers(data["federated_instances"])
566 # DEBUG: print("DEBUG: Added instance(s) to peers")
568 print("WARNING: JSON response does not contain 'federated_instances':", domain)
569 update_last_error(domain, res)
571 except BaseException as e:
572 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
574 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
575 instances.set_instance_data("total_peers", domain, len(peers))
577 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
578 update_last_instance_fetch(domain)
580 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
582 elif software == "peertube":
583 # DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
586 for mode in ["followers", "following"]:
587 # DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
590 res = reqto.get(f"https://{domain}/api/v1/server/{mode}?start={start}&count=100", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
593 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
594 if res.ok and isinstance(data, dict):
595 # DEBUG: print("DEBUG: Success, data:", len(data))
597 # DEBUG: print(f"DEBUG: Found {len(data['data'])} record(s).")
598 for record in data["data"]:
599 # DEBUG: print(f"DEBUG: record()={len(record)}")
600 if mode in record and "host" in record[mode]:
601 # DEBUG: print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
602 peers.append(record[mode]["host"])
604 print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
606 if len(data["data"]) < 100:
607 # DEBUG: print("DEBUG: Reached end of JSON response:", domain)
610 # Continue with next row
613 except BaseException as e:
614 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
616 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
617 instances.set_instance_data("total_peers", domain, len(peers))
619 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
620 update_last_instance_fetch(domain)
622 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
625 # DEBUG: print(f"DEBUG: Fetching get_peers_url='{get_peers_url}' from '{domain}' ...")
627 res = reqto.get(f"https://{domain}{get_peers_url}", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
630 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
631 if not res.ok or res.status_code >= 400:
632 # DEBUG: print(f"DEBUG: Was not able to fetch '{get_peers_url}', trying alternative ...")
633 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
636 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
637 if not res.ok or res.status_code >= 400:
638 print("WARNING: Could not reach any JSON API:", domain)
639 update_last_error(domain, res)
640 elif res.ok and isinstance(data, list):
641 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
643 elif "federated_instances" in data:
644 # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
645 peers = peers + add_peers(data["federated_instances"])
646 # DEBUG: print("DEBUG: Added instance(s) to peers")
648 print("WARNING: JSON response does not contain 'federated_instances':", domain)
649 update_last_error(domain, res)
651 # DEBUG: print("DEBUG: Querying API was successful:", domain, len(data))
654 except BaseException as e:
655 print("WARNING: Some error during get():", domain, e)
656 update_last_error(domain, e)
658 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
659 instances.set_instance_data("total_peers", domain, len(peers))
661 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
662 update_last_instance_fetch(domain)
664 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
667 def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict = {}) -> dict:
668 if type(domain) != str:
669 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
671 raise ValueError(f"Parameter 'domain' cannot be empty")
672 elif type(path) != str:
673 raise ValueError(f"path[]={type(path)} is not 'str'")
675 raise ValueError(f"path cannot be empty")
676 elif type(parameter) != str:
677 raise ValueError(f"parameter[]={type(parameter)} is not 'str'")
679 # DEBUG: print("DEBUG: Sending POST to domain,path,parameter:", domain, path, parameter, extra_headers)
682 res = reqto.post(f"https://{domain}{path}", data=parameter, headers={**api_headers, **extra_headers}, timeout=(config["connection_timeout"], config["read_timeout"]))
685 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
686 if not res.ok or res.status_code >= 400:
687 print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},res.status_code='{res.status_code}',data[]='{type(data)}'")
688 update_last_error(domain, res)
690 except BaseException as e:
691 print(f"WARNING: Some error during post(): domain='{domain}',path='{path}',parameter()={len(parameter)},exception[{type(e)}]:'{str(e)}'")
693 # DEBUG: print(f"DEBUG: Returning data({len(data)})=[]:{type(data)}")
696 def fetch_nodeinfo(domain: str, path: str = None) -> list:
697 # DEBUG: print(f"DEBUG: domain='{domain}',path={path} - CALLED!")
698 if type(domain) != str:
699 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
701 raise ValueError(f"Parameter 'domain' cannot be empty")
702 elif type(path) != str and path != None:
703 raise ValueError(f"Parameter path[]={type(path)} is not 'str'")
705 # DEBUG: print("DEBUG: Fetching nodeinfo from domain,path:", domain, path)
706 nodeinfo = fetch_wellknown_nodeinfo(domain)
708 # DEBUG: print(f"DEBUG: nodeinfo({len(nodeinfo)})={nodeinfo}")
709 if len(nodeinfo) > 0:
710 # DEBUG: print("DEBUG: nodeinfo()={len(nodeinfo))} - EXIT!")
714 f"https://{domain}/nodeinfo/2.1.json",
715 f"https://{domain}/nodeinfo/2.1",
716 f"https://{domain}/nodeinfo/2.0.json",
717 f"https://{domain}/nodeinfo/2.0",
718 f"https://{domain}/nodeinfo/1.0",
719 f"https://{domain}/api/v1/instance"
723 for request in requests:
724 if path != None and path != "" and request != path:
725 # DEBUG: print(f"DEBUG: path='{path}' does not match request='{request}' - SKIPPED!")
729 # DEBUG: print("DEBUG: Fetching request:", request)
730 res = reqto.get(request, headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
733 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
734 if res.ok and isinstance(data, dict):
735 # DEBUG: print("DEBUG: Success:", request)
736 instances.set_instance_data("detection_mode", domain, "STATIC_CHECK")
737 instances.set_instance_data("nodeinfo_url" , domain, request)
739 elif res.ok and isinstance(data, list):
740 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{data}'")
742 elif not res.ok or res.status_code >= 400:
743 print("WARNING: Failed fetching nodeinfo from domain:", domain)
744 update_last_error(domain, res)
747 except BaseException as e:
748 # DEBUG: print("DEBUG: Cannot fetch API request:", request)
749 update_last_error(domain, e)
752 # DEBUG: print(f"DEBUG: data()={len(data)} - EXIT!")
755 def fetch_wellknown_nodeinfo(domain: str) -> list:
756 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
757 if type(domain) != str:
758 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
760 raise ValueError(f"Parameter 'domain' cannot be empty")
762 # DEBUG: print("DEBUG: Fetching .well-known info for domain:", domain)
766 res = reqto.get(f"https://{domain}/.well-known/nodeinfo", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
769 # DEBUG: print("DEBUG: domain,res.ok,data[]:", domain, res.ok, type(data))
770 if res.ok and isinstance(data, dict):
772 # DEBUG: print("DEBUG: Found entries:", len(nodeinfo), domain)
773 if "links" in nodeinfo:
774 # DEBUG: print("DEBUG: Found links in nodeinfo():", len(nodeinfo["links"]))
775 for link in nodeinfo["links"]:
776 # DEBUG: print("DEBUG: rel,href:", link["rel"], link["href"])
777 if link["rel"] in nodeinfo_identifier:
778 # DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
779 res = reqto.get(link["href"])
782 # DEBUG: print("DEBUG: href,res.ok,res.status_code:", link["href"], res.ok, res.status_code)
783 if res.ok and isinstance(data, dict):
784 # DEBUG: print("DEBUG: Found JSON nodeinfo():", len(data))
785 instances.set_instance_data("detection_mode", domain, "AUTO_DISCOVERY")
786 instances.set_instance_data("nodeinfo_url" , domain, link["href"])
789 print("WARNING: Unknown 'rel' value:", domain, link["rel"])
791 print("WARNING: nodeinfo does not contain 'links':", domain)
793 except BaseException as e:
794 print("WARNING: Failed fetching .well-known info:", domain)
795 update_last_error(domain, e)
798 # DEBUG: print("DEBUG: Returning data[]:", type(data))
801 def fetch_generator_from_path(domain: str, path: str = "/") -> str:
802 # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},path={path} - CALLED!")
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")
807 elif type(path) != str:
808 raise ValueError(f"path[]={type(path)} is not 'str'")
810 raise ValueError(f"Parameter 'domain' cannot be empty")
812 # DEBUG: print(f"DEBUG: domain='{domain}',path='{path}' - CALLED!")
816 # DEBUG: print(f"DEBUG: Fetching path='{path}' from '{domain}' ...")
817 res = reqto.get(f"https://{domain}{path}", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
819 # DEBUG: print("DEBUG: domain,res.ok,res.status_code,res.text[]:", domain, res.ok, res.status_code, type(res.text))
820 if res.ok and res.status_code < 300 and len(res.text) > 0:
821 # DEBUG: print("DEBUG: Search for <meta name='generator'>:", domain)
822 doc = bs4.BeautifulSoup(res.text, "html.parser")
824 # DEBUG: print("DEBUG: doc[]:", type(doc))
825 generator = doc.find("meta", {"name": "generator"})
826 site_name = doc.find("meta", {"property": "og:site_name"})
828 # DEBUG: print(f"DEBUG: generator='{generator}',site_name='{site_name}'")
829 if isinstance(generator, bs4.element.Tag):
830 # DEBUG: print("DEBUG: Found generator meta tag:", domain)
831 software = tidyup_domain(generator.get("content"))
832 print(f"INFO: domain='{domain}' is generated by '{software}'")
833 instances.set_instance_data("detection_mode", domain, "GENERATOR")
834 remove_pending_error(domain)
835 elif isinstance(site_name, bs4.element.Tag):
836 # DEBUG: print("DEBUG: Found property=og:site_name:", domain)
837 sofware = tidyup_domain(site_name.get("content"))
838 print(f"INFO: domain='{domain}' has og:site_name='{software}'")
839 instances.set_instance_data("detection_mode", domain, "SITE_NAME")
840 remove_pending_error(domain)
842 except BaseException as e:
843 # DEBUG: print(f"DEBUG: Cannot fetch / from '{domain}':", e)
844 update_last_error(domain, e)
847 # DEBUG: print(f"DEBUG: software[]={type(software)}")
848 if type(software) is str and software == "":
849 # DEBUG: print(f"DEBUG: Corrected empty string to None for software of domain='{domain}'")
851 elif type(software) is str and ("." in software or " " in software):
852 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
853 software = remove_version(software)
855 # DEBUG: print(f"DEBUG: software[]={type(software)}")
856 if type(software) is str and " powered by " in software:
857 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
858 software = remove_version(strip_powered_by(software))
859 elif type(software) is str and " hosted on " in software:
860 # DEBUG: print(f"DEBUG: software='{software}' has 'hosted on' in it")
861 software = remove_version(strip_hosted_on(software))
862 elif type(software) is str and " by " in software:
863 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
864 software = strip_until(software, " by ")
865 elif type(software) is str and " see " in software:
866 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
867 software = strip_until(software, " see ")
869 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
872 def determine_software(domain: str, path: str = None) -> str:
873 # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},path={path} - CALLED!")
874 if type(domain) != str:
875 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
877 raise ValueError(f"Parameter 'domain' cannot be empty")
878 elif type(path) != str and path != None:
879 raise ValueError(f"Parameter path[]={type(path)} is not 'str'")
881 # DEBUG: print("DEBUG: Determining software for domain,path:", domain, path)
884 # DEBUG: print(f"DEBUG: Fetching nodeinfo from '{domain}' ...")
885 data = fetch_nodeinfo(domain, path)
887 # DEBUG: print("DEBUG: data[]:", type(data))
888 if not isinstance(data, dict) or len(data) == 0:
889 # DEBUG: print("DEBUG: Could not determine software type:", domain)
890 return fetch_generator_from_path(domain)
892 # DEBUG: print("DEBUG: data():", len(data), data)
893 if "status" in data and data["status"] == "error" and "message" in data:
894 print("WARNING: JSON response is an error:", data["message"])
895 update_last_error(domain, data["message"])
896 return fetch_generator_from_path(domain)
897 elif "message" in data:
898 print("WARNING: JSON response contains only a message:", data["message"])
899 update_last_error(domain, data["message"])
900 return fetch_generator_from_path(domain)
901 elif "software" not in data or "name" not in data["software"]:
902 # DEBUG: print(f"DEBUG: JSON response from domain='{domain}' does not include [software][name], fetching / ...")
903 software = fetch_generator_from_path(domain)
905 # DEBUG: print(f"DEBUG: Generator for domain='{domain}' is: {software}, EXIT!")
908 software = tidyup_domain(data["software"]["name"])
910 # DEBUG: print("DEBUG: sofware after tidyup_domain():", software)
911 if software in ["akkoma", "rebased"]:
912 # DEBUG: print("DEBUG: Setting pleroma:", domain, software)
914 elif software in ["hometown", "ecko"]:
915 # DEBUG: print("DEBUG: Setting mastodon:", domain, software)
916 software = "mastodon"
917 elif software in ["calckey", "groundpolis", "foundkey", "cherrypick", "meisskey"]:
918 # DEBUG: print("DEBUG: Setting misskey:", domain, software)
920 elif software.find("/") > 0:
921 print("WARNING: Spliting of slash:", software)
922 software = software.split("/")[-1];
923 elif software.find("|") > 0:
924 print("WARNING: Spliting of pipe:", software)
925 software = tidyup_domain(software.split("|")[0]);
926 elif "powered by" in software:
927 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
928 software = strip_powered_by(software)
929 elif type(software) is str and " by " in software:
930 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
931 software = strip_until(software, " by ")
932 elif type(software) is str and " see " in software:
933 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
934 software = strip_until(software, " see ")
936 # DEBUG: print(f"DEBUG: software[]={type(software)}")
938 print("WARNING: tidyup_domain() left no software name behind:", domain)
941 # DEBUG: print(f"DEBUG: software[]={type(software)}")
942 if str(software) == "":
943 # DEBUG: print(f"DEBUG: software for '{domain}' was not detected, trying generator ...")
944 software = fetch_generator_from_path(domain)
945 elif len(str(software)) > 0 and ("." in software or " " in software):
946 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
947 software = remove_version(software)
949 # DEBUG: print(f"DEBUG: software[]={type(software)}")
950 if type(software) is str and "powered by" in software:
951 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
952 software = remove_version(strip_powered_by(software))
954 # DEBUG: print("DEBUG: Returning domain,software:", domain, software)
957 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
958 # DEBUG: print(f"DEBUG: reason='{reason}',blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
959 if type(reason) != str and reason != None:
960 raise ValueError(f"Parameter reason[]='{type(reason)}' is not 'str'")
961 elif type(blocker) != str:
962 raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
963 elif type(blocked) != str:
964 raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
965 elif type(block_level) != str:
966 raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'")
968 # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
971 "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason IN ('','unknown') LIMIT 1",
981 # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
982 if cursor.rowcount == 0:
983 # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!")
986 except BaseException as e:
987 print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
990 # DEBUG: print("DEBUG: EXIT!")
992 def update_last_seen(blocker: str, blocked: str, block_level: str):
993 # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
996 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
1005 # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
1006 if cursor.rowcount == 0:
1007 # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!")
1010 except BaseException as e:
1011 print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
1014 # DEBUG: print("DEBUG: EXIT!")
1016 def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool:
1017 # DEBUG: print(f"DEBUG: blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
1018 if type(blocker) != str:
1019 raise ValueError(f"Parameter blocker[]={type(blocker)} is not of type 'str'")
1021 raise ValueError("Parameter 'blocker' cannot be empty")
1022 elif type(blocked) != str:
1023 raise ValueError(f"Parameter blocked[]={type(blocked)} is not of type 'str'")
1025 raise ValueError("Parameter 'blocked' cannot be empty")
1026 elif type(block_level) != str:
1027 raise ValueError(f"Parameter block_level[]={type(block_level)} is not of type 'str'")
1028 elif block_level == "":
1029 raise ValueError("Parameter 'block_level' cannot be empty")
1032 "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
1040 is_blocked = cursor.fetchone() != None
1042 # DEBUG: print(f"DEBUG: is_blocked='{is_blocked}' - EXIT!")
1045 def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
1046 # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
1047 if type(blocker) != str:
1048 raise ValueError(f"Parameter blocker[]={type(blocker)} is not 'str'")
1050 raise ValueError(f"Parameter 'blocker' cannot be empty")
1051 elif not validators.domain(blocker.split("/")[0]):
1052 raise ValueError(f"Bad blocker='{blocker}'")
1053 elif type(blocked) != str:
1054 raise ValueError(f"Parameter blocked[]={type(blocked)} is not 'str'")
1056 raise ValueError(f"Parameter 'blocked' cannot be empty")
1057 elif not validators.domain(blocked.split("/")[0]):
1058 raise ValueError(f"Bad blocked='{blocked}'")
1059 elif is_blacklisted(blocker):
1060 raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
1061 elif is_blacklisted(blocked):
1062 raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
1064 print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}', reason='{reason}', block_level='{block_level}'")
1067 "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
1077 except BaseException as e:
1078 print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
1081 # DEBUG: print("DEBUG: EXIT!")
1083 def is_instance_registered(domain: str) -> bool:
1084 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
1085 if type(domain) != str:
1086 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1088 raise ValueError(f"Parameter 'domain' cannot be empty")
1090 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
1091 if not cache.is_cache_initialized("is_registered"):
1092 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
1094 cursor.execute("SELECT domain FROM instances")
1097 cache.set_all_cache_key("is_registered", cursor.fetchall(), True)
1098 except BaseException as e:
1099 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
1103 registered = cache.is_cache_key_set("is_registered", domain)
1105 # NOISY-DEBUG: # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
1108 def add_instance(domain: str, origin: str, originator: str, path: str = None):
1109 # DEBUG: print(f"DEBUG: domain={domain},origin={origin},originator={originator},path={path} - CALLED!")
1110 if type(domain) != str:
1111 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1113 raise ValueError(f"Parameter 'domain' cannot be empty")
1114 elif type(origin) != str and origin != None:
1115 raise ValueError(f"origin[]={type(origin)} is not 'str'")
1116 elif type(originator) != str:
1117 raise ValueError(f"originator[]={type(originator)} is not 'str'")
1118 elif originator == "":
1119 raise ValueError(f"originator cannot be empty")
1120 elif not validators.domain(domain.split("/")[0]):
1121 raise ValueError(f"Bad domain name='{domain}'")
1122 elif origin is not None and not validators.domain(origin.split("/")[0]):
1123 raise ValueError(f"Bad origin name='{origin}'")
1124 elif is_blacklisted(domain):
1125 raise Exception(f"domain='{domain}' is blacklisted, but method invoked")
1127 # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
1128 software = determine_software(domain, path)
1129 # DEBUG: print("DEBUG: Determined software:", software)
1131 print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')")
1134 "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
1145 cache.set_cache_key("is_registered", domain, True)
1147 if instances.has_pending_instance_data(domain):
1148 # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
1149 instances.set_instance_data("last_status_code" , domain, None)
1150 instances.set_instance_data("last_error_details", domain, None)
1151 instances.update_instance_data(domain)
1152 remove_pending_error(domain)
1154 if domain in pending_errors:
1155 # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
1156 update_last_error(domain, pending_errors[domain])
1157 remove_pending_error(domain)
1159 except BaseException as e:
1160 print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
1163 # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
1164 update_last_nodeinfo(domain)
1166 # DEBUG: print("DEBUG: EXIT!")
1168 def send_bot_post(instance: str, blocks: dict):
1169 # DEBUG: print(f"DEBUG: instance={instance},blocks()={len(blocks)} - CALLED!")
1170 message = instance + " has blocked the following instances:\n\n"
1173 if len(blocks) > 20:
1175 blocks = blocks[0 : 19]
1177 for block in blocks:
1178 if block["reason"] == None or block["reason"] == '':
1179 message = message + block["blocked"] + " with unspecified reason\n"
1181 if len(block["reason"]) > 420:
1182 block["reason"] = block["reason"][0:419] + "[…]"
1184 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
1187 message = message + "(the list has been truncated to the first 20 entries)"
1189 botheaders = {**api_headers, **{"Authorization": "Bearer " + config["bot_token"]}}
1192 f"{config['bot_instance']}/api/v1/statuses",
1195 "visibility" : config['bot_visibility'],
1196 "content_type": "text/plain"
1204 def get_mastodon_blocks(domain: str) -> dict:
1205 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
1206 if type(domain) != str:
1207 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1209 raise ValueError(f"Parameter 'domain' cannot be empty")
1211 # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
1213 "Suspended servers": [],
1214 "Filtered media" : [],
1215 "Limited servers" : [],
1216 "Silenced servers" : [],
1220 doc = bs4.BeautifulSoup(
1221 reqto.get(f"https://{domain}/about", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
1224 except BaseException as e:
1225 print("ERROR: Cannot fetch from domain:", domain, e)
1226 update_last_error(domain, e)
1229 for header in doc.find_all("h3"):
1230 header_text = tidyup_domain(header.text)
1232 if header_text in language_mapping:
1233 # DEBUG: print(f"DEBUG: header_text='{header_text}'")
1234 header_text = language_mapping[header_text]
1236 if header_text in blocks or header_text.lower() in blocks:
1237 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
1238 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
1239 blocks[header_text].append(
1241 "domain": tidyup_domain(line.find("span").text),
1242 "hash" : tidyup_domain(line.find("span")["title"][9:]),
1243 "reason": tidyup_domain(line.find_all("td")[1].text),
1247 # DEBUG: print("DEBUG: Returning blocks for domain:", domain)
1249 "reject" : blocks["Suspended servers"],
1250 "media_removal" : blocks["Filtered media"],
1251 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
1254 def get_friendica_blocks(domain: str) -> dict:
1255 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
1256 if type(domain) != str:
1257 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1259 raise ValueError(f"Parameter 'domain' cannot be empty")
1261 # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
1265 doc = bs4.BeautifulSoup(
1266 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
1269 except BaseException as e:
1270 print("WARNING: Failed to fetch /friendica from domain:", domain, e)
1271 update_last_error(domain, e)
1274 blocklist = doc.find(id="about_blocklist")
1276 # Prevents exceptions:
1277 if blocklist is None:
1278 # DEBUG: print("DEBUG: Instance has no block list:", domain)
1281 for line in blocklist.find("table").find_all("tr")[1:]:
1282 # DEBUG: print(f"DEBUG: line='{line}'")
1284 "domain": tidyup_domain(line.find_all("td")[0].text),
1285 "reason": tidyup_domain(line.find_all("td")[1].text)
1288 # DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
1293 def get_misskey_blocks(domain: str) -> dict:
1294 # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
1295 if type(domain) != str:
1296 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
1298 raise ValueError(f"Parameter 'domain' cannot be empty")
1300 # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
1307 step = config["misskey_offset"]
1309 # iterating through all "suspended" (follow-only in its terminology)
1310 # instances page-by-page, since that troonware doesn't support
1311 # sending them all at once
1313 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
1315 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1316 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1321 }), {"Origin": domain})
1323 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1324 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1329 "offset" : offset - 1
1330 }), {"Origin": domain})
1332 # DEBUG: print("DEBUG: fetched():", len(fetched))
1333 if len(fetched) == 0:
1334 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1336 elif len(fetched) != config["misskey_offset"]:
1337 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1338 offset = offset + (config["misskey_offset"] - len(fetched))
1340 # DEBUG: print("DEBUG: Raising offset by step:", step)
1341 offset = offset + step
1343 for instance in fetched:
1345 if instance["isSuspended"]:
1346 blocks["suspended"].append(
1348 "domain": tidyup_domain(instance["host"]),
1349 # no reason field, nothing
1354 except BaseException as e:
1355 print("WARNING: Caught error, exiting loop:", domain, e)
1356 update_last_error(domain, e)
1361 # same shit, different asshole ("blocked" aka full suspend)
1364 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1365 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1370 }), {"Origin": domain})
1372 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1373 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1379 }), {"Origin": domain})
1381 # DEBUG: print("DEBUG: fetched():", len(fetched))
1382 if len(fetched) == 0:
1383 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1385 elif len(fetched) != config["misskey_offset"]:
1386 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1387 offset = offset + (config["misskey_offset"] - len(fetched))
1389 # DEBUG: print("DEBUG: Raising offset by step:", step)
1390 offset = offset + step
1392 for instance in fetched:
1393 if instance["isBlocked"]:
1394 blocks["blocked"].append({
1395 "domain": tidyup_domain(instance["host"]),
1399 except BaseException as e:
1400 print("ERROR: Exception during POST:", domain, e)
1401 update_last_error(domain, e)
1405 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
1406 update_last_instance_fetch(domain)
1408 # DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
1410 "reject" : blocks["blocked"],
1411 "followers_only": blocks["suspended"]
1414 def tidyup_domain(domain: str) -> str:
1415 # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
1416 if type(domain) != str:
1417 raise ValueError(f"Parameter domain[]={type(domain)} is not expected")
1419 # All lower-case and strip spaces out
1420 domain = domain.lower().strip()
1423 domain = re.sub("\:\d+$", "", domain)
1425 # No protocol, sometimes with the slashes
1426 domain = re.sub("^https?\:(\/*)", "", domain)
1429 domain = re.sub("\/$", "", domain)
1432 domain = re.sub("^\@", "", domain)
1434 # No individual users in block lists
1435 domain = re.sub("(.+)\@", "", domain)
1437 # DEBUG: print(f"DEBUG: domain='{domain}' - EXIT!")