Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Change tor config before tor is launched #85

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions bwscanner/attacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,36 @@ def connect_to_tor(launch_tor, circuit_build_timeout, tor_dir=None, control_port

if launch_tor:
log.info("Spawning a new Tor instance.")
tor = yield txtorcon.launch(reactor, data_directory=tor_dir)
# Create new TorConfig object
tor_config = txtorcon.TorConfig()
# Update tor config options from dictionary
for key, value in tor_options.items():
setattr(tor_config, key, value)
# Launch tor with config, in order to don't get CONF_CHANGED when
# updating options that can't be changed while tor is running.
tor = yield txtorcon.launch(reactor, data_directory=tor_dir,
_tor_config=tor_config)
else:
log.info("Trying to connect to a running Tor instance.")
if control_port:
endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", control_port)
else:
endpoint = None
tor = yield txtorcon.connect(reactor, endpoint)
# Get current tor config and update it with our options
# TODO: check whether CONF_CHANGED will happen here or not because
# FIXME: check that this is the recommended way to change config for
# a running tor, and whether the following is also possible
# tor._config = tor_config
# tor._config.save()
tor_config = yield tor.get_config()
for key, value in tor_options.items():
setattr(tor_config, key, value)
yield tor_config.save() # Send updated options to Tor

# Get Tor state first to avoid a race conditions where CONF_CHANGED
# messages are received while Txtorcon is reading the consensus.
tor_state = yield tor.create_state()

# Get current TorConfig object
tor_config = yield tor.get_config()
wait_for_consensus = options_need_new_consensus(tor_config, tor_options)

# Update Tor config options from dictionary
for key, value in tor_options.items():
setattr(tor_config, key, value)
yield tor_config.save() # Send updated options to Tor
tor_state = yield tor.create_state()

if wait_for_consensus:
yield wait_for_newconsensus(tor_state)
Expand Down