| 1 |
from twisted.internet import defer, endpoints, protocol, ssl, task |
| 2 |
from twisted.protocols.basic import LineReceiver |
| 3 |
from twisted.python.modules import getModule |
| 4 |
|
| 5 |
|
| 6 |
class TLSServer(LineReceiver): |
| 7 |
def lineReceived(self, line): |
| 8 |
print("received: ", line) |
| 9 |
if line == b"STARTTLS": |
| 10 |
print("-- Switching to TLS") |
| 11 |
self.sendLine(b"READY") |
| 12 |
self.transport.startTLS(self.factory.options) |
| 13 |
|
| 14 |
|
| 15 |
def main(reactor): |
| 16 |
certData = getModule(__name__).filePath.sibling("server.pem").getContent() |
| 17 |
cert = ssl.PrivateCertificate.loadPEM(certData) |
| 18 |
factory = protocol.Factory.forProtocol(TLSServer) |
| 19 |
factory.options = cert.options() |
| 20 |
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000) |
| 21 |
endpoint.listen(factory) |
| 22 |
return defer.Deferred() |
| 23 |
|
| 24 |
|
| 25 |
if __name__ == "__main__": |
| 26 |
import starttls_server |
| 27 |
|
| 28 |
task.react(starttls_server.main) |