Option to block government instances

main
Bob Mottram 2024-12-15 13:01:26 +00:00
parent baa01f22e5
commit eaf57a9781
36 changed files with 198 additions and 42 deletions

View File

@ -1922,6 +1922,27 @@ def load_blocked_military(base_dir: str) -> {}:
return nicknames_dict
def load_blocked_government(base_dir: str) -> {}:
"""Loads a list of nicknames for accounts which block government instances
"""
block_government_filename = data_dir(base_dir) + '/block_government.txt'
nicknames_list = []
if os.path.isfile(block_government_filename):
try:
with open(block_government_filename, 'r',
encoding='utf-8') as fp_gov:
nicknames_list = fp_gov.read()
except OSError:
print('EX: error while reading block government file')
if not nicknames_list:
return {}
nicknames_list = nicknames_list.split('\n')
nicknames_dict = {}
for nickname in nicknames_list:
nicknames_dict[nickname] = True
return nicknames_dict
def load_blocked_bluesky(base_dir: str) -> {}:
"""Loads a list of nicknames for accounts which block bluesky bridges
"""
@ -1930,8 +1951,8 @@ def load_blocked_bluesky(base_dir: str) -> {}:
if os.path.isfile(block_bluesky_filename):
try:
with open(block_bluesky_filename, 'r',
encoding='utf-8') as fp_mil:
nicknames_list = fp_mil.read()
encoding='utf-8') as fp_bsky:
nicknames_list = fp_bsky.read()
except OSError:
print('EX: error while reading block bluesky file')
if not nicknames_list:
@ -1959,6 +1980,22 @@ def save_blocked_military(base_dir: str, block_military: {}) -> None:
print('EX: error while saving block military file')
def save_blocked_government(base_dir: str, block_government: {}) -> None:
"""Saves a list of nicknames for accounts which block government instances
"""
nicknames_str = ''
for nickname, _ in block_government.items():
nicknames_str += nickname + '\n'
block_government_filename = data_dir(base_dir) + '/block_government.txt'
try:
with open(block_government_filename, 'w+',
encoding='utf-8') as fp_gov:
fp_gov.write(nicknames_str)
except OSError:
print('EX: error while saving block government file')
def save_blocked_bluesky(base_dir: str, block_bluesky: {}) -> None:
"""Saves a list of nicknames for accounts which block bluesky bridges
"""
@ -1969,8 +2006,8 @@ def save_blocked_bluesky(base_dir: str, block_bluesky: {}) -> None:
block_bluesky_filename = data_dir(base_dir) + '/block_bluesky.txt'
try:
with open(block_bluesky_filename, 'w+',
encoding='utf-8') as fp_mil:
fp_mil.write(nicknames_str)
encoding='utf-8') as fp_bsky:
fp_bsky.write(nicknames_str)
except OSError:
print('EX: error while saving block bluesky file')
@ -1982,6 +2019,12 @@ def get_mil_domains_list() -> []:
'sncorp.com', 'sierranevadacorp.us', 'ncontext.com')
def get_gov_domains_list() -> []:
"""returns a list of government domains
"""
return ('.gov', '.overheid.nl', '.bund.de')
def get_bsky_domains_list() -> []:
"""returns a list of bluesky bridges
"""
@ -2005,17 +2048,24 @@ def contains_military_domain(message_str: str) -> bool:
return False
def contains_government_domain(message_str: str) -> bool:
"""Returns true if the given string contains a government domain
"""
if '.gov.' in message_str:
return True
gov_domains = get_gov_domains_list()
for domain_str in gov_domains:
if domain_str + '"' in message_str or \
domain_str + '/' in message_str:
return True
return False
def contains_bluesky_domain(message_str: str) -> bool:
"""Returns true if the given string contains a bluesky bridge domain
"""
bsky_domains = get_bsky_domains_list()
for domain_str in bsky_domains:
if '.' not in domain_str:
tld = domain_str
if '.' + tld + '"' in message_str or \
'.' + tld + '/' in message_str:
return True
else:
if domain_str + '"' in message_str or \
domain_str + '/' in message_str:
return True

View File

