From f75268bc56931e0814b48fadabd463834cdcd5bc Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 14 Feb 2020 15:35:56 +0200 Subject: [PATCH] Set close_reason for exceptions in network.tls --- src/network/advanceddispatcher.py | 1 + src/network/tls.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 982be819..8df557ad 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -31,6 +31,7 @@ class AdvancedDispatcher(asyncore.dispatcher): def __init__(self, sock=None): if not hasattr(self, '_map'): asyncore.dispatcher.__init__(self, sock) + self.close_reason = None self.read_buf = bytearray() self.write_buf = bytearray() self.state = "init" diff --git a/src/network/tls.py b/src/network/tls.py index 7c16b9b5..e326fa32 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -154,12 +154,13 @@ class TLSDispatcher(AdvancedDispatcher): except AttributeError: return AdvancedDispatcher.handle_read(self) except ssl.SSLError as err: + self.close_reason = "SSL Error in handle_read" if err.errno == ssl.SSL_ERROR_WANT_READ: return elif err.errno in _DISCONNECTED_SSL: self.handle_close() return - logger.info("SSL Error: %s", str(err)) + logger.info("SSL Error: %s", err) self.handle_close() return @@ -184,12 +185,13 @@ class TLSDispatcher(AdvancedDispatcher): except AttributeError: return AdvancedDispatcher.handle_write(self) except ssl.SSLError as err: + self.close_reason = "SSL Error in handle_write" if err.errno == ssl.SSL_ERROR_WANT_WRITE: return 0 elif err.errno in _DISCONNECTED_SSL: self.handle_close() return 0 - logger.info("SSL Error: %s", str(err)) + logger.info("SSL Error: %s", err) self.handle_close() return @@ -203,8 +205,8 @@ class TLSDispatcher(AdvancedDispatcher): logger.debug("handshaking (internal)") self.sslSocket.do_handshake() except ssl.SSLError as err: - logger.debug( - '%s:%i: handshake fail', self.destination.host, self.destination.port) + self.close_reason = "SSL Error in tls_handshake" + logger.info("%s:%i: handshake fail", *self.destination) self.want_read = self.want_write = False if err.args[0] == ssl.SSL_ERROR_WANT_READ: logger.debug("want read") @@ -217,6 +219,7 @@ class TLSDispatcher(AdvancedDispatcher): except socket.error as err: # pylint: disable=protected-access if err.errno in asyncore._DISCONNECTED: + self.close_reason = "socket.error in tls_handshake" self.handle_close() else: raise