Beginning of federated blocklists

main
Bob Mottram 2024-02-09 18:39:44 +00:00
parent 32ed1ed7b3
commit 87f2f94ed3
30 changed files with 228 additions and 31 deletions

View File

@ -1815,3 +1815,127 @@ def contains_military_domain(message_str: str) -> bool:
domain_str + '/' in message_str: domain_str + '/' in message_str:
return True return True
return False return False
def load_federated_blocks_endpoints(base_dir: str) -> []:
"""Loads endpoint urls for federated blocklists
"""
block_federated_endpoints = []
block_api_endpoints_filename = \
base_dir + '/accounts/block_api_endpoints.txt'
if os.path.isfile(block_api_endpoints_filename):
try:
with open(block_api_endpoints_filename, 'r',
encoding='utf-8') as fp_ep:
block_federated_endpoints = fp_ep.read().split('\n')
except OSError:
print('EX: unable to load block_api_endpoints.txt')
return block_federated_endpoints
def update_federated_blocks(session, base_dir: str,
http_prefix: str,
domain: str, domain_full: str,
debug: bool, version: str,
signing_priv_key_pem: str,
max_api_blocks: int) -> []:
"""Creates block_api.txt
"""
block_federated = []
if not session:
print('WARN: No session for update_federated_blocks')
return block_federated
headers = {
'host': domain,
'Content-type': 'application/json',
'Accept': 'application/json'
}
block_federated_endpoints = load_federated_blocks_endpoints(base_dir)
new_block_api_str = ''
for endpoint in block_federated_endpoints:
url = endpoint.strip()
if debug:
print('Block API endpoint: ' + url)
blocked_json = get_json(signing_priv_key_pem, session, url, headers,
None, debug, version, http_prefix, None)
if not get_json_valid(blocked_json):
if debug:
print('DEBUG: GET blocked collection failed for c2s to ' + url)
continue
if isinstance(blocked_json, list):
# ensure that the size of the list does not become a form of denial
# of service
if len(blocked_json) < max_api_blocks:
for block_dict in blocked_json:
if not isinstance(block_dict, dict):
continue
if not block_dict.get('username'):
continue
if not isinstance(block_dict['username'], str):
continue
handle = block_dict['username']
if handle.startswith('@'):
handle = handle[1:]
if ' ' in handle or \
',' in handle or \
';' in handle or \
'.' not in handle or \
'<' in handle:
continue
new_block_api_str += handle + '\n'
if handle not in block_federated:
block_federated.append(handle)
block_api_filename = \
base_dir + '/accounts/block_api.txt'
if not new_block_api_str:
if os.path.isfile(block_api_filename):
try:
os.remove(block_api_filename)
except OSError:
print('EX: unable to remove block api: ' + block_api_filename)
else:
try:
with open(block_api_filename, 'w+', encoding='utf-8') as fp_api:
fp_api.write(new_block_api_str)
except OSError:
print('EX: unable to write block_api.txt')
return block_federated
def save_block_federated_endpoints(base_dir: str,
block_federated_endpoints: []) -> []:
"""Saves a list of blocking API endpoints
"""
block_api_endpoints_filename = \
base_dir + '/accounts/block_api_endpoints.txt'
result = []
block_federated_endpoints_str = ''
for endpoint in block_federated_endpoints:
if not endpoint:
continue
if '.' not in endpoint or \
' ' in endpoint or \
'<' in endpoint or \
',' in endpoint or \
';' in endpoint:
continue
if endpoint.startswith('@'):
endpoint = endpoint[1:]
if not endpoint:
continue
block_federated_endpoints_str += endpoint.strip() + '\n'
result.append(endpoint)
try:
with open(block_api_endpoints_filename, 'w+',
encoding='utf-8') as fp_api:
fp_api.write(block_federated_endpoints_str)
except OSError:
print('EX: unable to write block_api_endpoints.txt')
return result

View File

