[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/maint-1.1] chg: stem: Add function to connect or start tor



commit f7847ed28cab55bd1260d307b42c4ef59772937a
Author: juga0 <juga@xxxxxxxxxx>
Date:   Wed Dec 16 15:51:55 2020 +0000

    chg: stem: Add function to connect or start tor
    
    Move initialization via existing socket to this new function and start
    tor only when it fails.
    
    Closes: #33150.
---
 sbws/core/scanner.py          | 17 ++---------------
 sbws/util/stem.py             | 22 ++++++++++++++++------
 tests/integration/conftest.py |  4 ++--
 3 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/sbws/core/scanner.py b/sbws/core/scanner.py
index bcbeb47..312993a 100644
--- a/sbws/core/scanner.py
+++ b/sbws/core/scanner.py
@@ -679,21 +679,8 @@ def run_speedtest(args, conf):
 
     """
     global rd, pool, controller
-    controller = stem_utils.init_controller(conf)
-    if not controller:
-        controller = stem_utils.launch_tor(conf)
-    else:
-        log.warning(
-            'Is sbws already running? '
-            'We found an existing Tor process at %s. We are not going to '
-            'launch Tor, nor are we going to try to configure it to behave '
-            'like we expect. This might work okay, but it also might not. '
-            'If you experience problems, you should try letting sbws launch '
-            'Tor for itself. The ability to use an already running Tor only '
-            'exists for sbws developers. It is expected to be broken and may '
-            'even lead to messed up results.',
-            conf.getpath('tor', 'control_socket'))
-        time.sleep(15)
+
+    controller = stem_utils.launch_or_connect_to_tor(conf)
 
     # When there will be a refactor where conf is global, this can be removed
     # from here.
diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index 169acda..5605c29 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -6,7 +6,6 @@ from stem import (SocketError, InvalidRequest, UnsatisfiableRequest,
                   ProtocolError, SocketClosed)
 from stem.connection import IncorrectSocketType
 import stem.process
-from configparser import ConfigParser
 from threading import RLock
 import copy
 import logging
@@ -68,10 +67,8 @@ def init_controller(conf):
         control_port = int(control_port)
         # If it can not connect, the program will exit here
         c = _init_controller_port(control_port)
-    else:
-        c = _init_controller_socket(
-            socket=conf.getpath('tor', 'control_socket')
-        )
+    # There is no configuration for external control socket, therefore do not
+    # attempt to connect to the control socket.
     return c
 
 
@@ -190,7 +187,6 @@ def set_torrc_options_can_fail(controller):
 
 
 def launch_tor(conf):
-    assert isinstance(conf, ConfigParser)
     os.makedirs(conf.getpath('tor', 'datadir'), mode=0o700, exist_ok=True)
     os.makedirs(conf.getpath('tor', 'log'), exist_ok=True)
     os.makedirs(conf.getpath('tor', 'run_dpath'), mode=0o700, exist_ok=True)
@@ -216,6 +212,8 @@ def launch_tor(conf):
     torrc = parse_user_torrc_config(torrc, conf['tor']['extra_lines'])
     # Finally launch Tor
     try:
+        # If there is already a tor process running with the same control
+        # socket, this will exit here.
         stem.process.launch_tor_with_config(
             torrc, init_msg_handler=log.debug, take_ownership=True)
     except Exception as e:
@@ -223,6 +221,18 @@ def launch_tor(conf):
     log.info("Started own tor.")
     # And return a controller to it
     cont = _init_controller_socket(conf.getpath('tor', 'control_socket'))
+    # In the case it was not possible to connect to own tor socket.
+    if not cont:
+        fail_hard('Could not connect to own tor control socket.')
+    return cont
+
+
+def launch_or_connect_to_tor(conf):
+    # If connecting to an existing controller, there is no need to configure
+    # own tor.
+    cont = init_controller(conf)
+    if not cont:
+        cont = launch_tor(conf)
     # Set options that can fail at runtime
     set_torrc_options_can_fail(cont)
     # Set runtime options
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
index afd7256..2c0e675 100644
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -7,7 +7,7 @@ from sbws.lib.circuitbuilder import GapsCircuitBuilder as CB
 from sbws.lib.destination import DestinationList
 from sbws.lib.relaylist import RelayList
 from sbws.util.config import _get_default_config
-from sbws.util.stem import launch_tor
+from sbws.util.stem import launch_or_connect_to_tor
 
 
 class _PseudoArguments(argparse.Namespace):
@@ -91,7 +91,7 @@ SafeLogging 0
 
 @pytest.fixture(scope='session')
 def persistent_launch_tor(conf):
-    cont = launch_tor(conf)
+    cont = launch_or_connect_to_tor(conf)
     return cont
 
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits