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/>.
20 from fba import blacklist
21 from fba import blocks
22 from fba import config
27 "Silenced instances" : "Silenced servers",
28 "Suspended instances" : "Suspended servers",
29 "Limited instances" : "Limited servers",
30 "Filtered media" : "Filtered media",
31 # Mappuing German -> English
32 "Gesperrte Server" : "Suspended servers",
33 "Gefilterte Medien" : "Filtered media",
34 "Stummgeschaltete Server" : "Silenced servers",
36 "停止済みのサーバー" : "Suspended servers",
37 "制限中のサーバー" : "Limited servers",
38 "メディアを拒否しているサーバー": "Filtered media",
39 "サイレンス済みのサーバー" : "Silenced servers",
41 "שרתים מושעים" : "Suspended servers",
42 "מדיה מסוננת" : "Filtered media",
43 "שרתים מוגבלים" : "Silenced servers",
45 "Serveurs suspendus" : "Suspended servers",
46 "Médias filtrés" : "Filtered media",
47 "Serveurs limités" : "Limited servers",
48 "Serveurs modérés" : "Limited servers",
51 def fetch_blocks_from_about(domain: str) -> dict:
52 # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
53 if type(domain) != str:
54 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
56 raise ValueError(f"Parameter 'domain' is empty")
58 # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
60 "Suspended servers": [],
61 "Filtered media" : [],
62 "Limited servers" : [],
63 "Silenced servers" : [],
67 doc = bs4.BeautifulSoup(
68 fba.get_response(domain, "/about/more", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
71 except BaseException as e:
72 print("ERROR: Cannot fetch from domain:", domain, e)
73 fba.update_last_error(domain, e)
76 for header in doc.find_all("h3"):
77 header_text = fba.tidyup_reason(header.text)
79 # DEBUG: print(f"DEBUG: header_text='{header_text}'")
80 if header_text in language_mapping:
81 # DEBUG: print(f"DEBUG: header_text='{header_text}'")
82 header_text = language_mapping[header_text]
84 print(f"WARNING: header_text='{header_text}' not found in language mapping table")
86 if header_text in blocklist or header_text.lower() in blocklist:
87 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
88 for line in header.find_all_next("table")[0].find_all("tr")[1:]:
89 blocklist[header_text].append(
91 "domain": fba.tidyup_domain(line.find("span").text),
92 "hash" : fba.tidyup_domain(line.find("span")["title"][9:]),
93 "reason": fba.tidyup_domain(line.find_all("td")[1].text),
97 print(f"WARNING: header_text='{header_text}' not found in blocklist()={len(blocklist)}")
99 # DEBUG: print("DEBUG: Returning blocklist for domain:", domain)
101 "reject" : blocklist["Suspended servers"],
102 "media_removal" : blocklist["Filtered media"],
103 "followers_only": blocklist["Limited servers"] + blocklist["Silenced servers"],
106 def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
107 print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
108 if type(domain) != str:
109 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
111 raise ValueError(f"Parameter 'domain' is empty")
112 elif type(origin) != str and origin != None:
113 raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
115 raise ValueError(f"Parameter 'origin' is empty")
116 elif type(nodeinfo_url) != str:
117 raise ValueError(f"Parameter nodeinfo_url[]={type(nodeinfo_url)} is not 'str'")
118 elif nodeinfo_url == "":
119 raise ValueError(f"Parameter 'nodeinfo_url' is empty")
122 # json endpoint for newer mastodongs
127 "media_removal" : [],
128 "followers_only": [],
132 # handling CSRF, I've saw at least one server requiring it to access the endpoint
133 # DEBUG: print("DEBUG: Fetching meta:", domain)
134 meta = bs4.BeautifulSoup(
135 fba.get_response(domain, "/", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
139 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
140 # DEBUG: print("DEBUG: Adding CSRF token:", domain, csrf)
141 reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
142 except BaseException as e:
143 # DEBUG: print("DEBUG: No CSRF token found, using normal headers:", domain, e)
144 reqheaders = fba.api_headers
146 # DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
147 blocklist = fba.get_response(domain, "/api/v1/instance/domain_blocks", reqheaders, (config.get("connection_timeout"), config.get("read_timeout"))).json()
149 print(f"INFO: Checking {len(blocklist)} entries from domain='{domain}',software='mastodon' ...")
150 for block in blocklist:
152 'domain': block['domain'],
153 'hash' : block['digest'],
154 'reason': block['comment']
157 # DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
158 if block['severity'] == 'suspend':
159 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
160 json['reject'].append(entry)
161 elif block['severity'] == 'silence':
162 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
163 json['followers_only'].append(entry)
164 elif block['severity'] == 'reject_media':
165 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
166 json['media_removal'].append(entry)
167 elif block['severity'] == 'reject_reports':
168 # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
169 json['report_removal'].append(entry)
171 print("WARNING: Unknown severity:", block['severity'], block['domain'])
173 except BaseException as e:
174 # DEBUG: print(f"DEBUG: Failed, trying mastodon-specific fetches: domain='{domain}',exception[{type(e)}]={str(e)}")
175 json = fetch_blocks_from_about(domain)
177 print(f"INFO: Checking {len(json.items())} entries from domain='{domain}',software='mastodon' ...")
178 for block_level, blocklist in json.items():
179 # DEBUG: print("DEBUG: domain,block_level,blocklist():", domain, block_level, len(blocklist))
180 block_level = fba.tidyup_domain(block_level)
182 # DEBUG: print("DEBUG: AFTER-block_level:", block_level)
183 if block_level == "":
184 print("WARNING: block_level is empty, domain:", domain)
187 # DEBUG: print(f"DEBUG: Checking {len(blocklist)} entries from domain='{domain}',software='mastodon',block_level='{block_level}' ...")
188 for block in blocklist:
189 blocked, blocked_hash, reason = block.values()
190 # DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
191 blocked = fba.tidyup_domain(blocked)
192 # DEBUG: print("DEBUG: AFTER-blocked:", blocked)
195 print("WARNING: blocked is empty:", domain)
197 elif blacklist.is_blacklisted(blocked):
198 # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
200 elif blocked.count("*") > 0:
201 # Doing the hash search for instance names as well to tidy up DB
203 "SELECT domain, origin, nodeinfo_url FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
205 searchres = fba.cursor.fetchone()
207 if searchres == None:
208 print(f"WARNING: Cannot deobsfucate blocked='{blocked}',blocked_hash='{blocked_hash}' - SKIPPED!")
211 # DEBUG: print("DEBUG: Updating domain: ", searchres[0])
212 blocked = searchres[0]
213 origin = searchres[1]
214 nodeinfo_url = searchres[2]
216 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
217 if not validators.domain(blocked):
218 print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
220 elif not fba.is_instance_registered(blocked):
221 # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
222 fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
223 elif not validators.domain(blocked):
224 print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
227 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
228 if not validators.domain(blocked):
229 print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
231 elif not fba.is_instance_registered(blocked):
232 # DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, domain)
233 fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
235 blocking = blocked if blocked.count("*") <= 1 else blocked_hash
236 # DEBUG: print(f"DEBUG: blocking='{blocking}',blocked='{blocked}',blocked_hash='{blocked_hash}'")
238 if not blocks.is_instance_blocked(domain, blocked, block_level):
239 # DEBUG: print("DEBUG: Blocking:", domain, blocked, block_level)
240 blocks.add_instance(domain, blocking, reason, block_level)
242 if block_level == "reject":
248 # DEBUG: print(f"DEBUG: Updating block last seen and reason for domain='{domain}',blocking='{blocking}' ...")
249 blocks.update_last_seen(domain, blocking, block_level)
250 blocks.update_reason(reason, domain, blocking, block_level)
252 # DEBUG: print("DEBUG: Committing changes ...")
253 fba.connection.commit()
254 except Exception as e:
255 print(f"ERROR: domain='{domain}',software='mastodon',exception[{type(e)}]:'{str(e)}'")
257 # DEBUG: print("DEBUG: EXIT!")