From 72b5602fb2227f7cafcd30a51a69c72f2b7c6910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Setni=C4=8Dka?= Date: Fri, 7 Apr 2023 17:00:01 +0200 Subject: [PATCH] TorRunner: Ensure SocksPort != ControlPort --- uldlib/torrunner.py | 7 +++++-- uldlib/utils.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/uldlib/torrunner.py b/uldlib/torrunner.py index 2839c84..3d5888f 100644 --- a/uldlib/torrunner.py +++ b/uldlib/torrunner.py @@ -4,9 +4,12 @@ import stem.control from uldlib.utils import get_available_port +sockPort = get_available_port(9050) +controlPort = get_available_port(9051, skip=[sockPort]) + TOR_CONFIG = { - 'SocksPort': f"{get_available_port(9050)}", - "ControlPort": f"{get_available_port(9051)}", + 'SocksPort': f"{sockPort}", + "ControlPort": f"{controlPort}", 'SocksListenAddress': '127.0.0.1', 'SocksPolicy': 'accept 127.0.0.1', 'CookieAuthentication': '1', diff --git a/uldlib/utils.py b/uldlib/utils.py index 0e2a0ef..92d2f77 100644 --- a/uldlib/utils.py +++ b/uldlib/utils.py @@ -1,5 +1,6 @@ import socket from enum import Enum +from typing import List from colors import colors @@ -33,10 +34,10 @@ def _is_port_available(port: int) -> bool: return s.connect_ex(('127.0.0.1', port)) != 0 -def get_available_port(given_port: int) -> int: +def get_available_port(given_port: int, skip: List[int] = []) -> int: max_attempts = 65535 while given_port < max_attempts: - if _is_port_available(given_port): + if _is_port_available(given_port) and given_port not in skip: return given_port given_port += 1 else: