]> git.mxchange.org Git - fba.git/blob - fba/helpers/domain.py
84c9bcb3c48dc07a30aa3390408e7be061154993
[fba.git] / fba / helpers / domain.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
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.
8 #
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.
13 #
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/>.
16
17 import logging
18
19 from urllib.parse import urlparse
20
21 import validators
22
23 from fba.helpers import blacklist
24
25 from fba.models import instances
26
27 logging.basicConfig(level=logging.INFO)
28 logger = logging.getLogger(__name__)
29
30 def raise_on(domain: str):
31     logger.debug("domain='%s' - CALLED!", domain)
32
33     if not isinstance(domain, str):
34         raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
35     elif domain == "":
36         raise ValueError("Parameter 'domain' is empty")
37     elif domain.lower() != domain:
38         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
39     elif not validators.domain(domain.split("/")[0]):
40         raise ValueError(f"domain='{domain}' is not a valid domain")
41     elif domain.endswith(".arpa"):
42         raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
43     elif domain.endswith(".onion"):
44         raise ValueError(f"domain='{domain}' is a TOR, please don't crawl them!")
45     elif domain.endswith(".tld"):
46         raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
47
48     logger.debug("EXIT!")
49
50 def is_in_url(domain: str, url: str) -> bool:
51     logger.debug("domain='%s',url='%s' - CALLED!", domain, url)
52     raise_on(domain)
53
54     if not isinstance(url, str):
55         raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
56     elif url == "":
57         raise ValueError("Parameter 'url' is empty")
58
59     punycode = domain.encode("idna").decode("utf-8")
60
61     components = urlparse(url)
62     logger.debug("components[]='%s',punycode='%s'", type(components), punycode)
63
64     is_found = (punycode in [components.netloc, components.hostname])
65
66     logger.debug("is_found='%s' - EXIT!", is_found)
67     return is_found
68
69 def is_wanted(domain: str) -> bool:
70     logger.debug("domain='%s' - CALLED!", domain)
71
72     if not isinstance(domain, str):
73         raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
74     elif domain == "":
75         raise ValueError("Parameter 'domain' is empty")
76
77     wanted = True
78     if domain.lower() != domain:
79         wanted = False
80     elif not validators.domain(domain.split("/")[0]):
81         logger.debug("domain='%s' is not a valid domain name - setting False ...", domain)
82         wanted = False
83     elif domain.endswith(".arpa"):
84         logger.debug("domain='%s' is a domain for reversed IP addresses - setting False ...", domain)
85         wanted = False
86     elif domain.endswith(".onion"):
87         logger.debug("domain='%s' is a TOR .onion domain - setting False ...", domain)
88         wanted = False
89     elif domain.endswith(".tld"):
90         logger.debug("domain='%s' is a fake domain - setting False ...", domain)
91         wanted = False
92     elif blacklist.is_blacklisted(domain):
93         logger.debug("domain='%s' is blacklisted - setting False ...", domain)
94         wanted = False
95     elif domain.find("/profile/") > 0 or domain.find("/users/") > 0 or (instances.is_registered(domain.split("/")[0]) and domain.find("/c/") > 0):
96         logger.debug("domain='%s' is a single user", domain)
97         wanted = False
98     elif domain.find("/tag/") > 0:
99         logger.debug("domain='%s' is a tag", domain)
100         wanted = False
101
102     logger.debug("wanted='%s' - EXIT!", wanted)
103     return wanted