From e3976794d917f90f83e3c5d3140d3ff49659e4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 28 Jul 2022 13:22:02 +0200 Subject: [PATCH] some minor refactors & used sys.intern as key for rest api --- server/src/uds/REST/dispatcher.py | 13 +++++++------ tunnel-server/src/uds_tunnel/processes.py | 15 +++++++++++---- tunnel-server/src/uds_tunnel/proxy.py | 2 -- tunnel-server/src/uds_tunnel/stats.py | 2 +- tunnel-server/src/uds_tunnel/tunnel.py | 3 ++- tunnel-server/src/udstunnel.py | 6 +++--- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/server/src/uds/REST/dispatcher.py b/server/src/uds/REST/dispatcher.py index 3e5c1545..b5e60db7 100644 --- a/server/src/uds/REST/dispatcher.py +++ b/server/src/uds/REST/dispatcher.py @@ -30,7 +30,7 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging -from re import M +import sys import typing from django import http @@ -200,7 +200,7 @@ class Dispatcher(View): """ if not type_.name: - name = type_.__name__.lower() + name = sys.intern(type_.__name__.lower()) else: name = type_.name @@ -210,15 +210,16 @@ class Dispatcher(View): if type_.path: logger.info('Path: /%s/%s', type_.path, name) for k in type_.path.split('/'): + intern_k = sys.intern(k) if k not in service_node: - service_node[k] = {'': None} - service_node = service_node[k] + service_node[intern_k] = {'': None} + service_node = service_node[intern_k] else: logger.info('Path: /%s', name) if name not in service_node: service_node[name] = {'': None} - service_node[name][''] = type_ + service_node[name][sys.intern('')] = type_ # Initializes the dispatchers @staticmethod @@ -237,7 +238,7 @@ class Dispatcher(View): Dispatcher.registerClass, Handler, modName=modName, - checker=lambda x: not x.__subclasses__(), + checker=lambda x: not x.__subclasses__(), # only register if final class, no inherited classes packageName='methods', ) diff --git a/tunnel-server/src/uds_tunnel/processes.py b/tunnel-server/src/uds_tunnel/processes.py index d7b6bcca..6b8c03a7 100644 --- a/tunnel-server/src/uds_tunnel/processes.py +++ b/tunnel-server/src/uds_tunnel/processes.py @@ -18,7 +18,8 @@ ProcessType = typing.Callable[ typing.Coroutine[typing.Any, None, None], ] -NO_CPU_PERCENT = 1000001.0 +NO_CPU_PERCENT: float = 1000001.0 + class Processes: """ @@ -51,7 +52,9 @@ class Processes: ) task.start() logger.debug('ADD CHILD PID: %s', task.pid) - self.children.append((own_conn, task, psutil.Process(task.pid))) + self.children.append( + (typing.cast('Connection', own_conn), task, psutil.Process(task.pid)) + ) def best_child(self) -> 'Connection': best: typing.Tuple[float, 'Connection'] = (NO_CPU_PERCENT, self.children[0][0]) @@ -93,7 +96,11 @@ class Processes: if i not in missingProcesses ] # Now add new children - for _ in missingProcesses: # wee need to add as many as we removed, that is the len of missingProcesses + for ( + _ + ) in ( + missingProcesses + ): # wee need to add as many as we removed, that is the len of missingProcesses self.add_child_pid() # Recheck best if all child were missing @@ -102,7 +109,7 @@ class Processes: return best[1] - def stop(self): + def stop(self) -> None: # Try to stop running childs for i in self.children: try: diff --git a/tunnel-server/src/uds_tunnel/proxy.py b/tunnel-server/src/uds_tunnel/proxy.py index fdf2c8df..570e905c 100644 --- a/tunnel-server/src/uds_tunnel/proxy.py +++ b/tunnel-server/src/uds_tunnel/proxy.py @@ -64,9 +64,7 @@ class Proxy: logger.error('Proxy error from %s: %s', addr, e) async def proxy(self, source: socket.socket, context: 'ssl.SSLContext') -> None: - loop = asyncio.get_event_loop() - # Handshake correct in this point, upgrade the connection to TSL and let # the protocol controller do the rest diff --git a/tunnel-server/src/uds_tunnel/stats.py b/tunnel-server/src/uds_tunnel/stats.py index b35e42c2..0fd9e3ef 100644 --- a/tunnel-server/src/uds_tunnel/stats.py +++ b/tunnel-server/src/uds_tunnel/stats.py @@ -143,7 +143,7 @@ async def getServerStats(detailed: bool = False) -> None: # Context for local connection (ignores cert hostname) context = ssl.create_default_context() context.check_hostname = False - context.verify_mode = ssl.CERT_NONE # For ServerStats, do not checks certificate + context.verify_mode = ssl.CERT_NONE # For ServerStats, does not checks certificate try: host = cfg.listen_address if cfg.listen_address != '0.0.0.0' else 'localhost' diff --git a/tunnel-server/src/uds_tunnel/tunnel.py b/tunnel-server/src/uds_tunnel/tunnel.py index 903a773d..e3c6f9b5 100644 --- a/tunnel-server/src/uds_tunnel/tunnel.py +++ b/tunnel-server/src/uds_tunnel/tunnel.py @@ -23,7 +23,7 @@ class TunnelProtocol(asyncio.Protocol): transport: 'asyncio.transports.Transport' other_side: 'TunnelProtocol' # Current state - runner: typing.Any + runner: typing.Any # In fact, typing.Callable[[bytes], None], but mypy complains on its check # Command buffer cmd: bytes # Ticket @@ -117,6 +117,7 @@ class TunnelProtocol(asyncio.Protocol): self.close_connection() loop.create_task(open_other_side()) + # From now, proxy connection self.runner = self.do_proxy def process_stats(self, full: bool) -> None: diff --git a/tunnel-server/src/udstunnel.py b/tunnel-server/src/udstunnel.py index 112ce00a..3bf6c34a 100755 --- a/tunnel-server/src/udstunnel.py +++ b/tunnel-server/src/udstunnel.py @@ -57,7 +57,7 @@ logger = logging.getLogger(__name__) do_stop = False -def stop_signal(signum, frame): +def stop_signal(signum: int, frame: typing.Any) -> None: global do_stop do_stop = True logger.debug('SIGNAL %s, frame: %s', signum, frame) @@ -85,7 +85,7 @@ def setup_log(cfg: config.ConfigurationType) -> None: # Setup basic logging log = logging.getLogger() log.setLevel(cfg.log_level) - handler = logging.StreamHandler(sys.stdout) + handler = logging.StreamHandler(sys.stderr) handler.setLevel(cfg.log_level) formatter = logging.Formatter( '%(levelname)s - %(message)s' @@ -169,7 +169,7 @@ def process_connection( client.close() -def tunnel_main(): +def tunnel_main() -> None: cfg = config.read() # Try to bind to port as running user