enable ssl
This commit is contained in:
parent
02e4569634
commit
2fb661750a
@ -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,7 +73,10 @@ class JsonWebsocketServer(object):
|
|||||||
|
|
||||||
await connection.run()
|
await connection.run()
|
||||||
|
|
||||||
start_server = websockets.serve(main, self._host, self._port)
|
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)
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(start_server)
|
asyncio.get_event_loop().run_until_complete(start_server)
|
||||||
asyncio.get_event_loop().run_forever()
|
asyncio.get_event_loop().run_forever()
|
||||||
|
@ -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()
|
Loading…
Reference in New Issue
Block a user