]> git.mxchange.org Git - fba.git/blob - fba/networks/friendica.py
Continued:
[fba.git] / fba / networks / friendica.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 import bs4
20
21 from fba import utils
22
23 from fba.helpers import config
24 from fba.helpers import domain as domain_helper
25 from fba.helpers import tidyup
26
27 from fba.http import network
28
29 from fba.models import instances
30
31 logging.basicConfig(level=logging.INFO)
32 logger = logging.getLogger(__name__)
33 #logger.setLevel(logging.DEBUG)
34
35 def fetch_blocks(domain: str) -> list:
36     logger.debug("domain(%d)='%s' - CALLED!", len(domain), domain)
37     domain_helper.raise_on(domain)
38
39     blocklist = list()
40     block_tag = None
41
42     try:
43         logger.debug("Fetching friendica blocks from domain='%s'", domain)
44         doc = bs4.BeautifulSoup(
45             network.fetch_response(
46                 domain,
47                 "/friendica",
48                 network.web_headers,
49                 (config.get("connection_timeout"), config.get("read_timeout"))
50             ).text,
51             "html.parser",
52         )
53         logger.debug("doc[]='%s'", type(doc))
54
55         block_tag = doc.find(id="about_blocklist")
56     except network.exceptions as exception:
57         logger.warning("Exception '%s' during fetching instances from domain='%s'", type(exception), domain)
58         instances.set_last_error(domain, exception)
59         return list()
60
61     # Prevents exceptions:
62     if block_tag is None:
63         logger.debug("Instance has no block list: domain='%s'", domain)
64         return list()
65
66     table = block_tag.find("table")
67
68     logger.debug("table[]='%s'", type(table))
69     if table.find("tbody"):
70         rows = table.find("tbody").find_all("tr")
71     else:
72         rows = table.find_all("tr")
73
74     logger.debug("Found rows()=%d", len(rows))
75     for line in rows:
76         logger.debug("line='%s'", line)
77         blocked = tidyup.domain(line.find_all("td")[0].text)
78         reason  = tidyup.reason(line.find_all("td")[1].text)
79         logger.debug("blocked='%s',reason='%s'", blocked, reason)
80
81         if blocked == "":
82             logger.debug("line[]='%s' returned empty blocked domain - SKIPPED!")
83             continue
84         elif blocked.count("*") > 0:
85             logger.debug("domain='%s' uses obfuscated domains, marking ...", domain)
86             instances.set_has_obfuscation(domain, True)
87
88             # Obscured domain name with no hash
89             row = instances.deobfuscate("*", blocked)
90
91             logger.debug("row[]='%s'", type(row))
92             if row is None:
93                 logger.warning("Cannot deobfuscate blocked='%s',domain='%s' - SKIPPED!", blocked, domain)
94                 continue
95
96             logger.debug("blocked='%s' de-obscured to '%s'", blocked, row[0])
97             blocked = row[0]
98         elif blocked.count("?") > 0:
99             logger.debug("domain='%s' uses obfuscated domains, marking ...", domain)
100             instances.set_has_obfuscation(domain, True)
101
102             # Obscured domain name with no hash
103             row = instances.deobfuscate("?", blocked)
104
105             logger.debug("row[]='%s'", type(row))
106             if row is None:
107                 logger.warning("Cannot deobfuscate blocked='%s',domain='%s' - SKIPPED!", blocked, domain)
108                 continue
109
110             logger.debug("blocked='%s' de-obscured to '%s'", blocked, row[0])
111             blocked = row[0]
112
113         logger.debug("blocked[%s]='%s'", type(blocked), blocked)
114         if not utils.is_domain_wanted(blocked):
115             logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
116             continue
117
118         logger.debug(f"Appending blocked='{blocked}',reason='{reason}'")
119         blocklist.append({
120             "blocker"    : domain,
121             "blocked"    : tidyup.domain(blocked),
122             "reason"     : tidyup.reason(reason),
123             "block_level": "reject",
124         })
125
126     logger.debug("blocklist()=%d - EXIT!", len(blocklist))
127     return blocklist