@ -14,6 +14,7 @@ from utils import save_json
from utils import user_agent_domain
from utils import remove_eol
from blocking import get_mil_domains_list
from blocking import get_gov_domains_list
from blocking import get_bsky_domains_list
from blocking import update_blocked_cache
from blocking import is_blocked_domain
@ -112,6 +113,7 @@ def blocked_user_agent(calling_domain: str, agent_str: str,
crawlers_allowed: [],
known_bots: [], path: str,
block_military: {},
block_government: {},
block_bluesky: {}):
"""Should a GET or POST be blocked based upon its user agent?
"""
@ -252,6 +254,32 @@ def blocked_user_agent(calling_domain: str, agent_str: str,
agent_domain)
break
# optionally block government domains on a per account basis
if not blocked_ua and block_government:
if '/users/' in path:
# which accounts is this?
nickname = path.split('/users/')[1]
if '/' in nickname:
nickname = nickname.split('/')[0]
# does this account block government domains?
if block_government.get(nickname):
gov_domains = get_gov_domains_list()
for domain_str in gov_domains:
if '.' not in domain_str:
tld = domain_str
if agent_domain.endswith('.' + tld):
blocked_ua = True
print('BLOCK: ' +
'Blocked government tld user agent: ' +
agent_domain)
break
else:
if agent_domain.endswith(domain_str):
blocked_ua = True
print('BLOCK: Blocked government user agent: ' +
agent_domain)
break
# optionally block bluesky bridges on a per account basis
if not blocked_ua and block_bluesky:
if '/users/' in path:

View File

@ -32,6 +32,7 @@ from cwlists import load_cw_lists
from blocking import run_federated_blocks_daemon
from blocking import load_federated_blocks_endpoints
from blocking import load_blocked_military
from blocking import load_blocked_government
from blocking import load_blocked_bluesky
from blocking import update_blocked_cache
from blocking import set_broch_mode
@ -307,6 +308,7 @@ class EpicyonServer(ThreadingHTTPServer):
sites_unavailable = None
max_shares_on_profile = 0
block_military = {}
block_government = {}
block_bluesky = {}
followers_synchronization = False
followers_sync_cache = {}
@ -785,6 +787,9 @@ def run_daemon(accounts_data_dir: str,
# load a list of nicknames for accounts blocking military instances
httpd.block_military = load_blocked_military(base_dir)
# load a list of nicknames for accounts blocking government instances
httpd.block_government = load_blocked_government(base_dir)
# load a list of nicknames for accounts blocking bluesky bridges
httpd.block_bluesky = load_blocked_bluesky(base_dir)

View File

@ -359,6 +359,7 @@ def daemon_http_get(self) -> None:
self.server.crawlers_allowed,
self.server.known_bots,
self.path, self.server.block_military,
self.server.block_government,
self.server.block_bluesky)
if block:
if llm:
@ -4342,6 +4343,7 @@ def daemon_http_get(self) -> None:
self.server.reverse_sequence,
self.server.buy_sites,
self.server.block_military,
self.server.block_government,
self.server.block_bluesky,
self.server.block_federated_endpoints):
self.server.getreq_busy = False

View File

