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 config
21 #from fba import instances
22 from fba import network
24 def determine(domain: str, headers: dict) -> dict:
25 # DEBUG: print(f"DEBUG: domain='{domain}',headers()={len(headers)} - CALLED!")
26 if not isinstance(domain, str):
27 raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
29 raise ValueError("Parameter 'domain' is empty")
30 elif not isinstance(headers, dict):
31 raise ValueError(f"Parameter headers[]='{type(headers)}' is not 'dict'")
33 # Default headers with no CSRF
37 # Fetch / to check for meta tag indicating csrf
38 # DEBUG: print(f"DEBUG: Fetching / from domain='{domain}' for CSRF check ...")
41 headers=network.web_headers,
42 timeout=(config.get("connection_timeout"), config.get("read_timeout"))
45 # DEBUG: print(f"DEBUG: response.ok='{response.ok}',response.status_code={response.status_code},response.text()={len(response.text)}")
46 if response.ok and len(response.text) > 0:
47 meta = bs4.BeautifulSoup(
51 # DEBUG: print(f"DEBUG: meta[]='{type(meta)}'")
53 tag = meta.find("meta", attrs={"name": "csrf-token"})
54 # DEBUG: print(f"DEBUG: tag={tag}")
57 # DEBUG: print(f"DEBUG: Adding CSRF token='{csrf}' for domain='{domain}'")
59 reqheaders = {**headers, **{"X-CSRF-Token": csrf}}
61 except BaseException as exception:
62 # DEBUG: print(f"DEBUG: No CSRF token found, using normal headers: domain='{domain}',exception[{type(exception)}]={exception}")
65 # DEBUG: print(f"DEBUG: reqheaders()={len(reqheaders)} - EXIT!")