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/>.
19 from urllib.parse import urlparse
23 from fba.helpers import blacklist
25 from fba.models import instances
27 logging.basicConfig(level=logging.INFO)
28 logger = logging.getLogger(__name__)
30 def raise_on(domain: str):
31 logger.debug("domain='%s' - CALLED!", domain)
32 if not isinstance(domain, str):
33 raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
35 raise ValueError("Parameter 'domain' is empty")
36 elif domain.lower() != domain:
37 raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
38 elif not validators.domain(domain.split("/")[0]):
39 raise ValueError(f"domain='{domain}' is not a valid domain")
40 elif domain.endswith(".arpa"):
41 raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
42 elif domain.endswith(".onion"):
43 raise ValueError(f"domain='{domain}' is a TOR, please don't crawl them!")
44 elif domain.endswith(".tld"):
45 raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
49 def is_in_url(domain: str, url: str) -> bool:
50 logger.debug("domain='%s',url='%s' - CALLED!", domain, url)
53 if not isinstance(url, str):
54 raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
56 raise ValueError("Parameter 'url' is empty")
58 punycode = domain.encode("idna").decode("utf-8")
60 components = urlparse(url)
61 logger.debug("components[]='%s',punycode='%s'", type(components), punycode)
63 is_found = (punycode in [components.netloc, components.hostname])
65 logger.debug("is_found='%s' - EXIT!", is_found)
68 def is_wanted(domain: str) -> bool:
69 logger.debug("domain='%s' - CALLED!", domain)
72 if not isinstance(domain, str):
73 raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
75 raise ValueError("Parameter 'domain' is empty")
76 elif domain.lower() != domain:
78 elif not validators.domain(domain.split("/")[0]):
79 logger.debug("domain='%s' is not a valid domain name - setting False ...", domain)
81 elif domain.endswith(".arpa"):
82 logger.debug("domain='%s' is a domain for reversed IP addresses - setting False ...", domain)
84 elif domain.endswith(".onion"):
85 logger.debug("domain='%s' is a TOR .onion domain - setting False ...", domain)
87 elif domain.endswith(".tld"):
88 logger.debug("domain='%s' is a fake domain - setting False ...", domain)
90 elif blacklist.is_blacklisted(domain):
91 logger.debug("domain='%s' is blacklisted - setting False ...", domain)
93 elif domain.find("/profile/") > 0 or domain.find("/users/") > 0 or (instances.is_registered(domain.split("/")[0]) and domain.find("/c/") > 0):
94 logger.debug("domain='%s' is a single user", domain)
96 elif domain.find("/tag/") > 0:
97 logger.debug("domain='%s' is a tag", domain)
100 logger.debug("wanted='%s' - EXIT!", wanted)