diff options
author | mathieui <mathieui@mathieui.net> | 2018-08-08 23:35:33 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2018-08-08 23:35:33 +0200 |
commit | 490f15b8fc9e5d205fd75cf4ce13cd2b3841ddf1 (patch) | |
tree | 71539783faf5f3ebc49ebaf386fd9478946e4cab | |
parent | 62661ee04f9df94b0a79be077f7d38fb313ceffd (diff) | |
download | slixmpp-490f15b8fc9e5d205fd75cf4ce13cd2b3841ddf1.tar.gz slixmpp-490f15b8fc9e5d205fd75cf4ce13cd2b3841ddf1.tar.bz2 slixmpp-490f15b8fc9e5d205fd75cf4ce13cd2b3841ddf1.tar.xz slixmpp-490f15b8fc9e5d205fd75cf4ce13cd2b3841ddf1.zip |
Fix compatibility with python 3.5 and 3.6
which do not have loop.start_tls and require the old ssl implementation.
-rw-r--r-- | slixmpp/__init__.py | 4 | ||||
-rw-r--r-- | slixmpp/xmlstream/xmlstream.py | 17 |
2 files changed, 19 insertions, 2 deletions
diff --git a/slixmpp/__init__.py b/slixmpp/__init__.py index 0730cc60..caa39db7 100644 --- a/slixmpp/__init__.py +++ b/slixmpp/__init__.py @@ -9,6 +9,10 @@ import logging logging.getLogger(__name__).addHandler(logging.NullHandler()) +import asyncio +# Required for python < 3.7 to use the old ssl implementation +# and manage to do starttls as an unintended side effect +asyncio.sslproto._is_sslproto_available = lambda: False from slixmpp.stanza import Message, Presence, Iq from slixmpp.jid import JID, InvalidJID diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 1fa07b0c..0367db02 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -539,7 +539,17 @@ class XMLStream(asyncio.BaseProtocol): self.event_when_connected = "tls_success" ssl_context = self.get_ssl_context() try: - transp = await self.loop.start_tls(self.transport, self, ssl_context) + if hasattr(self.loop, 'start_tls'): + transp = await self.loop.start_tls(self.transport, + self, ssl_context) + # Python < 3.7 + else: + transp, _ = await self.loop.create_connection( + lambda: self, + ssl=self.ssl_context, + sock=self.socket, + server_hostname=self.default_domain + ) except ssl.SSLError as e: log.debug('SSL: Unable to connect', exc_info=True) log.error('CERT: Invalid certificate trust chain.') @@ -551,7 +561,10 @@ class XMLStream(asyncio.BaseProtocol): der_cert = transp.get_extra_info("ssl_object").getpeercert(True) pem_cert = ssl.DER_cert_to_PEM_cert(der_cert) self.event('ssl_cert', pem_cert) - self.connection_made(transp) + # If we use the builtin start_tls, the connection_made() protocol + # method is not called automatically + if hasattr(self.loop, 'start_tls'): + self.connection_made(transp) return True def _start_keepalive(self, event): |