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

[tor-commits] [sbws/master] stem: refactor, move setting runtime torrc options



commit 1d836ec5b67fdac0f43de383267dc6ac28265cf8
Author: juga0 <juga@xxxxxxxxxx>
Date:   Fri Dec 7 10:18:37 2018 +0000

    stem: refactor, move setting runtime torrc options
    
    to a function.
    Replace comment by docstring.
    Add an integration test to check the option is set.
---
 sbws/globals.py                     |  5 +++++
 sbws/util/stem.py                   | 32 ++++++++++++++++++++------------
 tests/integration/util/test_stem.py |  6 ++++++
 3 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/sbws/globals.py b/sbws/globals.py
index 60e56f5..c6adc44 100644
--- a/sbws/globals.py
+++ b/sbws/globals.py
@@ -33,6 +33,11 @@ TORRC_STARTING_POINT = {
     'ProtocolWarnings': '1',
 }
 
+TORRC_RUNTIME_OPTIONS = {
+    '__DisablePredictedCircuits': '1',
+    '__LeaveStreamsUnattached': '1',
+}
+
 PKG_DIR = os.path.abspath(os.path.dirname(__file__))
 DEFAULT_CONFIG_PATH = os.path.join(PKG_DIR, 'config.default.ini')
 DEFAULT_LOG_CONFIG_PATH = os.path.join(PKG_DIR, 'config.log.default.ini')
diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index 9b3dc0d..ead812b 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -12,7 +12,7 @@ import copy
 import logging
 import os
 from sbws.globals import fail_hard
-from sbws.globals import TORRC_STARTING_POINT
+from sbws.globals import TORRC_STARTING_POINT, TORRC_RUNTIME_OPTIONS
 
 log = logging.getLogger(__name__)
 stream_building_lock = RLock()
@@ -74,8 +74,12 @@ def init_controller(port=None, path=None, set_custom_stream_settings=True):
             return None, 'Unable to reach tor on control socket'
     assert c is not None
     if set_custom_stream_settings:
-        c.set_conf('__DisablePredictedCircuits', '1')
-        c.set_conf('__LeaveStreamsUnattached', '1')
+        # These options are also set in launch_tor.
+        # In a future refactor they could be set in the case they are not
+        # already in the running instance. This way the controller_port
+        # could also be used.
+        set_torrc_options_can_fail(c)
+        set_torrc_runtime_options(c)
     return c, ''
 
 
@@ -165,6 +169,16 @@ def parse_user_torrc_config(torrc, torrc_text):
     return torrc_dict
 
 
+def set_torrc_runtime_options(controller):
+    """Set torrc options at runtime."""
+    try:
+        controller.set_options(TORRC_RUNTIME_OPTIONS)
+    except (ControllerError, InvalidArguments, InvalidRequest) as e:
+        log.exception("Error trying to launch tor: %s. "
+                      "Maybe the tor directory is being used by other "
+                      "sbws instance?", e)
+        exit(1)
+
 def launch_tor(conf):
     assert isinstance(conf, ConfigParser)
     os.makedirs(conf.getpath('tor', 'datadir'), mode=0o700, exist_ok=True)
@@ -197,15 +211,9 @@ def launch_tor(conf):
         fail_hard('Error trying to launch tor: %s', e)
     # And return a controller to it
     cont = _init_controller_socket(conf.getpath('tor', 'control_socket'))
-    # Because we build things by hand and can't set these before Tor bootstraps
-    try:
-        cont.set_conf('__DisablePredictedCircuits', '1')
-        cont.set_conf('__LeaveStreamsUnattached', '1')
-    except (ControllerError, InvalidArguments, InvalidRequest) as e:
-        log.exception("Error trying to launch tor: %s. "
-                      "Maybe the tor directory is being used by other "
-                      "sbws instance?", e)
-        exit(1)
+
+    set_torrc_runtime_options(cont)
+
     log.info('Started and connected to Tor %s via %s', cont.get_version(),
              conf.getpath('tor', 'control_socket'))
     return cont
diff --git a/tests/integration/util/test_stem.py b/tests/integration/util/test_stem.py
index 291f485..33fee81 100644
--- a/tests/integration/util/test_stem.py
+++ b/tests/integration/util/test_stem.py
@@ -4,3 +4,9 @@ import sbws.util.stem as stem_utils
 def test_launch_and_okay(persistent_launch_tor):
     cont = persistent_launch_tor
     assert stem_utils.is_bootstrapped(cont)
+
+
+def test_set_torrc_runtime_option_succesful(persistent_launch_tor):
+    controller = persistent_launch_tor
+    runtime_options = controller.get_conf_map(['__LeaveStreamsUnattached'])
+    assert runtime_options == {'__LeaveStreamsUnattached': ['1']}



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