Skip to content

Instantly share code, notes, and snippets.

@GaretJax
Last active October 14, 2015 16:42
Show Gist options
  • Save GaretJax/66227827d17cb804420f to your computer and use it in GitHub Desktop.
Save GaretJax/66227827d17cb804420f to your computer and use it in GitHub Desktop.
reactor.listenSSL(
port=443,
factory=server_factory,
contextFactory=SNICallbackSSLFactory(RedisCertsStore()),
)
@implementer(IOpenSSLServerConnectionCreator)
class SNICallbackSSLFactory(object):
def __init__(self, certs_store):
self.certs_store = certs_store
def _buildContext(self):
context = ssl.Context(ssl.SSLv23_METHOD)
context.set_options(ssl.OP_NO_SSLv2)
return context
@defer.inlineCallbacks
def _servername_received(self, connection):
hostname = connection.get_servername()
# connection.pause()
cert, key = yield self.certs_store.get_cert_and_key(hostname)
context = self._buildContext()
context.use_privatekey(key)
context.use_certificate(cert)
connection.set_context(context)
# connection.resume()
def serverConnectionForTLS(self, tlsProtocol):
context = self._buildContext()
context.set_tlsext_servername_callback(self._servername_received)
return ssl.Connection(context, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment