]> git.mxchange.org Git - fba.git/blob - fba/federation/mastodon.py
Continued:
[fba.git] / fba / federation / mastodon.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 bs4
18 import validators
19
20 from fba import blacklist
21 from fba import blocks
22 from fba import config
23 from fba import fba
24
25 language_mapping = {
26     # English -> English
27     "Silenced instances"            : "Silenced servers",
28     "Suspended instances"           : "Suspended servers",
29     "Limited instances"             : "Limited servers",
30     "Filtered media"                : "Filtered media",
31     # Mappuing German -> English
32     "Gesperrte Server"              : "Suspended servers",
33     "Gefilterte Medien"             : "Filtered media",
34     "Stummgeschaltete Server"       : "Silenced servers",
35     # Japanese -> English
36     "停止済みのサーバー"            : "Suspended servers",
37     "制限中のサーバー"              : "Limited servers",
38     "メディアを拒否しているサーバー": "Filtered media",
39     "サイレンス済みのサーバー"      : "Silenced servers",
40     # ??? -> English
41     "שרתים מושעים"                  : "Suspended servers",
42     "מדיה מסוננת"                   : "Filtered media",
43     "שרתים מוגבלים"                 : "Silenced servers",
44     # French -> English
45     "Serveurs suspendus"            : "Suspended servers",
46     "Médias filtrés"                : "Filtered media",
47     "Serveurs limités"              : "Limited servers",
48     "Serveurs modérés"              : "Limited servers",
49 }
50
51 def fetch_blocks_from_about(domain: str) -> dict:
52     # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
53     if type(domain) != str:
54         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
55     elif domain == "":
56         raise ValueError(f"Parameter 'domain' is empty")
57
58     # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
59     blocklist = {
60         "Suspended servers": [],
61         "Filtered media"   : [],
62         "Limited servers"  : [],
63         "Silenced servers" : [],
64     }
65
66     try:
67         doc = bs4.BeautifulSoup(
68             fba.get_response(domain, "/about/more", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
69             "html.parser",
70         )
71     except BaseException as e:
72         print("ERROR: Cannot fetch from domain:", domain, e)
73         fba.update_last_error(domain, e)
74         return {}
75
76     for header in doc.find_all("h3"):
77         header_text = fba.tidyup_reason(header.text)
78
79         # DEBUG: print(f"DEBUG: header_text='{header_text}'")
80         if header_text in language_mapping:
81             # DEBUG: print(f"DEBUG: header_text='{header_text}'")
82             header_text = language_mapping[header_text]
83         else:
84             print(f"WARNING: header_text='{header_text}' not found in language mapping table")
85
86         if header_text in blocklist or header_text.lower() in blocklist:
87             # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
88             for line in header.find_all_next("table")[0].find_all("tr")[1:]:
89                 blocklist[header_text].append(
90                     {
91                         "domain": fba.tidyup_domain(line.find("span").text),
92                         "hash"  : fba.tidyup_domain(line.find("span")["title"][9:]),
93                         "reason": fba.tidyup_domain(line.find_all("td")[1].text),
94                     }
95                 )
96         else:
97             print(f"WARNING: header_text='{header_text}' not found in blocklist()={len(blocklist)}")
98
99     # DEBUG: print("DEBUG: Returning blocklist for domain:", domain)
100     return {
101         "reject"        : blocklist["Suspended servers"],
102         "media_removal" : blocklist["Filtered media"],
103         "followers_only": blocklist["Limited servers"] + blocklist["Silenced servers"],
104     }
105
106 def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
107     print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
108     if type(domain) != str:
109         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
110     elif domain == "":
111         raise ValueError(f"Parameter 'domain' is empty")
112     elif type(origin) != str and origin != None:
113         raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
114     elif origin == "":
115         raise ValueError(f"Parameter 'origin' is empty")
116     elif type(nodeinfo_url) != str:
117         raise ValueError(f"Parameter nodeinfo_url[]={type(nodeinfo_url)} is not 'str'")
118     elif nodeinfo_url == "":
119         raise ValueError(f"Parameter 'nodeinfo_url' is empty")
120
121     try:
122         # json endpoint for newer mastodongs
123         blockdict = list()
124         try:
125             json = {
126                 "reject"        : [],
127                 "media_removal" : [],
128                 "followers_only": [],
129                 "report_removal": []
130             }
131
132             # handling CSRF, I've saw at least one server requiring it to access the endpoint
133             # DEBUG: print("DEBUG: Fetching meta:", domain)
134             meta = bs4.BeautifulSoup(
135                 fba.get_response(domain, "/", fba.headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
136                 "html.parser",
137             )
138             try:
139                 csrf = meta.find("meta", attrs={"name": "csrf-token"})["content"]
140                 # DEBUG: print("DEBUG: Adding CSRF token:", domain, csrf)
141                 reqheaders = {**fba.api_headers, **{"X-CSRF-Token": csrf}}
142             except BaseException as e:
143                 # DEBUG: print("DEBUG: No CSRF token found, using normal headers:", domain, e)
144                 reqheaders = fba.api_headers
145
146             # DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
147             blocklist = fba.get_response(domain, "/api/v1/instance/domain_blocks", reqheaders, (config.get("connection_timeout"), config.get("read_timeout"))).json()
148
149             print(f"INFO: Checking {len(blocklist)} entries from domain='{domain}',software='mastodon' ...")
150             for block in blocklist:
151                 entry = {
152                     'domain': block['domain'],
153                     'hash'  : block['digest'],
154                     'reason': block['comment']
155                 }
156
157                 # DEBUG: print("DEBUG: severity,domain,hash,comment:", block['severity'], block['domain'], block['digest'], block['comment'])
158                 if block['severity'] == 'suspend':
159                     # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
160                     json['reject'].append(entry)
161                 elif block['severity'] == 'silence':
162                     # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
163                     json['followers_only'].append(entry)
164                 elif block['severity'] == 'reject_media':
165                     # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
166                     json['media_removal'].append(entry)
167                 elif block['severity'] == 'reject_reports':
168                     # DEBUG: print(f"DEBUG: Adding entry='{entry}' with severity='{block['severity']}' ...")
169                     json['report_removal'].append(entry)
170                 else:
171                     print("WARNING: Unknown severity:", block['severity'], block['domain'])
172
173         except BaseException as e:
174             # DEBUG: print(f"DEBUG: Failed, trying mastodon-specific fetches: domain='{domain}',exception[{type(e)}]={str(e)}")
175             json = fetch_blocks_from_about(domain)
176
177         print(f"INFO: Checking {len(json.items())} entries from domain='{domain}',software='mastodon' ...")
178         for block_level, blocklist in json.items():
179             # DEBUG: print("DEBUG: domain,block_level,blocklist():", domain, block_level, len(blocklist))
180             block_level = fba.tidyup_domain(block_level)
181
182             # DEBUG: print("DEBUG: AFTER-block_level:", block_level)
183             if block_level == "":
184                 print("WARNING: block_level is empty, domain:", domain)
185                 continue
186
187             # DEBUG: print(f"DEBUG: Checking {len(blocklist)} entries from domain='{domain}',software='mastodon',block_level='{block_level}' ...")
188             for block in blocklist:
189                 blocked, blocked_hash, reason = block.values()
190                 # DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
191                 blocked = fba.tidyup_domain(blocked)
192                 # DEBUG: print("DEBUG: AFTER-blocked:", blocked)
193
194                 if blocked == "":
195                     print("WARNING: blocked is empty:", domain)
196                     continue
197                 elif blacklist.is_blacklisted(blocked):
198                     # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!")
199                     continue
200                 elif blocked.count("*") > 0:
201                     # Doing the hash search for instance names as well to tidy up DB
202                     fba.cursor.execute(
203                         "SELECT domain, origin, nodeinfo_url FROM instances WHERE hash = ? LIMIT 1", [blocked_hash]
204                     )
205                     searchres = fba.cursor.fetchone()
206
207                     if searchres == None:
208                         print(f"WARNING: Cannot deobsfucate blocked='{blocked}',blocked_hash='{blocked_hash}' - SKIPPED!")
209                         continue
210
211                     # DEBUG: print("DEBUG: Updating domain: ", searchres[0])
212                     blocked = searchres[0]
213                     origin = searchres[1]
214                     nodeinfo_url = searchres[2]
215
216                     # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
217                     if not validators.domain(blocked):
218                         print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
219                         continue
220                     elif not fba.is_instance_registered(blocked):
221                         # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
222                         fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
223                 elif not validators.domain(blocked):
224                     print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
225                     continue
226
227                 # DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
228                 if not validators.domain(blocked):
229                     print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
230                     continue
231                 elif not fba.is_instance_registered(blocked):
232                     # DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, domain)
233                     fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
234
235                 blocking = blocked if blocked.count("*") <= 1 else blocked_hash
236                 # DEBUG: print(f"DEBUG: blocking='{blocking}',blocked='{blocked}',blocked_hash='{blocked_hash}'")
237
238                 if not blocks.is_instance_blocked(domain, blocked, block_level):
239                     # DEBUG: print("DEBUG: Blocking:", domain, blocked, block_level)
240                     blocks.add_instance(domain, blocking, reason, block_level)
241
242                     if block_level == "reject":
243                         blockdict.append({
244                             "blocked": blocked,
245                             "reason" : reason
246                         })
247                 else:
248                     # DEBUG: print(f"DEBUG: Updating block last seen and reason for domain='{domain}',blocking='{blocking}' ...")
249                     blocks.update_last_seen(domain, blocking, block_level)
250                     blocks.update_reason(reason, domain, blocking, block_level)
251
252         # DEBUG: print("DEBUG: Committing changes ...")
253         fba.connection.commit()
254     except Exception as e:
255         print(f"ERROR: domain='{domain}',software='mastodon',exception[{type(e)}]:'{str(e)}'")
256
257     # DEBUG: print("DEBUG: EXIT!")