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/>.
27 with open("config.json") as f:
28 config = json.loads(f.read())
30 # Don't check these, known trolls/flooders/testing/developing
32 # Floods network with fake nodes as "research" project
33 "activitypub-troll.cf",
39 "social.shrimpcam.pw",
41 "mastotroll.netz.org",
42 # Testing/developing installations
43 "ngrok.io", "ngrok-free.app",
46 # Array with pending errors needed to be written to database
50 # "rel" identifiers (no real URLs)
51 nodeinfo_identifier = [
52 "https://nodeinfo.diaspora.software/ns/schema/2.1",
53 "https://nodeinfo.diaspora.software/ns/schema/2.0",
54 "https://nodeinfo.diaspora.software/ns/schema/1.1",
55 "https://nodeinfo.diaspora.software/ns/schema/1.0",
56 "http://nodeinfo.diaspora.software/ns/schema/2.1",
57 "http://nodeinfo.diaspora.software/ns/schema/2.0",
58 "http://nodeinfo.diaspora.software/ns/schema/1.1",
59 "http://nodeinfo.diaspora.software/ns/schema/1.0",
62 # HTTP headers for non-API requests
64 "User-Agent": config["useragent"],
66 # HTTP headers for API requests
68 "User-Agent": config["useragent"],
69 "Content-Type": "application/json",
72 # Found info from node, such as nodeinfo URL, detection mode that needs to be
73 # written to database. Both arrays must be filled at the same time or else
74 # update_nodeinfos() will fail
76 # Detection mode: 'AUTO_DISCOVERY', 'STATIC_CHECKS' or 'GENERATOR'
77 # NULL means all detection methods have failed (maybe still reachable instance)
81 # Where to fetch peers (other instances)
87 "Silenced instances" : "Silenced servers",
88 "Suspended instances" : "Suspended servers",
89 "Limited instances" : "Limited servers",
90 # Mappuing German -> English
91 "Gesperrte Server" : "Suspended servers",
92 "Gefilterte Medien" : "Filtered media",
93 "Stummgeschaltete Server" : "Silenced servers",
95 "停止済みのサーバー" : "Suspended servers",
96 "制限中のサーバー" : "Limited servers",
97 "メディアを拒否しているサーバー": "Filtered media",
98 "サイレンス済みのサーバー" : "Silenced servers",
100 "שרתים מושעים" : "Suspended servers",
101 "מדיה מסוננת" : "Filtered media",
102 "שרתים מוגבלים" : "Silenced servers",
104 "Serveurs suspendus" : "Suspended servers",
105 "Médias filtrés" : "Filtered media",
106 "Serveurs limités" : "Limited servers",
107 "Serveurs modérés" : "Limited servers",
110 # URL for fetching peers
111 get_peers_url = "/api/v1/instance/peers"
113 # Connect to database
114 connection = sqlite3.connect("blocks.db")
115 cursor = connection.cursor()
117 # Pattern instance for version numbers
119 # semantic version number (with v|V) prefix)
120 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-]+)*))?)?$"),
121 # non-sematic, e.g. 1.2.3.4
122 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*))?)$"),
123 # non-sematic, e.g. 2023-05[-dev]
124 re.compile("^(?P<year>[1-9]{1}[0-9]{3})\.(?P<month>[0-9]{2})(-dev){0,1}$"),
125 # non-semantic, e.g. abcdef0
126 re.compile("^[a-f0-9]{7}$"),
129 def add_peers(rows: dict) -> dict:
130 # DEBUG: print(f"DEBUG: rows()={len(rows)} - CALLED!")
132 for element in ["linked", "allowed", "blocked"]:
133 # DEBUG: print(f"DEBUG: Checking element='{element}'")
134 if element in rows and rows[element] != None:
135 # DEBUG: print(f"DEBUG: Adding {len(rows[element])} peer(s) to peers list ...")
136 peers = {**peers, **rows[element]}
138 # DEBUG: print(f"DEBUG: peers()={len(peers)} - CALLED!")
141 def remove_version(software: str) -> str:
142 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
143 if not "." in software and " " not in software:
144 print(f"WARNING: software='{software}' does not contain a version number.")
149 temp = software.split(";")[0]
150 elif "," in software:
151 temp = software.split(",")[0]
152 elif " - " in software:
153 temp = software.split(" - ")[0]
155 # DEBUG: print(f"DEBUG: software='{software}'")
158 version = temp.split(" ")[-1]
159 elif "/" in software:
160 version = temp.split("/")[-1]
161 elif "-" in software:
162 version = temp.split("-")[-1]
164 # DEBUG: print(f"DEBUG: Was not able to find common seperator, returning untouched software='{software}'")
169 # DEBUG: print(f"DEBUG: Checking {len(patterns)} patterns ...")
170 for pattern in patterns:
172 match = pattern.match(version)
174 # DEBUG: print(f"DEBUG: match[]={type(match)}")
175 if type(match) is re.Match:
178 # DEBUG: print(f"DEBUG: version[{type(version)}]='{version}',match='{match}'")
179 if type(match) is not re.Match:
180 print(f"WARNING: version='{version}' does not match regex, leaving software='{software}' untouched.")
183 # DEBUG: print(f"DEBUG: Found valid version number: '{version}', removing it ...")
184 end = len(temp) - len(version) - 1
186 # DEBUG: print(f"DEBUG: end[{type(end)}]={end}")
187 software = temp[0:end].strip()
188 if " version" in software:
189 # DEBUG: print(f"DEBUG: software='{software}' contains word ' version'")
190 software = strip_until(software, " version")
192 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
195 def strip_powered_by(software: str) -> str:
196 # DEBUG: print(f"DEBUG: software='{software}' - CALLED!")
198 print(f"ERROR: Bad method call, 'software' is empty")
199 raise Exception("Parameter 'software' is empty")
200 elif not "powered by" in software:
201 print(f"WARNING: Cannot find 'powered by' in '{software}'!")
204 start = software.find("powered by ")
205 # DEBUG: print(f"DEBUG: start[{type(start)}]='{start}'")
207 software = software[start + 11:].strip()
208 # DEBUG: print(f"DEBUG: software='{software}'")
210 software = strip_until(software, " - ")
212 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
215 def strip_until(software: str, until: str) -> str:
216 # DEBUG: print(f"DEBUG: software='{software}',until='{until}' - CALLED!")
218 print(f"ERROR: Bad method call, 'software' is empty")
219 raise Exception("Parameter 'software' is empty")
221 print(f"ERROR: Bad method call, 'until' is empty")
222 raise Exception("Parameter 'until' is empty")
223 elif not until in software:
224 print(f"WARNING: Cannot find 'powered by' in '{software}'!")
227 # Next, strip until part
228 end = software.find(until)
230 # DEBUG: print(f"DEBUG: end[{type(end)}]='{end}'")
232 software = software[0:end].strip()
234 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
237 def is_blacklisted(domain: str) -> bool:
239 for peer in blacklist:
245 def remove_pending_error(domain: str):
247 # Prevent updating any pending errors, nodeinfo was found
248 del pending_errors[domain]
253 def get_hash(domain: str) -> str:
254 return hashlib.sha256(domain.encode("utf-8")).hexdigest()
256 def update_last_blocked(domain: str):
257 # DEBUG: print("DEBUG: Updating last_blocked for domain", domain)
259 cursor.execute("UPDATE instances SET last_blocked = ?, last_updated = ? WHERE domain = ? LIMIT 1", [
265 if cursor.rowcount == 0:
266 print("WARNING: Did not update any rows:", domain)
268 except BaseException as e:
269 print(f"ERROR: failed SQL query: domain='{domain}',exception:'{str(e)}'")
272 # DEBUG: print("DEBUG: EXIT!")
274 def update_nodeinfos(domain: str):
275 # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
278 for key in nodeinfos:
279 # DEBUG: print("DEBUG: key:", key)
280 if domain in nodeinfos[key]:
281 # DEBUG: print(f"DEBUG: Adding '{nodeinfos[key][domain]}' for key='{key}' ...")
282 fields.append(nodeinfos[key][domain])
283 sql_string += f" {key} = ?,"
285 fields.append(domain)
286 # DEBUG: print(f"DEBUG: sql_string='{sql_string}',fields()={len(fields)}")
288 sql = "UPDATE instances SET" + sql_string + " last_status_code = NULL, last_error_details = NULL WHERE domain = ? LIMIT 1"
289 # DEBUG: print("DEBUG: sql:", sql)
292 # DEBUG: print("DEBUG: Executing SQL:", sql)
293 cursor.execute(sql, fields)
294 # DEBUG: print(f"DEBUG: Success! (rowcount={cursor.rowcount })")
296 if cursor.rowcount == 0:
297 print("WARNING: Did not update any rows:", domain)
299 except BaseException as e:
300 print(f"ERROR: failed SQL query: domain='{domain}',sql='{sql}',exception:'{str(e)}'")
303 # DEBUG: print("DEBUG: Deleting nodeinfos for domain:", domain)
304 for key in nodeinfos:
306 # DEBUG: print("DEBUG: Deleting key:", key)
307 del nodeinfos[key][domain]
311 # DEBUG: print("DEBUG: EXIT!")
313 def update_last_error(domain: str, res: any):
314 # DEBUG: print("DEBUG: domain,res[]:", domain, type(res))
316 # DEBUG: print("DEBUG: BEFORE res[]:", type(res))
317 if isinstance(res, BaseException) or isinstance(res, json.JSONDecodeError):
320 # DEBUG: print("DEBUG: AFTER res[]:", type(res))
322 # DEBUG: print(f"DEBUG: Setting last_error_details='{res}'");
323 cursor.execute("UPDATE instances SET last_status_code = 999, last_error_details = ?, last_updated = ? WHERE domain = ? LIMIT 1", [
329 # DEBUG: print(f"DEBUG: Setting last_error_details='{res.reason}'");
330 cursor.execute("UPDATE instances SET last_status_code = ?, last_error_details = ?, last_updated = ? WHERE domain = ? LIMIT 1", [
337 if cursor.rowcount == 0:
338 # DEBUG: print("DEBUG: Did not update any rows:", domain)
339 pending_errors[domain] = res
341 except BaseException as e:
342 print(f"ERROR: failed SQL query: domain='{domain}',exception:'{str(e)}'")
345 # DEBUG: print("DEBUG: EXIT!")
347 def update_last_instance_fetch(domain: str):
348 #print("DEBUG: Updating last_instance_fetch for domain:", domain)
350 cursor.execute("UPDATE instances SET last_instance_fetch = ?, last_updated = ? WHERE domain = ? LIMIT 1", [
356 if cursor.rowcount == 0:
357 print("WARNING: Did not update any rows:", domain)
359 except BaseException as e:
360 print(f"ERROR: failed SQL query: domain='{domain}',exception:'{str(e)}'")
364 #print("DEBUG: EXIT!")
366 def update_last_nodeinfo(domain: str):
367 # DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
369 cursor.execute("UPDATE instances SET last_nodeinfo = ?, last_updated = ? WHERE domain = ? LIMIT 1", [
375 if cursor.rowcount == 0:
376 print("WARNING: Did not update any rows:", domain)
378 except BaseException as e:
379 print(f"ERROR: failed SQL query: domain='{domain}',exception:'{str(e)}'")
383 # DEBUG: print("DEBUG: EXIT!")
385 def get_peers(domain: str, software: str) -> list:
386 # DEBUG: print("DEBUG: Getting peers for domain:", domain, software)
389 if software == "misskey":
390 # DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
393 step = config["misskey_offset"]
394 # iterating through all "suspended" (follow-only in its terminology)
395 # instances page-by-page, since that troonware doesn't support
396 # sending them all at once
398 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
400 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
404 }), {"Origin": domain})
406 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
411 }), {"Origin": domain})
413 # DEBUG: print("DEBUG: fetched():", len(fetched))
414 if len(fetched) == 0:
415 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
417 elif len(fetched) != config["misskey_offset"]:
418 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
419 offset = offset + (config["misskey_offset"] - len(fetched))
421 # DEBUG: print("DEBUG: Raising offset by step:", step)
422 offset = offset + step
426 # DEBUG: print(f"DEBUG: row():{len(row)}")
427 if not "host" in row:
428 print(f"WARNING: row()={len(row)} does not contain element 'host': {row},domain='{domain}'")
430 elif "host" in row and is_blacklisted(row["host"]):
431 # DEBUG: print(f"DEBUG: row[host]='{row['host']}' is blacklisted. domain='{domain}'")
434 # DEBUG: print(f"DEBUG: Adding peer: '{row['host']}'")
435 peers.append(row["host"])
437 update_last_instance_fetch(domain)
439 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
441 elif software == "lemmy":
442 # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
444 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
446 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',res.json[]='{type(res.json())}'")
447 if not res.ok or res.status_code >= 400:
448 print("WARNING: Could not reach any JSON API:", domain)
449 update_last_error(domain, res)
450 elif res.ok and isinstance(res.json(), list):
451 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{res.json()}'")
453 elif "federated_instances" in res.json():
454 # DEBUG: print("DEBUG: Found federated_instances", domain)
455 peers = peers + add_peers(res.json()["federated_instances"])
457 print("WARNING: JSON response does not contain 'federated_instances':", domain)
458 update_last_error(domain, res)
460 except BaseException as e:
461 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception:'{str(e)}'")
463 update_last_instance_fetch(domain)
465 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
467 elif software == "peertube":
468 # DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
471 for mode in ["followers", "following"]:
472 # DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
475 res = reqto.get(f"https://{domain}/api/v1/server/{mode}?start={start}&count=100", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
477 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',res.json[]='{type(res.json())}'")
478 if res.ok and isinstance(res.json(), dict):
479 # DEBUG: print("DEBUG: Success, res.json():", len(res.json()))
483 # DEBUG: print(f"DEBUG: Found {len(data['data'])} record(s).")
484 for record in data["data"]:
485 # DEBUG: print(f"DEBUG: record()={len(record)}")
486 if mode in record and "host" in record[mode]:
487 # DEBUG: print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
488 peers.append(record[mode]["host"])
490 print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
492 if len(data["data"]) < 100:
493 # DEBUG: print("DEBUG: Reached end of JSON response:", domain)
496 # Continue with next row
499 except BaseException as e:
500 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception:'{str(e)}'")
502 update_last_instance_fetch(domain)
504 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
507 # DEBUG: print(f"DEBUG: Fetching get_peers_url='{get_peers_url}' from '{domain}' ...")
509 res = reqto.get(f"https://{domain}{get_peers_url}", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
511 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},res.json[]='{type(res.json())}'")
512 if not res.ok or res.status_code >= 400:
513 # DEBUG: print(f"DEBUG: Was not able to fetch '{get_peers_url}', trying alternative ...")
514 res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
516 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},res.json[]='{type(res.json())}'")
517 if not res.ok or res.status_code >= 400:
518 print("WARNING: Could not reach any JSON API:", domain)
519 update_last_error(domain, res)
520 elif res.ok and isinstance(res.json(), list):
521 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{res.json()}'")
523 elif "federated_instances" in res.json():
524 # DEBUG: print("DEBUG: Found federated_instances", domain)
525 peers = peers + add_peers(res.json()["federated_instances"])
527 print("WARNING: JSON response does not contain 'federated_instances':", domain)
528 update_last_error(domain, res)
530 # DEBUG: print("DEBUG: Querying API was successful:", domain, len(res.json()))
532 nodeinfos["get_peers_url"][domain] = get_peers_url
534 except BaseException as e:
535 print("WARNING: Some error during get():", domain, e)
536 update_last_error(domain, e)
538 update_last_instance_fetch(domain)
540 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
543 def post_json_api(domain: str, path: str, parameter: str, extra_headers: dict = {}) -> list:
544 # DEBUG: print("DEBUG: Sending POST to domain,path,parameter:", domain, path, parameter, extra_headers)
547 res = reqto.post(f"https://{domain}{path}", data=parameter, headers={**api_headers, **extra_headers}, timeout=(config["connection_timeout"], config["read_timeout"]))
549 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},res.json[]='{type(res.json())}'")
550 if not res.ok or res.status_code >= 400:
551 print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},res.status_code='{res.status_code}',res.json[]='{type(res.json())}'")
552 update_last_error(domain, res)
554 update_last_nodeinfo(domain)
557 except BaseException as e:
558 print(f"WARNING: Some error during post(): domain='{domain}',path='{path}',parameter()={len(parameter)},exception[{type(e)}]:'{str(e)}'")
560 # DEBUG: print("DEBUG: Returning data():", len(data))
563 def fetch_nodeinfo(domain: str, path: str = None) -> list:
564 # DEBUG: print("DEBUG: Fetching nodeinfo from domain,path:", domain, path)
566 nodeinfo = fetch_wellknown_nodeinfo(domain)
567 # DEBUG: print("DEBUG: nodeinfo:", len(nodeinfo))
569 if len(nodeinfo) > 0:
570 # DEBUG: print("DEBUG: Returning auto-discovered nodeinfo:", len(nodeinfo))
574 f"https://{domain}/nodeinfo/2.1.json",
575 f"https://{domain}/nodeinfo/2.1",
576 f"https://{domain}/nodeinfo/2.0.json",
577 f"https://{domain}/nodeinfo/2.0",
578 f"https://{domain}/nodeinfo/1.0",
579 f"https://{domain}/api/v1/instance"
583 for request in requests:
584 if path != None and path != "" and request != path:
585 print(f"DEBUG: path='{path}' does not match request='{request}' - SKIPPED!")
589 # DEBUG: print("DEBUG: Fetching request:", request)
590 res = reqto.get(request, headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
592 # DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},res.json[]='{type(res.json())}'")
593 if res.ok and isinstance(res.json(), dict):
594 # DEBUG: print("DEBUG: Success:", request)
596 nodeinfos["detection_mode"][domain] = "STATIC_CHECK"
597 nodeinfos["nodeinfo_url"][domain] = request
599 elif res.ok and isinstance(res.json(), list):
600 # DEBUG: print(f"DEBUG: domain='{domain}' returned a list: '{res.json()}'")
602 elif not res.ok or res.status_code >= 400:
603 print("WARNING: Failed fetching nodeinfo from domain:", domain)
604 update_last_error(domain, res)
607 except BaseException as e:
608 # DEBUG: print("DEBUG: Cannot fetch API request:", request)
609 update_last_error(domain, e)
612 # DEBUG: print("DEBUG: Returning data[]:", type(data))
615 def fetch_wellknown_nodeinfo(domain: str) -> list:
616 # DEBUG: print("DEBUG: Fetching .well-known info for domain:", domain)
620 res = reqto.get(f"https://{domain}/.well-known/nodeinfo", headers=api_headers, timeout=(config["connection_timeout"], config["read_timeout"]))
621 # DEBUG: print("DEBUG: domain,res.ok,res.json[]:", domain, res.ok, type(res.json()))
622 if res.ok and isinstance(res.json(), dict):
623 nodeinfo = res.json()
624 # DEBUG: print("DEBUG: Found entries:", len(nodeinfo), domain)
625 if "links" in nodeinfo:
626 # DEBUG: print("DEBUG: Found links in nodeinfo():", len(nodeinfo["links"]))
627 for link in nodeinfo["links"]:
628 # DEBUG: print("DEBUG: rel,href:", link["rel"], link["href"])
629 if link["rel"] in nodeinfo_identifier:
630 # DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
631 res = reqto.get(link["href"])
633 # DEBUG: print("DEBUG: href,res.ok,res.status_code:", link["href"], res.ok, res.status_code)
634 if res.ok and isinstance(res.json(), dict):
635 # DEBUG: print("DEBUG: Found JSON nodeinfo():", len(res.json()))
637 nodeinfos["detection_mode"][domain] = "AUTO_DISCOVERY"
638 nodeinfos["nodeinfo_url"][domain] = link["href"]
641 print("WARNING: Unknown 'rel' value:", domain, link["rel"])
643 print("WARNING: nodeinfo does not contain 'links':", domain)
645 except BaseException as e:
646 print("WARNING: Failed fetching .well-known info:", domain)
647 update_last_error(domain, e)
650 # DEBUG: print("DEBUG: Returning data[]:", type(data))
653 def fetch_generator_from_path(domain: str, path: str = "/") -> str:
654 # DEBUG: print(f"DEBUG: domain='{domain}',path='{path}' - CALLED!")
658 # DEBUG: print(f"DEBUG: Fetching path='{path}' from '{domain}' ...")
659 res = reqto.get(f"https://{domain}{path}", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
661 # DEBUG: print("DEBUG: domain,res.ok,res.status_code,res.text[]:", domain, res.ok, res.status_code, type(res.text))
662 if res.ok and res.status_code < 300 and len(res.text) > 0:
663 # DEBUG: print("DEBUG: Search for <meta name='generator'>:", domain)
664 doc = bs4.BeautifulSoup(res.text, "html.parser")
666 # DEBUG: print("DEBUG: doc[]:", type(doc))
667 tag = doc.find("meta", {"name": "generator"})
669 # DEBUG: print(f"DEBUG: tag[{type(tag)}: {tag}")
670 if isinstance(tag, bs4.element.Tag):
671 # DEBUG: print("DEBUG: Found generator meta tag: ", domain)
672 software = tidyup(tag.get("content"))
673 print(f"INFO: domain='{domain}' is generated by '{software}'")
674 nodeinfos["detection_mode"][domain] = "GENERATOR"
675 remove_pending_error(domain)
677 except BaseException as e:
678 # DEBUG: print(f"DEBUG: Cannot fetch / from '{domain}':", e)
679 update_last_error(domain, e)
682 # DEBUG: print(f"DEBUG: software[]={type(software)}")
683 if type(software) is str and software == "":
684 # DEBUG: print(f"DEBUG: Corrected empty string to None for software of domain='{domain}'")
686 elif type(software) is str and ("." in software or " " in software):
687 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
688 software = remove_version(software)
690 # DEBUG: print(f"DEBUG: software[]={type(software)}")
691 if type(software) is str and "powered by" in software:
692 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
693 software = remove_version(strip_powered_by(software))
694 elif type(software) is str and " by " in software:
695 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
696 software = strip_until(software, " by ")
697 elif type(software) is str and " see " in software:
698 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
699 software = strip_until(software, " see ")
701 # DEBUG: print(f"DEBUG: software='{software}' - EXIT!")
704 def determine_software(domain: str, path: str = None) -> str:
705 # DEBUG: print("DEBUG: Determining software for domain,path:", domain, path)
708 # DEBUG: print(f"DEBUG: Fetching nodeinfo from '{domain}' ...")
709 data = fetch_nodeinfo(domain, path)
711 # DEBUG: print("DEBUG: data[]:", type(data))
712 if not isinstance(data, dict) or len(data) == 0:
713 # DEBUG: print("DEBUG: Could not determine software type:", domain)
714 return fetch_generator_from_path(domain)
716 # DEBUG: print("DEBUG: data():", len(data), data)
717 if "status" in data and data["status"] == "error" and "message" in data:
718 print("WARNING: JSON response is an error:", data["message"])
719 update_last_error(domain, data["message"])
720 return fetch_generator_from_path(domain)
721 elif "software" not in data or "name" not in data["software"]:
722 # DEBUG: print(f"DEBUG: JSON response from {domain} does not include [software][name], fetching / ...")
723 software = fetch_generator_from_path(domain)
725 # DEBUG: print(f"DEBUG: Generator for domain='{domain}' is: {software}, EXIT!")
728 software = tidyup(data["software"]["name"])
730 # DEBUG: print("DEBUG: sofware after tidyup():", software)
731 if software in ["akkoma", "rebased"]:
732 # DEBUG: print("DEBUG: Setting pleroma:", domain, software)
734 elif software in ["hometown", "ecko"]:
735 # DEBUG: print("DEBUG: Setting mastodon:", domain, software)
736 software = "mastodon"
737 elif software in ["calckey", "groundpolis", "foundkey", "cherrypick", "meisskey"]:
738 # DEBUG: print("DEBUG: Setting misskey:", domain, software)
740 elif software.find("/") > 0:
741 print("WARNING: Spliting of slash:", software)
742 software = software.split("/")[-1];
743 elif software.find("|") > 0:
744 print("WARNING: Spliting of pipe:", software)
745 software = tidyup(software.split("|")[0]);
746 elif "powered by" in software:
747 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
748 software = strip_powered_by(software)
749 elif type(software) is str and " by " in software:
750 # DEBUG: print(f"DEBUG: software='{software}' has ' by ' in it")
751 software = strip_until(software, " by ")
752 elif type(software) is str and " see " in software:
753 # DEBUG: print(f"DEBUG: software='{software}' has ' see ' in it")
754 software = strip_until(software, " see ")
756 # DEBUG: print(f"DEBUG: software[]={type(software)}")
758 print("WARNING: tidyup() left no software name behind:", domain)
761 # DEBUG: print(f"DEBUG: software[]={type(software)}")
762 if str(software) == "":
763 # DEBUG: print(f"DEBUG: software for '{domain}' was not detected, trying generator ...")
764 software = fetch_generator_from_path(domain)
765 elif len(str(software)) > 0 and ("." in software or " " in software):
766 # DEBUG: print(f"DEBUG: software='{software}' may contain a version number, domain='{domain}', removing it ...")
767 software = remove_version(software)
769 # DEBUG: print(f"DEBUG: software[]={type(software)}")
770 if type(software) is str and "powered by" in software:
771 # DEBUG: print(f"DEBUG: software='{software}' has 'powered by' in it")
772 software = remove_version(strip_powered_by(software))
774 # DEBUG: print("DEBUG: Returning domain,software:", domain, software)
777 def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
778 # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
781 "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
791 # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
792 if cursor.rowcount == 0:
793 print("WARNING: Did not update any rows:", domain)
795 except BaseException as e:
796 print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',sql='{sql}',exception:'{str(e)}'")
799 # DEBUG: print("DEBUG: EXIT!")
801 def update_last_seen(blocker: str, blocked: str, block_level: str):
802 # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
805 "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
814 if cursor.rowcount == 0:
815 print("WARNING: Did not update any rows:", domain)
817 except BaseException as e:
818 print(f"ERROR: failed SQL query: last_seen='{last_seen}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception:'{str(e)}'")
821 # DEBUG: print("DEBUG: EXIT!")
823 def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
824 # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
825 if not validators.domain(blocker.split("/")[0]):
826 print("WARNING: Bad blocker:", blocker)
828 elif not validators.domain(blocked.split("/")[0]):
829 print("WARNING: Bad blocked:", blocked)
832 print("INFO: New block:", blocker, blocked, reason, block_level, first_seen, last_seen)
835 "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
846 except BaseException as e:
847 print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',first_seen='{first_seen}',last_seen='{last_seen}',exception:'{str(e)}'")
850 # DEBUG: print("DEBUG: EXIT!")
852 def is_instance_registered(domain: str) -> bool:
853 # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
854 # Default is not registered
859 "SELECT rowid FROM instances WHERE domain = ? LIMIT 1", [domain]
863 registered = cursor.fetchone() != None
864 except BaseException as e:
865 print(f"ERROR: failed SQL query: last_seen='{last_seen}'blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',first_seen='{first_seen}',last_seen='{last_seen}',exception:'{str(e)}'")
868 # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
871 def add_instance(domain: str, origin: str, originator: str, path: str = None):
872 # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
873 if not validators.domain(domain.split("/")[0]):
874 print("WARNING: Bad domain name:", domain)
876 elif origin is not None and not validators.domain(origin.split("/")[0]):
877 print("WARNING: Bad origin name:", origin)
880 software = determine_software(domain, path)
881 # DEBUG: print("DEBUG: Determined software:", software)
883 print(f"INFO: Adding instance {domain} (origin: {origin})")
886 "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
897 for key in nodeinfos:
898 # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',nodeinfos[key]={nodeinfos[key]}")
899 if domain in nodeinfos[key]:
900 # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
901 update_nodeinfos(domain)
902 remove_pending_error(domain)
905 if domain in pending_errors:
906 # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
907 update_last_error(domain, pending_errors[domain])
908 remove_pending_error(domain)
910 except BaseException as e:
911 print(f"ERROR: failed SQL query: domain='{domain}',exception:'{str(e)}'")
914 # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
915 update_last_nodeinfo(domain)
917 # DEBUG: print("DEBUG: EXIT!")
919 def send_bot_post(instance: str, blocks: dict):
920 message = instance + " has blocked the following instances:\n\n"
925 blocks = blocks[0 : 19]
928 if block["reason"] == None or block["reason"] == '':
929 message = message + block["blocked"] + " with unspecified reason\n"
931 if len(block["reason"]) > 420:
932 block["reason"] = block["reason"][0:419] + "[…]"
934 message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
937 message = message + "(the list has been truncated to the first 20 entries)"
939 botheaders = {**api_headers, **{"Authorization": "Bearer " + config["bot_token"]}}
942 f"{config['bot_instance']}/api/v1/statuses",
945 "visibility" : config['bot_visibility'],
946 "content_type": "text/plain"
954 def get_mastodon_blocks(domain: str) -> dict:
955 # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
957 "Suspended servers": [],
958 "Filtered media" : [],
959 "Limited servers" : [],
960 "Silenced servers" : [],
964 doc = bs4.BeautifulSoup(
965 reqto.get(f"https://{domain}/about/more", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
968 except BaseException as e:
969 print("ERROR: Cannot fetch from domain:", domain, e)
970 update_last_error(domain, e)
973 for header in doc.find_all("h3"):
974 header_text = tidyup(header.text)
976 if header_text in language_mapping:
977 # DEBUG: print(f"DEBUG: header_text='{header_text}'")
978 header_text = language_mapping[header_text]
980 if header_text in blocks or header_text.lower() in blocks:
981 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
982 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
983 blocks[header_text].append(
985 "domain": tidyup(line.find("span").text),
986 "hash" : tidyup(line.find("span")["title"][9:]),
987 "reason": tidyup(line.find_all("td")[1].text),
991 # DEBUG: print("DEBUG: Returning blocks for domain:", domain)
993 "reject" : blocks["Suspended servers"],
994 "media_removal" : blocks["Filtered media"],
995 "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
998 def get_friendica_blocks(domain: str) -> dict:
999 # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
1003 doc = bs4.BeautifulSoup(
1004 reqto.get(f"https://{domain}/friendica", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
1007 except BaseException as e:
1008 print("WARNING: Failed to fetch /friendica from domain:", domain, e)
1009 update_last_error(domain, e)
1012 blocklist = doc.find(id="about_blocklist")
1014 # Prevents exceptions:
1015 if blocklist is None:
1016 # DEBUG: print("DEBUG:Instance has no block list:", domain)
1019 for line in blocklist.find("table").find_all("tr")[1:]:
1020 # DEBUG: print(f"DEBUG: line='{line}'")
1022 "domain": tidyup(line.find_all("td")[0].text),
1023 "reason": tidyup(line.find_all("td")[1].text)
1026 # DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
1031 def get_misskey_blocks(domain: str) -> dict:
1032 # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
1039 step = config["misskey_offset"]
1041 # iterating through all "suspended" (follow-only in its terminology)
1042 # instances page-by-page, since that troonware doesn't support
1043 # sending them all at once
1045 # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
1047 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1048 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1053 }), {"Origin": domain})
1055 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1056 fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
1061 "offset" : offset - 1
1062 }), {"Origin": domain})
1064 # DEBUG: print("DEBUG: fetched():", len(fetched))
1065 if len(fetched) == 0:
1066 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1068 elif len(fetched) != config["misskey_offset"]:
1069 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1070 offset = offset + (config["misskey_offset"] - len(fetched))
1072 # DEBUG: print("DEBUG: Raising offset by step:", step)
1073 offset = offset + step
1075 for instance in fetched:
1077 if instance["isSuspended"]:
1078 blocks["suspended"].append(
1080 "domain": tidyup(instance["host"]),
1081 # no reason field, nothing
1086 except BaseException as e:
1087 print("WARNING: Caught error, exiting loop:", domain, e)
1088 update_last_error(domain, e)
1093 # same shit, different asshole ("blocked" aka full suspend)
1096 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1097 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1102 }), {"Origin": domain})
1104 # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
1105 fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
1111 }), {"Origin": domain})
1113 # DEBUG: print("DEBUG: fetched():", len(fetched))
1114 if len(fetched) == 0:
1115 # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
1117 elif len(fetched) != config["misskey_offset"]:
1118 # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
1119 offset = offset + (config["misskey_offset"] - len(fetched))
1121 # DEBUG: print("DEBUG: Raising offset by step:", step)
1122 offset = offset + step
1124 for instance in fetched:
1125 if instance["isBlocked"]:
1126 blocks["blocked"].append({
1127 "domain": tidyup(instance["host"]),
1131 except BaseException as e:
1132 print("ERROR: Exception during POST:", domain, e)
1133 update_last_error(domain, e)
1137 # DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
1139 "reject" : blocks["blocked"],
1140 "followers_only": blocks["suspended"]
1143 def tidyup(string: str) -> str:
1144 # some retards put their blocks in variable case
1145 string = string.lower().strip()
1147 # other retards put the port
1148 string = re.sub("\:\d+$", "", string)
1150 # bigger retards put the schema in their blocklist, sometimes even without slashes
1151 string = re.sub("^https?\:(\/*)", "", string)
1153 # and trailing slash
1154 string = re.sub("\/$", "", string)
1157 string = re.sub("^\@", "", string)
1159 # the biggest retards of them all try to block individual users
1160 string = re.sub("(.+)\@", "", string)