From d5a11e7bc23f85eac01d560ae10c34e7190bb24a Mon Sep 17 00:00:00 2001 From: TurBoss Date: Mon, 3 Sep 2018 14:24:38 +0200 Subject: [PATCH 1/3] Twisted ssl This reverts commit b7f2517566120382c9f4a1736609efe9475b7f44. --- .gitignore | 2 ++ server.py | 5 +++-- tests/testsslclient.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/testsslclient.py diff --git a/.gitignore b/.gitignore index 43416c37..f3838eae 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ xmlrpc.log* /proxies.txt /dbconfig.py venv/ + +*.crt diff --git a/server.py b/server.py index 880ba90e..96f7a76c 100755 --- a/server.py +++ b/server.py @@ -8,7 +8,7 @@ import _thread import traceback, signal, socket, sys, logging -from twisted.internet import reactor +from twisted.internet import reactor, ssl sys.path.append("protocol") sys.path.append(".") @@ -56,7 +56,8 @@ def sighup(sig, frame): _root.init() try: - reactor.listenTCP(_root.port, twistedserver.ChatFactory(_root)) + # reactor.listenTCP(_root.port, twistedserver.ChatFactory(_root)) + reactor.listenSSL(8243, twistedserver.ChatFactory(_root), ssl.DefaultOpenSSLContextFactory('server.key', 'server.crt')) print('Started lobby server!') print('Connect the lobby client to') print(' public: %s:%d' %(_root.online_ip, _root.port)) diff --git a/tests/testsslclient.py b/tests/testsslclient.py new file mode 100644 index 00000000..86556379 --- /dev/null +++ b/tests/testsslclient.py @@ -0,0 +1,29 @@ +from twisted.internet import ssl, reactor +from twisted.internet.protocol import ClientFactory, Protocol + + +class EchoClient(Protocol): + def connectionMade(self): + print("connection made") + + def dataReceived(self, data): + print("Server said:", data) + self.transport.loseConnection() + + +class EchoClientFactory(ClientFactory): + protocol = EchoClient + + def clientConnectionFailed(self, connector, reason): + print("Connection failed - goodbye!") + reactor.stop() + + def clientConnectionLost(self, connector, reason): + print("Connection lost - goodbye!") + reactor.stop() + + +if __name__ == '__main__': + factory = EchoClientFactory() + reactor.connectSSL('localhost', 8243, factory, ssl.ClientContextFactory()) + reactor.run() From cda32d3a7e22119b9d7adc9a5953247e728658d6 Mon Sep 17 00:00:00 2001 From: TurBoss Date: Mon, 3 Sep 2018 21:44:50 +0200 Subject: [PATCH 2/3] listen on both ports --- .gitignore | 2 ++ DataHandler.py | 1 + certificate.py | 15 ++++++++++++--- server.py | 10 ++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index f3838eae..61dbd535 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ xmlrpc.log* venv/ *.crt + +keys/ diff --git a/DataHandler.py b/DataHandler.py index 718a7c67..df1993d8 100644 --- a/DataHandler.py +++ b/DataHandler.py @@ -30,6 +30,7 @@ def __init__(self): self.dispatcher = None self.console_buffer = [] self.port = 8200 + self.ssl_port = 8243 self.natport = self.port + 1 self.latestspringversion = '*' self.agreementfile = 'agreement.txt' diff --git a/certificate.py b/certificate.py index 0535bc50..cad0f176 100644 --- a/certificate.py +++ b/certificate.py @@ -34,9 +34,18 @@ def create_self_signed_cert(filename): cert.set_pubkey(k) cert.sign(k, 'sha1') + cert_file = crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode("UTF-8") + key_file = crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("UTF-8") + with open(filename, 'wt') as certfile: - certfile.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode("UTF-8")) - certfile.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("UTF-8")) + certfile.write(cert_file) + certfile.write(key_file) + + with open("keys/server.crt", 'wt') as crt: + crt.write(cert_file) + + with open("keys/server.key", 'wt') as key: + key.write(key_file) -#create_self_signed_cert("server.key") +# create_self_signed_cert("server.key") diff --git a/server.py b/server.py index 96f7a76c..8580d063 100755 --- a/server.py +++ b/server.py @@ -7,6 +7,8 @@ # thread was renamed to _thread in python 3 import _thread +from OpenSSL import SSL + import traceback, signal, socket, sys, logging from twisted.internet import reactor, ssl @@ -55,13 +57,17 @@ def sighup(sig, frame): _root.init() + try: - # reactor.listenTCP(_root.port, twistedserver.ChatFactory(_root)) - reactor.listenSSL(8243, twistedserver.ChatFactory(_root), ssl.DefaultOpenSSLContextFactory('server.key', 'server.crt')) + reactor.listenTCP(_root.port, twistedserver.ChatFactory(_root)) + reactor.listenSSL(_root.ssl_port, twistedserver.ChatFactory(_root), + ssl.DefaultOpenSSLContextFactory('keys/server.key', 'keys/server.crt')) print('Started lobby server!') print('Connect the lobby client to') print(' public: %s:%d' %(_root.online_ip, _root.port)) print(' private: %s:%d' %(_root.local_ip, _root.port)) + print(' public ssl: %s:%d' %(_root.online_ip, _root.ssl_port)) + print(' private ssl: %s:%d' %(_root.local_ip, _root.ssl_port)) reactor.run() except KeyboardInterrupt: From 6550a6ea3beeb94ee5b579d116511b819b26319b Mon Sep 17 00:00:00 2001 From: TurBoss Date: Mon, 14 Jan 2019 05:28:57 +0100 Subject: [PATCH 3/3] add requirement --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 468126b4..3bc4e88a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ mysqlclient==1.3.10 pyOpenSSL==18.0.0 SQLAlchemy==1.1.9 Twisted==17.1.0 +service_identity \ No newline at end of file