From f44bcec91f8c92853116c6c17d71bc3666ca2e37 Mon Sep 17 00:00:00 2001 From: cc Date: Fri, 18 Oct 2024 21:37:27 +0200 Subject: [PATCH] use random UI port --- frida_tools/tracer.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/frida_tools/tracer.py b/frida_tools/tracer.py index b5ded91..3fe8044 100644 --- a/frida_tools/tracer.py +++ b/frida_tools/tracer.py @@ -5,7 +5,6 @@ import binascii import codecs import email.utils -import errno import gzip import http import mimetypes @@ -43,7 +42,7 @@ class TracerApplication(ConsoleApplication, UI): def __init__(self) -> None: super().__init__(await_ctrl_c) self._handlers = OrderedDict() - self._ui_port = 1337 + self._ui_port = 0 self._ui_zip = ZipFile(Path(__file__).parent / "tracer_ui.zip", "r") self._ui_socket_handlers: Set[UISocketHandler] = set() self._ui_worker = None @@ -306,21 +305,14 @@ def _run_ui_server(self): async def _handle_ui_requests(self): self._asyncio_loop = asyncio.get_running_loop() - while True: - try: - async with websockets.asyncio.server.serve( - self._handle_websocket_connection, - "localhost", - self._ui_port, - process_request=self._handle_asset_request, - ): - await asyncio.get_running_loop().create_future() - return - except OSError as e: - if e.errno == errno.EADDRINUSE: - self._ui_port += 1 - else: - raise + async with websockets.asyncio.server.serve( + self._handle_websocket_connection, + "localhost", + process_request=self._handle_asset_request, + ) as server: + self._ui_port = server.sockets[0].getsockname()[1] + await asyncio.get_running_loop().create_future() + return async def _handle_websocket_connection(self, websocket: websockets.asyncio.server.ServerConnection): if self._tracer is None: