From 6b6f20a032eb31d747366c8bc270ca81731eaf6a Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 4 Sep 2026 17:09:24 +0200 Subject: Fixed pre-commit issues --- ttun_server/websockets.py | 60 +++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 28 deletions(-) (limited to 'ttun_server/websockets.py') diff --git a/ttun_server/websockets.py b/ttun_server/websockets.py index 88d6cac..a3cbc5f 100644 --- a/ttun_server/websockets.py +++ b/ttun_server/websockets.py @@ -16,24 +16,25 @@ from ttun_server.types import ( ) logger = logging.getLogger(__name__) -logger.setLevel('DEBUG') +logger.setLevel("DEBUG") + async def assert_compatible_version(websocket: WebSocket, config: Config) -> None: - client_version = config.get('version', '1.0.0') - logger.debug('client_version %s', client_version) + client_version = config.get("version", "1.0.0") + logger.debug("client_version %s", client_version) - if 'git' not in client_version and ttun_server.__version__ != 'development': - [client_major, *_] = [int(i) for i in client_version.split('.')[:3]] - [server_major, *_] = [int(i) for i in ttun_server.__version__.split('.')] + if "git" not in client_version and ttun_server.__version__ != "development": + [client_major, *_] = [int(i) for i in client_version.split(".")[:3]] + [server_major, *_] = [int(i) for i in ttun_server.__version__.split(".")] if client_major < server_major: - await websocket.close(4000, 'Your client is too old') + await websocket.close(4000, "Your client is too old") if client_major > server_major: - await websocket.close(4001, 'Your client is too new') + await websocket.close(4001, "Your client is too new") -@requires(['authenticated', 'token'] if conf.ENABLE_AUTH else []) +@requires(["authenticated", "token"] if conf.ENABLE_AUTH else []) async def tunnel(websocket: WebSocket) -> None: request_tasks: dict[str, asyncio.Task] = {} proxy_queues: dict[str, ProxyQueue] = {} @@ -43,36 +44,39 @@ async def tunnel(websocket: WebSocket) -> None: await assert_compatible_version(websocket, config) - if 'subdomains' not in config: - config['subdomains'] = [config['subdomain']] - elif config['subdomains'] is None: - config['subdomains'] = [None] + if "subdomains" not in config: + config["subdomains"] = [config["subdomain"]] + elif config["subdomains"] is None: + config["subdomains"] = [None] - for i, subdomain in enumerate(config['subdomains']): + for i, subdomain in enumerate(config["subdomains"]): if subdomain is None or await ProxyQueue.has_connection(subdomain): - config['subdomains'][i] = uuid4().hex + config["subdomains"][i] = uuid4().hex - for subdomain in config['subdomains']: + for subdomain in config["subdomains"]: proxy_queues[subdomain] = await ProxyQueue.create_for_identifier(subdomain) - hostname = os.environ.get('TUNNEL_DOMAIN') - protocol = 'https' if os.environ.get('SECURE', False) else 'http' + hostname = os.environ.get("TUNNEL_DOMAIN") + protocol = "https" if os.environ.get("SECURE") else "http" urls = [ - f'{protocol}://{subdomain}.{hostname}' - for subdomain in config['subdomains'] + f"{protocol}://{subdomain}.{hostname}" for subdomain in config["subdomains"] ] - await websocket.send_json({ - 'url': urls[0], - 'urls': urls, - }) + await websocket.send_json( + { + "url": urls[0], + "urls": urls, + } + ) async def handle_requests(subdomain: str) -> None: while request := await proxy_queues[subdomain].dequeue(): - asyncio.create_task(websocket.send_json(request), name=request['identifier']) + asyncio.create_task( + websocket.send_json(request), name=request["identifier"] + ) - for subdomain in config['subdomains']: + for subdomain in config["subdomains"]: request_tasks[subdomain] = asyncio.create_task( handle_requests(subdomain), name=subdomain ) @@ -81,8 +85,8 @@ async def tunnel(websocket: WebSocket) -> None: while True: data: Message = await websocket.receive_json() try: - data['type'] = MessageType(data['type']).value - response_queue = await ProxyQueue.get_for_identifier(data['identifier']) + data["type"] = MessageType(data["type"]).value + response_queue = await ProxyQueue.get_for_identifier(data["identifier"]) await response_queue.enqueue(data) except AssertionError: pass -- cgit v1.3