support blocklists from Mastodon API compatible nodes

This commit is contained in:
Tobias Diekershoff 2023-01-07 22:46:20 +01:00
parent a30b809cbf
commit 4fa20216f9
2 changed files with 31 additions and 11 deletions

View file

@ -92,6 +92,13 @@ For example
domains = friendica.example.com domains = friendica.example.com
``` ```
You can also add Mastodon instances you trust. In addition to the configuration
needed for Friendica nodes you have to add the `type = mastodon` entry to the
config section.
Please note only suspended entries from the Mastodon blocklist will be added to
the blocklist. Silenced entries will be ignored.
### Running the script ### Running the script
You have to supply the file name of the configuration file on the command You have to supply the file name of the configuration file on the command

View file

@ -37,9 +37,12 @@ class BrewBlocklist():
for section in config.sections(): for section in config.sections():
section_values = dict(config.items(section)) section_values = dict(config.items(section))
if not section == 'safe harbor': if not section == 'safe harbor':
if not 'type' in section_values.keys():
section_values['type'] = 'friendica'
self.sources.append({ self.sources.append({
'url': section, 'url': section,
'trust': int(section_values['trust']), 'trust': int(section_values['trust']),
'type': section_values['type']
}) })
else: else:
for item in section_values['domains'].split(','): for item in section_values['domains'].split(','):
@ -57,17 +60,27 @@ class BrewBlocklist():
mention of the server wins) and sum the trust levels of the blocks. mention of the server wins) and sum the trust levels of the blocks.
""" """
for source in self.sources: for source in self.sources:
requ = requests.get('https://{}/blocklist/domain/download'.format(source['url'])) if source['type'] == 'friendica':
for line in requ.text.split('\n'): # Friendica publishes the blocklist as CSV file
try: requ = requests.get('https://{}/blocklist/domain/download'.format(source['url']))
pattern, reason = line.split(',') for line in requ.text.split('\n'):
except ValueError: try:
# happens in an empty line in the source CSV file, which seems pattern, reason = line.split(',')
# to be the last line of the file so we can just break the loop except ValueError:
# one step early and ignore the exception silently. # happens in an empty line in the source CSV file, which seems
break # to be the last line of the file so we can just break the loop
self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust'] # one step early and ignore the exception silently.
self.reasons[pattern] = self.reasons.get(pattern, reason) break
self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust']
self.reasons[pattern] = self.reasons.get(pattern, reason)
elif source['type'] == 'mastodon':
# Mastodon has an API endpoint that contains the information
requ = requests.get('https://{}//api/v1/instance/domain_blocks'.format(source['url']))
for item in requ.json():
self.blocklist[item['domain']] = self.blocklist.get(item['domain'], 0) + source['trust']
self.reasons[item['domain']] = self.reasons.get(item['domain'], item['comment'])
else:
raise ValueError('{} is not a supported node type, check your config file'.format(source['type']))
def clean_list(self): def clean_list(self):
""" """