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/>.
22 from fba import database
25 from fba.helpers import blacklist
26 from fba.helpers import config
27 from fba.helpers import domain as domain_helper
28 from fba.helpers import tidyup
30 from fba.http import network
31 from fba.http import nodeinfo
33 from fba.models import blocks
34 from fba.models import instances
36 logging.basicConfig(level=logging.INFO)
37 logger = logging.getLogger(__name__)
39 # Language mapping X -> English
42 "filtered media" : "filtered_media",
43 "limited servers" : "followers_only",
44 "followers-only" : "followers_only",
45 "media removal" : "media_removal",
46 "media_removal" : "media_removal",
47 "media force-set as sensitive": "media_nsfw",
48 "nsfw" : "media_nsfw",
50 "suspended servers": "reject",
51 "silenced servers" : "silenced",
52 "removal from \"the whole known network\" timeline": "federated_timeline_removal",
55 def fetch_blocks(domain: str) -> list:
56 logger.debug("domain='%s' - CALLED!", domain)
57 domain_helper.raise_on(domain)
59 if blacklist.is_blacklisted(domain):
60 raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
61 elif not instances.is_registered(domain):
62 raise Exception(f"domain='{domain}' is not registered but function is invoked.")
68 logger.debug("Fetching nodeinfo: domain='%s'", domain)
69 rows = nodeinfo.fetch(domain, update_mode=False)
71 if "error_message" in rows:
72 logger.warning("Error message '%s' during fetching nodeinfo for domain='%s'", rows["error_message"], domain)
73 instances.set_last_error(domain, rows)
74 instances.update(domain)
76 logger.debug("Returning empty list ... - EXIT!")
78 elif "exception" in rows:
79 logger.warning("Exception '%s' during fetching nodeinfo for domain='%s' - EXIT!", type(rows["exception"]), domain)
82 logger.debug("rows[json] found for domain='%s'", domain)
85 except network.exceptions as exception:
86 logger.warning("Exception '%s' during fetching nodeinfo from domain='%s'", type(exception), domain)
87 instances.set_last_error(domain, exception)
90 logger.warning("Could not fetch nodeinfo from domain='%s' - EXIT!", domain)
92 elif "metadata" not in rows:
93 logger.warning("rows()=%d does not have key 'metadata', domain='%s' - EXIT!", len(rows), domain)
95 elif "federation" not in rows["metadata"]:
96 logger.warning("rows()=%d does not have key 'federation', domain='%s' - EXIT!", len(rows["metadata"]), domain)
99 data = rows["metadata"]["federation"]
102 logger.debug("data[]='%s'", type(data))
103 if "mrf_simple" in data:
104 logger.debug("Found mrf_simple in API response from domain='%s'", domain)
106 for block_level, blocklist in (
108 **data["mrf_simple"],
110 "quarantined_instances": data["quarantined_instances"]
114 logger.debug("block_level='%s', blocklist()=%d", block_level, len(blocklist))
115 block_level = tidyup.domain(block_level) if block_level != "" else None
116 logger.debug("block_level='%s' - AFTER!", block_level)
118 if block_level == "":
119 logger.warning("block_level is now empty!")
121 elif block_level == "accept":
122 logger.debug("domain='%s' skipping block_level='accept'", domain)
125 block_level = blocks.alias_block_level(block_level)
127 logger.debug("Checking %d entries from domain='%s',block_level='%s' ...", len(blocklist), domain, block_level)
128 for blocked in blocklist:
129 logger.debug("blocked='%s' - BEFORE!", blocked)
130 blocked = tidyup.domain(blocked) if blocked != "" else None
131 logger.debug("blocked='%s' - AFTER!", blocked)
133 if blocked in [None, ""]:
134 logger.warning("blocked='%s' is empty after tidyup.domain(): domain='%s',block_level='%s' - SKIPPED!", blocked, domain, block_level)
136 elif validators.domain(blocked) and blacklist.is_blacklisted(blocked):
137 logger.debug("blocked='%s' is blacklisted - SKIPPED!")
140 logger.debug("Invoking utils.deobfuscate(%s, %s) ...", blocked, domain)
141 blocked = utils.deobfuscate(blocked, domain)
142 logger.debug("blocked='%s' - DEOBFUSCATED!", blocked)
144 if blocked in [None, ""]:
145 logger.warning("instance[host]='%s' is None or empty after tidyup.domain() - SKIPPED!", instance["host"])
147 elif not domain_helper.is_wanted(blocked):
148 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
151 logger.debug("Appending blocker='%s',blocked='%s',block_level='%s' ...", domain, blocked, block_level)
156 "block_level": block_level,
159 elif "quarantined_instances" in data:
160 logger.debug("Found 'quarantined_instances' in JSON response: domain='%s'", domain)
162 block_level = "quarantined"
164 logger.debug("Checking %d quarantined instance(s) ...", len(data["quarantined_instances"]))
165 for blocked in data["quarantined_instances"]:
166 logger.debug("blocked='%s' - BEFORE!", blocked)
167 blocked = tidyup.domain(blocked) if blocked != "" else None
169 logger.debug("blocked='%s' - AFTER!", blocked)
170 if blocked in [None, ""]:
171 logger.warning("blocked is empty after tidyup.domain(): domain='%s',block_level='%s'", domain, block_level)
173 elif not domain_helper.is_wanted(blocked):
174 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
177 logger.debug("Appending blocker='%s',blocked='%s',block_level='%s' ...", domain, blocked, block_level)
182 "block_level": block_level,
186 logger.warning("Cannot find 'mrf_simple' or 'quarantined_instances' in JSON reply: domain='%s'", domain)
188 logger.debug("Invoking commit() ...")
189 database.connection.commit()
192 if "mrf_simple_info" in data:
193 logger.debug("Found mrf_simple_info in API response: domain='%s'", domain)
195 for block_level, info in (
197 **data["mrf_simple_info"],
198 **(data["quarantined_instances_info"] if "quarantined_instances_info" in data else {})
201 logger.debug("block_level='%s', info.items()=%d", block_level, len(info.items()))
202 block_level = tidyup.domain(block_level) if block_level != "" else None
203 logger.debug("block_level='%s' - AFTER!", block_level)
205 if block_level in [None, ""]:
206 logger.warning("block_level='%s' is now empty!", block_level)
208 elif block_level == "accept":
209 logger.debug("domain='%s': Skipping block_level='%s' ...", domain, block_level)
212 block_level = blocks.alias_block_level(block_level)
214 logger.debug("Checking %d entries from domain='%s',block_level='%s' ...", len(info.items()), domain, block_level)
215 for blocked, reason in info.items():
216 logger.debug("blocked='%s',reason[%s]='%s' - BEFORE!", blocked, type(reason), reason)
217 blocked = tidyup.domain(blocked) if blocked != "" else None
218 logger.debug("blocked='%s' - AFTER!", blocked)
220 if isinstance(reason, str):
221 logger.debug("reason[] is a string")
222 reason = tidyup.reason(reason)
223 elif isinstance(reason, dict) and "reason" in reason:
224 logger.debug("reason[] is a dict")
225 reason = tidyup.reason(reason["reason"]) if isinstance(reason["reason"], str) else None
226 elif reason is not None:
227 raise ValueError(f"Cannot handle reason[]='{type(reason)}'")
229 logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason)
232 logger.warning("blocked is empty after tidyup.domain(): domain='%s',block_level='%s'", domain, block_level)
235 logger.debug("Checking %d blockdict records ...", len(blockdict))
236 for block in blockdict:
237 logger.debug("block[blocked]='%s',blocked='%s'", block["blocked"], blocked)
238 if block["blocked"] == blocked:
239 logger.debug("Updating reason='%s' for blocker='%s'", reason, block["blocked"])
240 block["reason"] = reason
242 elif "quarantined_instances_info" in data and "quarantined_instances" in data["quarantined_instances_info"]:
243 logger.debug("Found 'quarantined_instances_info' in JSON response: domain='%s'", domain)
245 block_level = "quarantined"
247 #print(data["quarantined_instances_info"])
248 rows = data["quarantined_instances_info"]["quarantined_instances"]
250 logger.debug("blocked='%s' - BEFORE!", blocked)
251 reason = tidyup.reason(rows[blocked]["reason"])
252 blocked = tidyup.domain(blocked) if blocked != "" else None
253 logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason)
255 if blocked not in rows or "reason" not in rows[blocked]:
256 logger.warning("Cannot find blocked='%s' in rows()=%d,domain='%s' - BREAK!", blocked, len(rows), domain)
259 logger.warning("blocked is empty after tidyup.domain(): domain='%s',block_level='%s'", domain, block_level)
262 logger.debug("Checking %d blockdict record(s) ...", len(blockdict))
263 for block in blockdict:
264 logger.debug("block[blocked]='%s',blocked='%s'", block["blocked"], blocked)
265 if block["blocked"] == blocked:
266 logger.debug("Updating reason='%s' for blocker='%s'", reason, block["blocked"])
267 block["reason"] = reason
269 logger.warning("Cannot find 'mrf_simple_info' or 'quarantined_instances_info' in JSON reply: domain='%s'", domain)
272 logger.debug("Did not find any useable JSON elements, domain='%s', continuing with /about page ...", domain)
273 blocklist = fetch_blocks_from_about(domain)
275 logger.debug("blocklist()=%d", len(blocklist))
276 if len(blocklist) > 0:
277 logger.info("Checking %d different blocklist(s) ...", len(blocklist))
278 for block_level in blocklist:
279 logger.debug("block_level='%s'", block_level)
280 rows = blocklist[block_level]
282 logger.debug("rows[%s]()=%d'", type(rows), len(rows))
284 logger.debug("Appending blocker='%s',block[blocked]='%s',block[reason]='%s',block_level='%s' ...",domain, block["blocked"], block["reason"], block_level)
287 "blocked" : block["blocked"],
288 "reason" : block["reason"],
289 "block_level": block_level,
292 logger.debug("blockdict()=%d - EXIT!", len(blockdict))
295 def fetch_blocks_from_about(domain: str) -> dict:
296 logger.debug("domain='%s' - CALLED!", domain)
297 domain_helper.raise_on(domain)
299 if blacklist.is_blacklisted(domain):
300 raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
301 elif not instances.is_registered(domain):
302 raise Exception(f"domain='{domain}' is not registered but function is invoked.")
304 logger.debug("Fetching mastodon blocks from domain='%s'", domain)
306 for path in ["/instance/about/index.html"]:
311 logger.debug("Fetching path='%s' from domain='%s' ...", path, domain)
312 response = network.fetch_response(
316 (config.get("connection_timeout"), config.get("read_timeout"))
319 logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text))
320 if not response.ok or response.text.strip() == "":
321 logger.warning("path='%s' does not exist on domain='%s' - SKIPPED!", path, domain)
324 logger.debug("Parsing response.text()=%d Bytes ...", len(response.text))
325 doc = bs4.BeautifulSoup(
330 logger.debug("doc[]='%s'", type(doc))
331 if doc.find("h2") is not None:
332 logger.debug("Found 'h2' header in path='%s' - BREAK!", path)
335 except network.exceptions as exception:
336 logger.warning("Cannot fetch from domain='%s',exception[%s]='%s'", domain, type(exception), str(exception))
337 instances.set_last_error(domain, exception)
342 "filtered_media": [],
343 "followers_only": [],
346 "media_removal" : [],
347 "federated_timeline_removal": [],
350 logger.debug("doc[]='%s'", type(doc))
352 logger.warning("Cannot fetch any /about pages for domain='%s' - EXIT!", domain)
355 headers = doc.find_all("h2")
357 logger.debug("headers[]='%s'", type(headers))
359 logger.warning("Cannot fetch any /about pages for domain='%s' - EXIT!", domain)
362 logger.info("Checking %d headers ...", len(headers))
363 for header in headers:
364 logger.debug("header[%s]='%s'", type(header), header)
365 block_level = tidyup.reason(header.text).lower()
367 logger.debug("block_level='%s' - BEFORE!", block_level)
368 if block_level in language_mapping:
369 logger.debug("block_level='%s' - FOUND!", block_level)
370 block_level = language_mapping[block_level].lower()
372 logger.warning("block_level='%s' not found in language mapping table", block_level)
374 logger.debug("block_level='%s - AFTER!'", block_level)
375 if block_level in blocklist:
376 # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
377 logger.debug("Found block_level='%s', importing domain blocks ...", block_level)
378 for line in header.find_next("table").find_all("tr")[1:]:
379 logger.debug("line[]='%s'", type(line))
380 blocked = line.find_all("td")[0].text
381 logger.debug("blocked='%s'", blocked)
383 blocked = tidyup.domain(blocked) if blocked != "" else None
384 reason = tidyup.reason(line.find_all("td")[1].text)
385 logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason)
387 if blocked is None or blocked == "":
388 logger.debug("domain='%s',block_level='%s': blocked is empty - SKIPPED!", domain, block_level)
390 elif not domain_helper.is_wanted(blocked):
391 logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
394 logger.debug("Appending block_level='%s',blocked='%s',reason='%s' ...", block_level, blocked, reason)
395 blocklist[block_level].append({
400 logger.warning("block_level='%s' not found in blocklist()=%d", block_level, len(blocklist))
402 logger.debug("Returning blocklist for domain='%s' - EXIT!", domain)