@ -160,6 +160,9 @@ from media import path_is_transcript
from media import path_is_audio from media import path_is_audio
from cwlists import get_cw_list_variable from cwlists import get_cw_list_variable
from cwlists import load_cw_lists from cwlists import load_cw_lists
from blocking import save_block_federated_endpoints
from blocking import load_federated_blocks_endpoints
from blocking import update_federated_blocks
from blocking import contains_military_domain from blocking import contains_military_domain
from blocking import load_blocked_military from blocking import load_blocked_military
from blocking import save_blocked_military from blocking import save_blocked_military
@ -8456,6 +8459,20 @@ class PubServer(BaseHTTPRequestHandler):
print('EX: unable to delete ' + print('EX: unable to delete ' +
buy_sites_filename) buy_sites_filename)
# save blocking API endpoints
block_ep_new = []
if fields.get('blockFederated'):
block_federated_str = \
fields['blockFederated']
block_ep_new = \
block_federated_str.split('\n')
if str(self.server.block_federated_endpoints) != \
str(block_ep_new):
base_dir = self.server.base_dir
self.server.block_federated_endpoints = \
save_block_federated_endpoints(base_dir,
block_ep_new)
# save peertube instances list # save peertube instances list
peertube_instances_file = \ peertube_instances_file = \
base_dir + '/accounts/peertube.txt' base_dir + '/accounts/peertube.txt'
@ -17311,7 +17328,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.max_recent_posts, self.server.max_recent_posts,
self.server.reverse_sequence, self.server.reverse_sequence,
self.server.buy_sites, self.server.buy_sites,
self.server.block_military) self.server.block_military,
self.server.block_federated_endpoints)
if msg: if msg:
msg = msg.encode('utf-8') msg = msg.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -25259,6 +25277,22 @@ def run_daemon(no_of_books: int,
# this is the instance actor private key # this is the instance actor private key
httpd.signing_priv_key_pem = get_instance_actor_key(base_dir, domain) httpd.signing_priv_key_pem = get_instance_actor_key(base_dir, domain)
# load federated blocklists
httpd.max_api_blocks = 32000
httpd.block_federated_endpoints = \
load_federated_blocks_endpoints(base_dir)
httpd.block_federated = []
if not unit_test:
curr_session = create_session(proxy_type)
if curr_session:
httpd.block_federated = \
update_federated_blocks(curr_session, base_dir,
http_prefix,
domain, httpd.domain_full,
debug, project_version,
httpd.signing_priv_key_pem,
httpd.max_api_blocks)
# threads used for checking for actor changes when clicking on # threads used for checking for actor changes when clicking on
# avatar icon / person options # avatar icon / person options
httpd.thrCheckActor = {} httpd.thrCheckActor = {}

View File

@ -656,5 +656,6 @@
"reading": "قراءة", "reading": "قراءة",
"Birthday": "عيد ميلاد", "Birthday": "عيد ميلاد",
"Only show reminders": "عرض التذكيرات فقط", "Only show reminders": "عرض التذكيرات فقط",
"Show all events": "عرض كافة الأحداث" "Show all events": "عرض كافة الأحداث",
"Blocking API endpoints": "حظر نقاط نهاية API"
} }

View File

@ -656,5 +656,6 @@
"reading": "পড়া", "reading": "পড়া",
"Birthday": "জন্মদিন", "Birthday": "জন্মদিন",
"Only show reminders": "শুধুমাত্র অনুস্মারক দেখান", "Only show reminders": "শুধুমাত্র অনুস্মারক দেখান",
"Show all events": "সমস্ত ঘটনা দেখান" "Show all events": "সমস্ত ঘটনা দেখান",
"Blocking API endpoints": "এপিআই এন্ডপয়েন্ট ব্লক করা"
} }

View File

@ -656,5 +656,6 @@
"reading": "lectura", "reading": "lectura",
"Birthday": "Aniversari", "Birthday": "Aniversari",
"Only show reminders": "Mostra només recordatoris", "Only show reminders": "Mostra només recordatoris",
"Show all events": "Mostra tots els esdeveniments" "Show all events": "Mostra tots els esdeveniments",
"Blocking API endpoints": "Bloqueig de punts finals de l'API"
} }

View File

@ -656,5 +656,6 @@
"reading": "darllen", "reading": "darllen",
"Birthday": "Penblwydd", "Birthday": "Penblwydd",
"Only show reminders": "Dangoswch nodiadau atgoffa yn unig", "Only show reminders": "Dangoswch nodiadau atgoffa yn unig",
"Show all events": "Dangos pob digwyddiad" "Show all events": "Dangos pob digwyddiad",
"Blocking API endpoints": "Rhwystro pwyntiau terfyn API"
} }

