diff --git a/daemon.py b/daemon.py index 25a69e13d..5bf7e6c66 100644 --- a/daemon.py +++ b/daemon.py @@ -63,6 +63,7 @@ from utils import get_full_domain from utils import set_config_param from utils import get_config_param from utils import load_json +from utils import load_mitm_servers from content import load_auto_cw_cache from content import load_dogwhistles from theme import scan_themes_for_scripts @@ -712,7 +713,7 @@ def run_daemon(accounts_data_dir: str, httpd.last_llm_time = None # servers with man-in-the-middle transport encryption - httpd.mitm_servers = [] + httpd.mitm_servers = load_mitm_servers(base_dir) # default "searchable by" for new posts for each account httpd.searchable_by_default = load_searchable_by_default(base_dir) diff --git a/inbox.py b/inbox.py index 23bfd322b..dc0af0466 100644 --- a/inbox.py +++ b/inbox.py @@ -22,6 +22,7 @@ from flags import is_group_account from flags import has_group_type from flags import is_quote_toot from flags import url_permitted +from utils import save_mitm_servers from utils import harmless_markup from utils import quote_toots_allowed from utils import lines_in_file @@ -3227,6 +3228,7 @@ def run_inbox_queue(server, heart_beat_ctr = 0 queue_restore_ctr = 0 + curr_mitm_servers = [] # time when the last DM bounce message was sent # This is in a list so that it can be changed by reference @@ -3259,6 +3261,11 @@ def run_inbox_queue(server, '{:%F %T}'.format(datetime.datetime.now())) heart_beat_ctr = 0 + # save MITM servers list if it has changed + if str(server.mitm_servers) != str(curr_mitm_servers): + curr_mitm_servers = server.mitm_servers.copy() + save_mitm_servers(base_dir, curr_mitm_servers) + if len(queue) == 0: # restore any remaining queue items queue_restore_ctr += 1 diff --git a/utils.py b/utils.py index ac78a9cab..2bdc34428 100644 --- a/utils.py +++ b/utils.py @@ -5359,3 +5359,38 @@ def detect_mitm(self) -> bool: self.headers[header_name.lower()]) return True return False + + +def load_mitm_servers(base_dir: str) -> []: + """Loads a list of servers implementing insecure transport security + """ + mitm_servers_filename = data_dir(base_dir) + '/mitm_servers.txt' + mitm_servers = [] + if os.path.isfile(mitm_servers_filename): + try: + with open(mitm_servers_filename, 'r', + encoding='utf-8') as fp_mitm: + mitm_servers = fp_mitm.read() + except OSError: + print('EX: error while reading mitm_servers.txt') + if not mitm_servers: + return {} + mitm_servers = mitm_servers.split('\n') + return mitm_servers + + +def save_mitm_servers(base_dir: str, mitm_servers: []) -> None: + """Saves a list of servers implementing insecure transport security + """ + mitm_servers_str = '' + for domain in mitm_servers: + if domain: + mitm_servers_str += domain + '\n' + + mitm_servers_filename = data_dir(base_dir) + '/mitm_servers.txt' + try: + with open(mitm_servers_filename, 'w+', + encoding='utf-8') as fp_mitm: + fp_mitm.write(mitm_servers_str) + except OSError: + print('EX: error while saving mitm_servers.txt')