Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

Commit

Permalink
TorRunner: Ensure SocksPort != ControlPort
Browse files Browse the repository at this point in the history
  • Loading branch information
setnicka committed Apr 7, 2023
1 parent 19172be commit 72b5602
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions uldlib/torrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions uldlib/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
from enum import Enum
from typing import List

from colors import colors

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 72b5602

Please sign in to comment.