View File

@ -656,5 +656,6 @@
"reading": "lektüre", "reading": "lektüre",
"Birthday": "Geburtstag", "Birthday": "Geburtstag",
"Only show reminders": "Nur Erinnerungen anzeigen", "Only show reminders": "Nur Erinnerungen anzeigen",
"Show all events": "Alle Veranstaltungen anzeigen" "Show all events": "Alle Veranstaltungen anzeigen",
"Blocking API endpoints": "Blockieren von API-Endpunkten"
} }

View File

@ -656,5 +656,6 @@
"reading": "ΑΝΑΓΝΩΣΗ", "reading": "ΑΝΑΓΝΩΣΗ",
"Birthday": "Γενέθλια", "Birthday": "Γενέθλια",
"Only show reminders": "Εμφάνιση μόνο υπενθυμίσεων", "Only show reminders": "Εμφάνιση μόνο υπενθυμίσεων",
"Show all events": "Εμφάνιση όλων των συμβάντων" "Show all events": "Εμφάνιση όλων των συμβάντων",
"Blocking API endpoints": "Αποκλεισμός τελικών σημείων API"
} }

View File

@ -656,5 +656,6 @@
"reading": "reading", "reading": "reading",
"Birthday": "Birthday", "Birthday": "Birthday",
"Only show reminders": "Only show reminders", "Only show reminders": "Only show reminders",
"Show all events": "Show all events" "Show all events": "Show all events",
"Blocking API endpoints": "Blocking API endpoints"
} }

View File

@ -656,5 +656,6 @@
"reading": "lectura", "reading": "lectura",
"Birthday": "Cumpleaños", "Birthday": "Cumpleaños",
"Only show reminders": "Mostrar solo recordatorios", "Only show reminders": "Mostrar solo recordatorios",
"Show all events": "Mostrar todos los eventos" "Show all events": "Mostrar todos los eventos",
"Blocking API endpoints": "Bloqueo de puntos finales de API"
} }

View File

@ -656,5 +656,6 @@
"reading": "خواندن", "reading": "خواندن",
"Birthday": "روز تولد", "Birthday": "روز تولد",
"Only show reminders": "فقط یادآوری ها را نشان دهید", "Only show reminders": "فقط یادآوری ها را نشان دهید",
"Show all events": "نمایش همه رویدادها" "Show all events": "نمایش همه رویدادها",
"Blocking API endpoints": "مسدود کردن نقاط پایانی API"
} }

View File

@ -656,5 +656,6 @@
"reading": "en lisant", "reading": "en lisant",
"Birthday": "Anniversaire", "Birthday": "Anniversaire",
"Only show reminders": "Afficher uniquement les rappels", "Only show reminders": "Afficher uniquement les rappels",
"Show all events": "Afficher tous les événements" "Show all events": "Afficher tous les événements",
"Blocking API endpoints": "Blocage des points de terminaison de l'API"
} }

View File

@ -656,5 +656,6 @@
"reading": "ag léamh", "reading": "ag léamh",
"Birthday": "Breithlá", "Birthday": "Breithlá",
"Only show reminders": "Ná taispeáin ach meabhrúcháin", "Only show reminders": "Ná taispeáin ach meabhrúcháin",
"Show all events": "Taispeáin gach imeacht" "Show all events": "Taispeáin gach imeacht",
"Blocking API endpoints": "Ag cur bac ar chríochphointí API"
} }

View File

@ -656,5 +656,6 @@
"reading": "קריאה", "reading": "קריאה",
"Birthday": "יום הולדת", "Birthday": "יום הולדת",
"Only show reminders": "הצג רק תזכורות", "Only show reminders": "הצג רק תזכורות",
"Show all events": "הצג את כל האירועים" "Show all events": "הצג את כל האירועים",
"Blocking API endpoints": "חסימת נקודות קצה של API"
} }

View File

@ -656,5 +656,6 @@
"reading": "पढ़ना", "reading": "पढ़ना",
"Birthday": "जन्मदिन", "Birthday": "जन्मदिन",
"Only show reminders": "केवल अनुस्मारक दिखाएँ", "Only show reminders": "केवल अनुस्मारक दिखाएँ",
"Show all events": "सभी घटनाएँ दिखाएँ" "Show all events": "सभी घटनाएँ दिखाएँ",
"Blocking API endpoints": "एपीआई समापनबिंदुओं को अवरुद्ध करना"
} }

