Bug28692 #316
Bug28692 #316
Changes from 3 commits
aef7a96
a88ad8c
0fe78f9
b57c5f4
b33f40f
a60950f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from collections import OrderedDict | ||
|
||
from stem.control import (Controller, Listener) | ||
from stem import (SocketError, InvalidRequest, UnsatisfiableRequest, | ||
OperationFailed, ControllerError, InvalidArguments, | ||
|
@@ -16,6 +18,9 @@ | |
stream_building_lock = RLock() | ||
|
||
|
||
TORRC_OPTIONS_CAN_FAIL = OrderedDict({'ConnectionPadding': '0'}) | ||
|
||
|
||
def attach_stream_to_circuit_listener(controller, circ_id): | ||
''' Returns a function that should be given to add_event_listener(). It | ||
looks for newly created streams and attaches them to the given circ_id ''' | ||
|
@@ -117,6 +122,33 @@ def _init_controller_socket(socket): | |
return c | ||
|
||
|
||
def set_torrc_runtime_options(cont): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please call this function on line 74 as well, to remove duplicate code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i forgot |
||
"""Set torrc options at runtime.""" | ||
try: | ||
cont.set_conf('__DisablePredictedCircuits', '1') | ||
cont.set_conf('__LeaveStreamsUnattached', '1') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you refactor these options into a dictionary constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my initial idea (https://trac.torproject.org/projects/tor/ticket/28692#comment:8):
|
||
# Only the first option that fails will be logged here. | ||
# Just log stem's exceptions. | ||
except (ControllerError, InvalidRequest, InvalidArguments) as e: | ||
log.exception(e) | ||
exit(1) | ||
|
||
|
||
def set_torrc_options_can_fail(controller): | ||
"""Set options that can fail, at runtime. | ||
They can be set at launch, but since the may fail because they are not | ||
supported in some Tor versions, it's easier to try one by one at runtime | ||
and ignore the ones that fail. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: extra newline? |
||
""" | ||
for k, v in TORRC_OPTIONS_CAN_FAIL.items(): | ||
try: | ||
controller.set_conf(k, v) | ||
except InvalidArguments as error: | ||
log.debug('Ignoring option not supported by this Tor version. %s', | ||
error) | ||
|
||
|
||
def launch_tor(conf): | ||
assert isinstance(conf, ConfigParser) | ||
os.makedirs(conf.getpath('tor', 'datadir'), mode=0o700, exist_ok=True) | ||
|
@@ -194,15 +226,11 @@ 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 options that can fail at runtime | ||
set_torrc_options_can_fail(cont) | ||
# Set runtime options | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put all the options in the same place.
You could put this constant in globals.py after TORRC_STARTING_POINT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my initial idea (https://trac.torproject.org/projects/tor/ticket/28692#comment:8)