]> git.mxchange.org Git - fba.git/blob - fba/helpers/domain.py
39fbb7797acee74c7c5207ed0b3b60c507276b2f
[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 functools import lru_cache
20 from urllib.parse import urlparse
21
22 import validators
23
24 from fba.helpers import blacklist
25 from fba.helpers import config
26
27 from fba.models import instances
28
29 logging.basicConfig(level=logging.INFO)
30 logger = logging.getLogger(__name__)
31
32 def raise_on(domain: str):
33     logger.debug("domain='%s' - CALLED!", domain)
34
35     if not isinstance(domain, str):
36         raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
37     elif domain == "":
38         raise ValueError("Parameter 'domain' is empty")
39     elif domain.lower() != domain:
40         raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
41     elif not validators.domain(domain.split("/")[0]):
42         raise ValueError(f"domain='{domain}' is not a valid domain")
43     elif domain.endswith(".onion"):
44         raise ValueError(f"domain='{domain}' is a TOR, please don't crawl them!")
45     elif domain.endswith(".i2p") and config.get("allow_i2p_domain") == "true":
46         raise ValueError(f"domain='{domain}' is an I2P, please don't crawl them!")
47     elif domain.endswith(".arpa"):
48         raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
49     elif domain.endswith(".tld"):
50         raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
51
52     logger.debug("EXIT!")
53
54 @lru_cache
55 def is_in_url(domain: str, url: str) -> bool:
56     logger.debug("domain='%s',url='%s' - CALLED!", domain, url)
57     raise_on(domain)
58
59     if blacklist.is_blacklisted(domain):
60         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
61     elif not isinstance(url, str):
62         raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
63     elif url == "":
64         raise ValueError("Parameter 'url' is empty")
65     elif not validators.url(url):
66         raise ValueError(f"Parameter url='{url}' is not a valid URL")
67
68     punycode = domain.encode("idna").decode("utf-8")
69
70     components = urlparse(url)
71     logger.debug("components[]='%s',punycode='%s'", type(components), punycode)
72
73     is_found = (punycode in [components.netloc, components.hostname])
74
75     logger.debug("is_found='%s' - EXIT!", is_found)
76     return is_found
77
78 @lru_cache
79 def is_wanted(domain: str) -> bool:
80     logger.debug("domain='%s' - CALLED!", domain)
81
82     if not isinstance(domain, str):
83         raise ValueError(f"Parameter domain[]='{type(domain)}' is not of type 'str'")
84     elif domain == "":
85         raise ValueError("Parameter 'domain' is empty")
86
87     wanted = True
88     if domain.lower() != domain:
89         logger.debug("domain='%s' is not all-lowercase - setting False ...", domain)
90         wanted = False
91     elif not validators.domain(domain.split("/")[0]):
92         logger.debug("domain='%s' is not a valid domain name - setting False ...", domain)
93         wanted = False
94     elif domain.endswith(".arpa"):
95         logger.debug("domain='%s' is a domain for reversed IP addresses - setting False ...", domain)
96         wanted = False
97     elif domain.endswith(".onion"):
98         logger.debug("domain='%s' is a TOR .onion domain - setting False ...", domain)
99         wanted = False
100     elif domain.endswith(".i2p") and config.get("allow_i2p_domain") == "true":
101         logger.debug("domain='%s' is an I2P domain - setting False ...", domain)
102         wanted = False
103     elif domain.endswith(".tld"):
104         logger.debug("domain='%s' is a fake domain - setting False ...", domain)
105         wanted = False
106     elif blacklist.is_blacklisted(domain):
107         logger.debug("domain='%s' is blacklisted - setting False ...", domain)
108         wanted = False
109     elif domain.find("/profile/") > 0 or domain.find("/users/") > 0 or (instances.is_registered(domain.split("/")[0]) and domain.find("/c/") > 0):
110         logger.debug("domain='%s' is a single user", domain)
111         wanted = False
112     elif domain.find("/tag/") > 0:
113         logger.debug("domain='%s' is a tag", domain)
114         wanted = False
115
116     logger.debug("wanted='%s' - EXIT!", wanted)
117     return wanted