View File

@ -656,5 +656,6 @@
"reading": "lettura", "reading": "lettura",
"Birthday": "Compleanno", "Birthday": "Compleanno",
"Only show reminders": "Mostra solo promemoria", "Only show reminders": "Mostra solo promemoria",
"Show all events": "Mostra tutti gli eventi" "Show all events": "Mostra tutti gli eventi",
"Blocking API endpoints": "Blocco degli endpoint API"
} }

View File

@ -656,5 +656,6 @@
"reading": "読む", "reading": "読む",
"Birthday": "誕生日", "Birthday": "誕生日",
"Only show reminders": "リマインダーのみを表示", "Only show reminders": "リマインダーのみを表示",
"Show all events": "すべてのイベントを表示" "Show all events": "すべてのイベントを表示",
"Blocking API endpoints": "APIエンドポイントのブロック"
} }

View File

@ -656,5 +656,6 @@
"reading": "독서", "reading": "독서",
"Birthday": "생일", "Birthday": "생일",
"Only show reminders": "알림만 표시", "Only show reminders": "알림만 표시",
"Show all events": "모든 이벤트 표시" "Show all events": "모든 이벤트 표시",
"Blocking API endpoints": "API 엔드포인트 차단"
} }

View File

@ -656,5 +656,6 @@
"reading": "xwendinî", "reading": "xwendinî",
"Birthday": "Rojbûn", "Birthday": "Rojbûn",
"Only show reminders": "Tenê bîranînan nîşan bidin", "Only show reminders": "Tenê bîranînan nîşan bidin",
"Show all events": "Hemî bûyeran nîşan bide" "Show all events": "Hemî bûyeran nîşan bide",
"Blocking API endpoints": "Astengkirina xalên dawiya API-ê"
} }

View File

@ -656,5 +656,6 @@
"reading": "lezing", "reading": "lezing",
"Birthday": "Verjaardag", "Birthday": "Verjaardag",
"Only show reminders": "Toon alleen herinneringen", "Only show reminders": "Toon alleen herinneringen",
"Show all events": "Toon alle evenementen" "Show all events": "Toon alle evenementen",
"Blocking API endpoints": "API-eindpunten blokkeren"
} }

View File

@ -652,5 +652,6 @@
"reading": "reading", "reading": "reading",
"Birthday": "Birthday", "Birthday": "Birthday",
"Only show reminders": "Only show reminders", "Only show reminders": "Only show reminders",
"Show all events": "Show all events" "Show all events": "Show all events",
"Blocking API endpoints": "Blocking API endpoints"
} }

View File

@ -656,5 +656,6 @@
"reading": "czytanie", "reading": "czytanie",
"Birthday": "Urodziny", "Birthday": "Urodziny",
"Only show reminders": "Pokazuj tylko przypomnienia", "Only show reminders": "Pokazuj tylko przypomnienia",
"Show all events": "Pokaż wszystkie wydarzenia" "Show all events": "Pokaż wszystkie wydarzenia",
"Blocking API endpoints": "Blokowanie punktów końcowych API"
} }

View File

@ -656,5 +656,6 @@
"reading": "leitura", "reading": "leitura",
"Birthday": "Aniversário", "Birthday": "Aniversário",
"Only show reminders": "Mostrar apenas lembretes", "Only show reminders": "Mostrar apenas lembretes",
"Show all events": "Mostrar todos os eventos" "Show all events": "Mostrar todos os eventos",
"Blocking API endpoints": "Bloqueio de endpoints de API"
} }

View File

@ -656,5 +656,6 @@
"reading": "чтение", "reading": "чтение",
"Birthday": "День рождения", "Birthday": "День рождения",
"Only show reminders": "Показывать только напоминания", "Only show reminders": "Показывать только напоминания",
"Show all events": "Показать все события" "Show all events": "Показать все события",
"Blocking API endpoints": "Блокировка конечных точек API"
} }

View File

@ -656,5 +656,6 @@
"reading": "kusoma", "reading": "kusoma",
"Birthday": "Siku ya kuzaliwa", "Birthday": "Siku ya kuzaliwa",
"Only show reminders": "Onyesha vikumbusho pekee", "Only show reminders": "Onyesha vikumbusho pekee",
"Show all events": "Onyesha matukio yote" "Show all events": "Onyesha matukio yote",
"Blocking API endpoints": "Inazuia miisho ya API"
} }

