From: Christian Heller Date: Mon, 2 Nov 2020 03:27:56 +0000 (+0100) Subject: De-hardcode cert and key file for PlomSocketSSL. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/copy_free?a=commitdiff_plain;h=b747db93005261dbf46c657099be0bf687ad2ce3;p=plomrogue2-experiments De-hardcode cert and key file for PlomSocketSSL. --- diff --git a/new2/plomrogue/io.py b/new2/plomrogue/io.py index 5450c34..8e0f9bf 100644 --- a/new2/plomrogue/io.py +++ b/new2/plomrogue/io.py @@ -1,5 +1,6 @@ import queue import threading +import inspect @@ -30,12 +31,15 @@ class GameIO(): c = threading.Thread(target=self.loop, args=(self.queue,)) c.start() - def start_server(self, port, server_class): + def start_server(self, port, server_class, certfile=None, keyfile=None): """Start server of server_class in talk with game loop. The server communicates with the game loop via self.queue. """ - server = server_class(self.queue, port) + if 'certfile' in list(inspect.signature(server_class.__init__).parameters): + server = server_class(self.queue, port, certfile=certfile, keyfile=keyfile) + else: + server = server_class(self.queue, port) self.servers += [server] c = threading.Thread(target=server.serve_forever) c.start() @@ -47,7 +51,6 @@ class GameIO(): signature will only be called if god_mode is set. """ - from inspect import signature from plomrogue.errors import GameError, ArgError, PlayError from plomrogue.misc import quote @@ -62,7 +65,7 @@ class GameIO(): if command is None: answer(connection_id, 'UNHANDLED_INPUT') else: - if 'connection_id' in list(signature(command).parameters): + if 'connection_id' in list(inspect.signature(command).parameters): command(*args, connection_id=connection_id) elif god_mode: command(*args) diff --git a/new2/plomrogue/io_tcp.py b/new2/plomrogue/io_tcp.py index 78e43f5..5dd2508 100644 --- a/new2/plomrogue/io_tcp.py +++ b/new2/plomrogue/io_tcp.py @@ -102,14 +102,12 @@ class PlomSocket: class PlomSocketSSL(PlomSocket): - def __init__(self, *args, server_side=False, **kwargs): + def __init__(self, *args, server_side=False, certfile=None, keyfile=None, **kwargs): import ssl - print('DEBUG', args, kwargs) super().__init__(*args, **kwargs) if server_side: self.socket = ssl.wrap_socket(self.socket, server_side=True, - certfile="server.pem", - keyfile="key.pem") + certfile=certfile, keyfile=keyfile) else: self.socket = ssl.wrap_socket(self.socket) @@ -150,7 +148,9 @@ class IO_Handler(socketserver.BaseRequestHandler): import queue import threading if self.socket_class == PlomSocketSSL: - plom_socket = self.socket_class(self.request, server_side=True) + plom_socket = self.socket_class(self.request, server_side=True, + certfile=self.server.certfile, + keyfile=self.server.keyfile) else: plom_socket = self.socket_class(self.request) print('CONNECTION FROM:', str(self.client_address)) @@ -202,5 +202,7 @@ class PlomTCPServer(socketserver.ThreadingTCPServer): class PlomTCPServerSSL(PlomTCPServer): - def __init__(self, *args, **kwargs): + def __init__(self, *args, certfile=None, keyfile=None, **kwargs): + self.certfile = certfile + self.keyfile = keyfile super().__init__(*args, host='0.0.0.0', io_handler=IO_HandlerSSL, **kwargs)