enable ssl

This commit is contained in:
Jonas Weinz 2021-06-17 16:12:16 +02:00
parent 02e4569634
commit 2fb661750a
2 changed files with 24 additions and 5 deletions

View File

@ -59,10 +59,11 @@ class JsonWebsocketConnection(object):
class JsonWebsocketServer(object): class JsonWebsocketServer(object):
def __init__(self, handler_class: JsonWebsocketConnection, host:str = 'localhost', port:int = 8765): def __init__(self, handler_class: JsonWebsocketConnection, host:str = 'localhost', port:int = 8765, ssl_context = None):
self._host = host self._host = host
self._port = port self._port = port
self._handler_class = handler_class self._handler_class = handler_class
self._ssl_context = ssl_context
def run(self): def run(self):
async def main(websocket: websockets.WebSocketServerProtocol, async def main(websocket: websockets.WebSocketServerProtocol,
@ -72,6 +73,9 @@ class JsonWebsocketServer(object):
await connection.run() await connection.run()
if (self._ssl_context is not None):
start_server = websockets.serve(main, self._host, self._port, ssl=self._ssl_context)
else:
start_server = websockets.serve(main, self._host, self._port) start_server = websockets.serve(main, self._host, self._port)
asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_until_complete(start_server)

View File

@ -1,7 +1,22 @@
import ssl
import logging
from . import json_websockets from . import json_websockets
from . import crossword_connection from . import crossword_connection
try:
cert_file = "fullchain.pem"
key_file = "privkey.pem"
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(cert_file, keyfile=key_file)
except Exception as e:
logging.error("no ssl context available")
ssl_context = None
server = json_websockets.JsonWebsocketServer( server = json_websockets.JsonWebsocketServer(
crossword_connection.CrosswordConnection crossword_connection.CrosswordConnection, ssl_context=ssl_context
) )
server.run() server.run()