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

Commit

Permalink
Add tor directory parameter to launch_tor
Browse files Browse the repository at this point in the history
  • Loading branch information
juga0 committed Feb 11, 2018
1 parent 943e003 commit ccaf654
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 2 additions & 3 deletions bwscanner/attacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def got_newconsensus(event):


@defer.inlineCallbacks
def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,
def connect_to_tor(launch_tor, circuit_build_timeout, tor_dir=None, control_port=None,
tor_overrides=None):
"""
Launch or connect to a Tor instance
Expand All @@ -158,8 +158,7 @@ def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,

if launch_tor:
log.info("Spawning a new Tor instance.")
# TODO: Pass in data_dir directory so consensus can be cached
tor = yield txtorcon.launch(reactor)
tor = yield txtorcon.launch(reactor, data_directory=tor_dir)
else:
log.info("Trying to connect to a running Tor instance.")
if control_port:
Expand Down
27 changes: 24 additions & 3 deletions bwscanner/scanner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import signal
import time

import click
Expand All @@ -12,6 +13,7 @@


BWSCAN_VERSION = '0.0.1'
BASEURL = 'https://siv.sunet.se/bwauth/'


class ScanInstance(object):
Expand All @@ -21,6 +23,7 @@ class ScanInstance(object):
def __init__(self, data_dir):
self.data_dir = data_dir
self.measurement_dir = os.path.join(data_dir, 'measurements')
self.tor_dir = os.path.join(data_dir, 'tor_data')

def __repr__(self):
return '<BWScan %r>' % self.data_dir
Expand Down Expand Up @@ -58,13 +61,18 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
os.makedirs(ctx.obj.measurement_dir)

# Create a connection to a Tor instance
ctx.obj.tor_state = connect_to_tor(launch_tor, circuit_build_timeout)
ctx.obj.tor_state = connect_to_tor(launch_tor, circuit_build_timeout,
ctx.obj.tor_dir)

# Set up the logger to only output log lines of level `loglevel` and above.
setup_logging(log_level=loglevel, log_name=logfile)


@cli.command(short_help="Measure the Tor relays.")
# FIXME: when having a configuration file the default will be given by it.
@click.option('--baseurl',
help='URL that provides the files to perform the measurements with',
default=BASEURL)
@click.option('--partitions', '-p', default=1,
help='Divide the set of relays into subsets. 1 by default.')
@click.option('--current-partition', '-c', default=1,
Expand All @@ -75,7 +83,7 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout):
help='Limit the number of simultaneous bandwidth measurements '
'(default: %d).' % 10)
@pass_scan
def scan(scan, partitions, current_partition, timeout, request_limit):
def scan(scan, baseurl, partitions, current_partition, timeout, request_limit):
"""
Start a scan through each Tor relay to measure it's bandwidth.
"""
Expand All @@ -88,18 +96,31 @@ def scan(scan, partitions, current_partition, timeout, request_limit):
os.makedirs(scan_data_dir)

def rename_finished_scan(deferred):
log.debug('renaming finished scan with deferred')
click.echo(deferred)
os.rename(scan_data_dir, os.path.join(scan.measurement_dir, scan_time))

scan.tor_state.addCallback(BwScan, reactor, scan_data_dir,
log.debug('adding callback to tor_state')
scan.tor_state.addCallback(BwScan, reactor, scan_data_dir, baseurl,
request_timeout=timeout,
request_limit=request_limit,
partitions=partitions,
this_partition=current_partition)
# NOTE: in measurements?
log.debug('adding callback to tor_state')
scan.tor_state.addCallback(lambda scanner: scanner.run_scan())
# NOTE: why lambda here?
log.debug('adding callback to tor_state')
scan.tor_state.addCallback(lambda _: reactor.stop())
log.debug('adding callback to tor_state')
scan.tor_state.addCallback(rename_finished_scan)
def signal_handler(signal, frame):
print "signal caught, stopping probe"
reactor.stop()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

log.debug('reactor run')
reactor.run()


Expand Down

0 comments on commit ccaf654

Please sign in to comment.