View File

@ -656,5 +656,6 @@
"reading": "okuma", "reading": "okuma",
"Birthday": "Doğum günü", "Birthday": "Doğum günü",
"Only show reminders": "Yalnızca hatırlatıcıları göster", "Only show reminders": "Yalnızca hatırlatıcıları göster",
"Show all events": "Tüm etkinlikleri göster" "Show all events": "Tüm etkinlikleri göster",
"Blocking API endpoints": "API uç noktalarını engelleme"
} }

View File

@ -656,5 +656,6 @@
"reading": "читання", "reading": "читання",
"Birthday": "день народження", "Birthday": "день народження",
"Only show reminders": "Показувати лише нагадування", "Only show reminders": "Показувати лише нагадування",
"Show all events": "Показати всі події" "Show all events": "Показати всі події",
"Blocking API endpoints": "Блокування кінцевих точок API"
} }

View File

@ -656,5 +656,6 @@
"reading": "לייענען", "reading": "לייענען",
"Birthday": "דיין געבורסטאָג", "Birthday": "דיין געבורסטאָג",
"Only show reminders": "בלויז ווייַזן רימיינדערז", "Only show reminders": "בלויז ווייַזן רימיינדערז",
"Show all events": "ווייַזן אַלע געשעענישן" "Show all events": "ווייַזן אַלע געשעענישן",
"Blocking API endpoints": "בלאַקינג אַפּי ענדפּאָינץ"
} }

View File

@ -656,5 +656,6 @@
"reading": "阅读", "reading": "阅读",
"Birthday": "生日", "Birthday": "生日",
"Only show reminders": "只显示提醒", "Only show reminders": "只显示提醒",
"Show all events": "显示所有活动" "Show all events": "显示所有活动",
"Blocking API endpoints": "阻止 API 端点"
} }

View File

@ -2223,7 +2223,8 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
crawlers_allowed: str, crawlers_allowed: str,
translate: {}, reply_interval_hours: int, translate: {}, reply_interval_hours: int,
cw_lists: {}, lists_enabled: str, cw_lists: {}, lists_enabled: str,
buy_sites: {}, block_military: {}) -> str: buy_sites: {}, block_military: {},
block_federated_endpoints: []) -> str:
"""Filtering and blocking section of edit profile screen """Filtering and blocking section of edit profile screen
""" """
filter_str = '' filter_str = ''
@ -2447,6 +2448,16 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
edit_profile_form += \ edit_profile_form += \
edit_check_box(idx, 'blockMilitary', block_mil) edit_check_box(idx, 'blockMilitary', block_mil)
block_federated_endpoints_list_str = ''
for block_api_url in block_federated_endpoints:
block_federated_endpoints_list_str += block_api_url.strip() + '\n'
block_federated_str = "Blocking API endpoints"
edit_profile_form += \
edit_text_area(translate[block_federated_str], None,
'blockFederated',
block_federated_endpoints_list_str,
200, '', False)
cw_lists_str = '' cw_lists_str = ''
for name, _ in cw_lists.items(): for name, _ in cw_lists.items():
variablename = get_cw_list_variable(name) variablename = get_cw_list_variable(name)
@ -2943,7 +2954,8 @@ def html_edit_profile(server, translate: {},
max_recent_posts: int, max_recent_posts: int,
reverse_sequence: [], reverse_sequence: [],
buy_sites: {}, buy_sites: {},
block_military: {}) -> str: block_military: {},
block_federated_endpoints: []) -> str:
"""Shows the edit profile screen """Shows the edit profile screen
""" """
path = path.replace('/inbox', '').replace('/outbox', '') path = path.replace('/inbox', '').replace('/outbox', '')
@ -3214,7 +3226,7 @@ def html_edit_profile(server, translate: {},
user_agents_blocked, crawlers_allowed, user_agents_blocked, crawlers_allowed,
translate, reply_interval_hours, translate, reply_interval_hours,
cw_lists, lists_enabled, buy_sites, cw_lists, lists_enabled, buy_sites,
block_military) block_military, block_federated_endpoints)
# git projects section # git projects section
edit_profile_form += \ edit_profile_form += \