From 6bd34360a6661b6ba26ac0ddabf2cf322302a408 Mon Sep 17 00:00:00 2001 From: example Date: Wed, 3 Aug 2022 17:58:27 +0200 Subject: [PATCH] secure socket wieder in try-except; finally-klauses wieder raus --- source/server/apistatusd.py | 67 +++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/source/server/apistatusd.py b/source/server/apistatusd.py index f8e3355..9a89b4a 100755 --- a/source/server/apistatusd.py +++ b/source/server/apistatusd.py @@ -379,44 +379,47 @@ def main(): ClientSocket, ClientAddress = MySocket.accept() logging.info('Client connected: {}:{}'.format(ClientAddress[0], ClientAddress[1])) # die verbindung in den ssl-context verpacken => Connection - with context.wrap_socket(ClientSocket, server_side=True) as Connection: + try: + Connection = context.wrap_socket(ClientSocket, server_side=True) logging.info('SSL Connection established') - try: - Connection.settimeout(float(config['general']['timeout'])) - logging.debug('Connection timeout set to {}'.format(config['general']['timeout'])) - cert = Connection.getpeercert(binary_form=False) - display_peercert(cert) - except Exception as e: - logging.error('Unexpected error: {}'.format(e)) - continue - # empfangen und antworten - raw_data = Connection.recv(1) - if receive_buffer_is_valid(raw_data) is True: - status, timestamp = get_status_and_time(raw_data) - if change_status(status, timestamp, config['api']['api']) is True: - answer = raw_data - if config['mastodon']['send'].lower() == 'true': - logging.debug('Toot is set to true') - try: - toot_thread = Toot(status, timestamp, config) - toot_thread.run() - except InitException as e: - logging.error('InitException: {}'.format(e)) - except Exception as ex: - logging.debug('Exception: {}'.format(ex)) - else: logging.debug('Toot is set to false') - logging.debug('Send {} back'.format(raw_data)) - Connection.send(answer) - Connection.close() + Connection.settimeout(float(config['general']['timeout'])) + logging.debug('Connection timeout set to {}'.format(config['general']['timeout'])) + cert = Connection.getpeercert(binary_form=False) + display_peercert(cert) + except Exception as e: + logging.error('Unexpected error: {}'.format(e)) + continue + # empfangen und antworten + raw_data = Connection.recv(1) + if receive_buffer_is_valid(raw_data) is True: + status, timestamp = get_status_and_time(raw_data) + if change_status(status, timestamp, config['api']['api']) is True: + answer = raw_data + if config['mastodon']['send'].lower() == 'true': + logging.debug('Toot is set to true') + try: + toot_thread = Toot(status, timestamp, config) + toot_thread.run() + except InitException as e: + logging.error('InitException: {}'.format(e)) + except Exception as ex: + logging.debug('Exception: {}'.format(ex)) + else: logging.debug('Toot is set to false') + logging.debug('Send {} back'.format(raw_data)) + Connection.send(answer) + Connection.close() except KeyboardInterrupt: logging.info('Keyboard interrupt received') - sys.exit(255) - except Exception as e: - logging.error('{}'.format(e)) - finally: if MySocket: MySocket.close() logging.debug('TCP socket closed') + sys.exit(255) + except Exception as e: + logging.error('{}'.format(e)) + if MySocket: + MySocket.close() + logging.debug('TCP socket closed') + sys.exit(254) if __name__ == '__main__':