@ -534,6 +534,7 @@ def edit_profile2(self, calling_domain: str, path: str,
reverse_sequence: bool,
buy_sites: [],
block_military: {},
block_government: {},
block_bluesky: {},
block_federated_endpoints: []) -> bool:
"""Show the edit profile screen
@ -563,6 +564,7 @@ def edit_profile2(self, calling_domain: str, path: str,
reverse_sequence,
buy_sites,
block_military,
block_government,
block_bluesky,
block_federated_endpoints)
if msg:

View File

@ -22,6 +22,7 @@ from utils import contains_invalid_chars
from utils import remove_id_ending
from utils import check_bad_path
from blocking import contains_military_domain
from blocking import contains_government_domain
from blocking import contains_bluesky_domain
from crawlers import blocked_user_agent
from session import get_session_for_domain
@ -183,6 +184,7 @@ def daemon_http_post(self) -> None:
self.server.crawlers_allowed,
self.server.known_bots,
self.path, self.server.block_military,
self.server.block_government,
self.server.block_bluesky)
if block:
http_400(self)
@ -1121,6 +1123,12 @@ def daemon_http_post(self) -> None:
print('BLOCK: blocked military domain')
self.server.postreq_busy = False
return
if self.server.block_government.get(nickname):
if contains_government_domain(decoded_message_bytes):
http_400(self)
print('BLOCK: blocked government domain')
self.server.postreq_busy = False
return
if self.server.block_bluesky.get(nickname):
if contains_bluesky_domain(decoded_message_bytes):
http_400(self)

View File

@ -12,6 +12,7 @@ import errno
from webfinger import webfinger_update
from socket import error as SocketError
from blocking import save_blocked_military
from blocking import save_blocked_government
from blocking import save_blocked_bluesky
from httpheaders import redirect_headers
from httpheaders import clear_login_details
@ -854,6 +855,25 @@ def _profile_post_block_military(nickname: str, fields: {}, self) -> None:
self.server.block_military)
def _profile_post_block_government(nickname: str, fields: {}, self) -> None:
""" HTTP POST block government instances
"""
block_gov_instances = False
if fields.get('blockGovernment'):
if fields['blockGovernment'] == 'on':
block_gov_instances = True
if block_gov_instances:
if not self.server.block_government.get(nickname):
self.server.block_government[nickname] = True
save_blocked_government(self.server.base_dir,
self.server.block_government)
else:
if self.server.block_government.get(nickname):
del self.server.block_government[nickname]
save_blocked_government(self.server.base_dir,
self.server.block_government)
def _profile_post_block_bluesky(nickname: str, fields: {}, self) -> None:
""" HTTP POST block bluesky bridges
"""
@ -3259,6 +3279,7 @@ def profile_edit(self, calling_domain: str, cookie: str,
actor_json, fields, self,
actor_changed, premium)
_profile_post_block_military(nickname, fields, self)
_profile_post_block_government(nickname, fields, self)
_profile_post_block_bluesky(nickname, fields, self)
_profile_post_no_reply_boosts(base_dir, nickname, domain,
fields)

View File

@ -704,5 +704,6 @@
"Searchable by": "قابلة للبحث بواسطة",
"Yourself": "نفسك",
"Epicyon Instances": "حالات Epicyon",
"Block BlueSky bridges": "حجب جسور بلو سكاي"
"Block BlueSky bridges": "حجب جسور بلو سكاي",
"Block government instances": "حظر حالات الحكومة"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "দ্বারা অনুসন্ধানযোগ্য",
"Yourself": "নিজেকে",
"Epicyon Instances": "এপিসিয়ন দৃষ্টান্ত",
"Block BlueSky bridges": "ব্লুস্কাই ব্রিজ ব্লক করুন"
"Block BlueSky bridges": "ব্লুস্কাই ব্রিজ ব্লক করুন",
"Block government instances": "সরকারী দৃষ্টান্ত ব্লক করুন"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Cercable per",
"Yourself": "Tu mateix",
"Epicyon Instances": "Instàncies d'Epicyon",
"Block BlueSky bridges": "Bloqueja els ponts BlueSky"
"Block BlueSky bridges": "Bloqueja els ponts BlueSky",
"Block government instances": "Bloquejar instàncies governamentals"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Chwiliadwy gan",
"Yourself": "Eich Hun",
"Epicyon Instances": "Enghreifftiau Epicyon",
"Block BlueSky bridges": "Rhwystro pontydd BlueSky"
"Block BlueSky bridges": "Rhwystro pontydd BlueSky",
"Block government instances": "Rhwystro achosion llywodraeth"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Durchsuchbar nach",
"Yourself": "Selbst",
"Epicyon Instances": "Epicyon-Instanzen",
"Block BlueSky bridges": "Blockieren Sie BlueSky-Brücken"
"Block BlueSky bridges": "Blockieren Sie BlueSky-Brücken",
"Block government instances": "Blockieren von Regierungsinstanzen"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Δυνατότητα αναζήτησης από",
"Yourself": "Σύ ο ίδιος",
"Epicyon Instances": "Περιπτώσεις Epicyon",
"Block BlueSky bridges": "Αποκλείστε τις γέφυρες BlueSky"
"Block BlueSky bridges": "Αποκλείστε τις γέφυρες BlueSky",
"Block government instances": "Αποκλεισμός κρατικών περιπτώσεων"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Searchable by",
"Yourself": "Yourself",
"Epicyon Instances": "Epicyon Instances",
"Block BlueSky bridges": "Block BlueSky bridges"
"Block BlueSky bridges": "Block BlueSky bridges",
"Block government instances": "Block government instances"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Buscable por",
"Yourself": "Tú mismo",
"Epicyon Instances": "Instancias de Epicyon",
"Block BlueSky bridges": "Bloquear puentes BlueSky"
"Block BlueSky bridges": "Bloquear puentes BlueSky",
"Block government instances": "Bloquear instancias gubernamentales"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "قابل جستجو توسط",
"Yourself": "خودت",
"Epicyon Instances": "موارد Epicyon",
"Block BlueSky bridges": "پل های BlueSky را مسدود کنید"
"Block BlueSky bridges": "پل های BlueSky را مسدود کنید",
"Block government instances": "موارد دولتی را مسدود کنید"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Haettavissa",
"Yourself": "itseäsi",
"Epicyon Instances": "Epicyonin esiintymät",
"Block BlueSky bridges": "Estä BlueSky-sillat"
"Block BlueSky bridges": "Estä BlueSky-sillat",
"Block government instances": "Estä hallituksen esiintymät"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Recherchable par",
"Yourself": "Toi-même",
"Epicyon Instances": "Instances d'Epicyon",
"Block BlueSky bridges": "Bloquer les ponts BlueSky"
"Block BlueSky bridges": "Bloquer les ponts BlueSky",
"Block government instances": "Bloquer les instances gouvernementales"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Inchuardaithe ag",
"Yourself": "Tú féin",
"Epicyon Instances": "Cásanna Epicyon",
"Block BlueSky bridges": "Bloc droichid BlueSky"
"Block BlueSky bridges": "Bloc droichid BlueSky",
"Block government instances": "Cuir bac ar chásanna rialtais"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "ניתן לחיפוש לפי",
"Yourself": "עַצמְךָ",
"Epicyon Instances": "מופעי Epicyon",
"Block BlueSky bridges": "חסום גשרי BlueSky"
"Block BlueSky bridges": "חסום גשרי BlueSky",
"Block government instances": "חסום מקרים ממשלתיים"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "द्वारा खोजा जा सकता है",
"Yourself": "अपने आप को",
"Epicyon Instances": "एपिक्योन इंस्टेंसेस",
"Block BlueSky bridges": "ब्लूस्काई पुलों को ब्लॉक करें"
"Block BlueSky bridges": "ब्लूस्काई पुलों को ब्लॉक करें",
"Block government instances": "सरकारी इंस्टेंस ब्लॉक करें"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Ricercabile per",
"Yourself": "Te stesso",
"Epicyon Instances": "Istanze di Epicyon",
"Block BlueSky bridges": "Blocca i ponti BlueSky"
"Block BlueSky bridges": "Blocca i ponti BlueSky",
"Block government instances": "Bloccare le istanze governative"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "検索可能",
"Yourself": "あなた自身",
"Epicyon Instances": "エピキオンインスタンス",
"Block BlueSky bridges": "ブルースカイ橋をブロックする"
"Block BlueSky bridges": "ブルースカイ橋をブロックする",
"Block government instances": "政府インスタンスをブロックする"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "검색 가능",
"Yourself": "당신 자신",
"Epicyon Instances": "에피시온 인스턴스",
"Block BlueSky bridges": "블루스카이 다리 차단"
"Block BlueSky bridges": "블루스카이 다리 차단",
"Block government instances": "정부 인스턴스 차단"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Lêgerîn ji hêla",
"Yourself": "Xwe",
"Epicyon Instances": "Mînakên Epicyon",
"Block BlueSky bridges": "Pirên BlueSky asteng bikin"
"Block BlueSky bridges": "Pirên BlueSky asteng bikin",
"Block government instances": "Bûyerên hikûmetê asteng bikin"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Doorzoekbaar op",
"Yourself": "Jezelf",
"Epicyon Instances": "Epicyon-instanties",
"Block BlueSky bridges": "Blokkeer BlueSky-bruggen"
"Block BlueSky bridges": "Blokkeer BlueSky-bruggen",
"Block government instances": "Overheidsinstanties blokkeren"
}

View File

@ -700,5 +700,6 @@
"Searchable by": "Searchable by",
"Yourself": "Yourself",
"Epicyon Instances": "Epicyon Instances",
"Block BlueSky bridges": "Block BlueSky bridges"
"Block BlueSky bridges": "Block BlueSky bridges",
"Block government instances": "Block government instances"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Można wyszukiwać według",
"Yourself": "Się",
"Epicyon Instances": "Instancje Epicyon",
"Block BlueSky bridges": "Zablokuj mosty BlueSky"
"Block BlueSky bridges": "Zablokuj mosty BlueSky",
"Block government instances": "Zablokuj wystąpienia rządowe"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Pesquisável por",
"Yourself": "Tu próprio",
"Epicyon Instances": "Instâncias Epicyon",
"Block BlueSky bridges": "Bloquear pontes BlueSky"
"Block BlueSky bridges": "Bloquear pontes BlueSky",
"Block government instances": "Bloquear instâncias governamentais"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Поиск по",
"Yourself": "Сам",
"Epicyon Instances": "Эпиционные экземпляры",
"Block BlueSky bridges": "Блокировать мосты BlueSky"
"Block BlueSky bridges": "Блокировать мосты BlueSky",
"Block government instances": "Блокировать правительственные инстанции"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Inaweza kutafutwa na",
"Yourself": "Mwenyewe",
"Epicyon Instances": "Matukio ya Epicyon",
"Block BlueSky bridges": "Zuia madaraja ya BlueSky"
"Block BlueSky bridges": "Zuia madaraja ya BlueSky",
"Block government instances": "Zuia matukio ya serikali"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Aranabilir",
"Yourself": "Kendin",
"Epicyon Instances": "Epikyon Örnekleri",
"Block BlueSky bridges": "BlueSky köprülerini engelle"
"Block BlueSky bridges": "BlueSky köprülerini engelle",
"Block government instances": "Hükümet örneklerini engelle"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "Можливість пошуку за",
"Yourself": "себе",
"Epicyon Instances": "Примірники Epicyon",
"Block BlueSky bridges": "Блок BlueSky bridges"
"Block BlueSky bridges": "Блок BlueSky bridges",
"Block government instances": "Блокувати державні інстанції"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "זוך דורך",
"Yourself": "זיך",
"Epicyon Instances": "Epicyon ינסטאַנסיז",
"Block BlueSky bridges": "פאַרשפּאַרן בלוסקי בריקן"
"Block BlueSky bridges": "פאַרשפּאַרן בלוסקי בריקן",
"Block government instances": "פאַרשפּאַרן רעגירונג ינסטאַנסיז"
}

View File

@ -704,5 +704,6 @@
"Searchable by": "可搜索",
"Yourself": "你自己",
"Epicyon Instances": "史诗副本",
"Block BlueSky bridges": "封锁蓝天桥梁"
"Block BlueSky bridges": "封锁蓝天桥梁",
"Block government instances": "阻止政府实例"
}

View File

@ -2408,6 +2408,7 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
translate: {}, reply_interval_hours: int,
cw_lists: {}, lists_enabled: str,
buy_sites: {}, block_military: {},
block_government: {},
block_bluesky: {},
block_federated_endpoints: []) -> str:
"""Filtering and blocking section of edit profile screen
@ -2686,6 +2687,15 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
edit_profile_form += \
edit_check_box(idx, 'blockMilitary', block_mil)
idx = 'Block government instances'
if translate.get(idx):
name = translate[idx]
block_gov = False
if block_government.get(nickname):
block_gov = block_government[nickname]
edit_profile_form += \
edit_check_box(idx, 'blockGovernment', block_gov)
idx = 'Block BlueSky bridges'
if translate.get(idx):
name = translate[idx]
@ -3275,6 +3285,7 @@ def html_edit_profile(server, translate: {},
reverse_sequence: [],
buy_sites: {},
block_military: {},
block_government: {},
block_bluesky: {},
block_federated_endpoints: []) -> str:
"""Shows the edit profile screen
@ -3600,7 +3611,8 @@ def html_edit_profile(server, translate: {},
user_agents_blocked, crawlers_allowed,
translate, reply_interval_hours,
cw_lists, lists_enabled, buy_sites,
block_military, block_bluesky,
block_military, block_government,
block_bluesky,
block_federated_endpoints)
# git projects section