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

Commit ccaf654

Browse files
committed
Add tor directory parameter to launch_tor
1 parent 943e003 commit ccaf654

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

bwscanner/attacher.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def got_newconsensus(event):
135135

136136

137137
@defer.inlineCallbacks
138-
def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,
138+
def connect_to_tor(launch_tor, circuit_build_timeout, tor_dir=None, control_port=None,
139139
tor_overrides=None):
140140
"""
141141
Launch or connect to a Tor instance
@@ -158,8 +158,7 @@ def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,
158158

159159
if launch_tor:
160160
log.info("Spawning a new Tor instance.")
161-
# TODO: Pass in data_dir directory so consensus can be cached
162-
tor = yield txtorcon.launch(reactor)
161+
tor = yield txtorcon.launch(reactor, data_directory=tor_dir)
163162
else:
164163
log.info("Trying to connect to a running Tor instance.")
165164
if control_port:

bwscanner/scanner.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import signal
34
import time
45

56
import click
@@ -12,6 +13,7 @@
1213

1314

1415
BWSCAN_VERSION = '0.0.1'
16+
BASEURL = 'https://siv.sunet.se/bwauth/'
1517

1618

1719
class ScanInstance(object):
@@ -21,6 +23,7 @@ class ScanInstance(object):
2123
def __init__(self, data_dir):
2224
self.data_dir = data_dir
2325
self.measurement_dir = os.path.join(data_dir, 'measurements')
26+
self.tor_dir = os.path.join(data_dir, 'tor_data')
2427

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

6063
# Create a connection to a Tor instance
61-
ctx.obj.tor_state = connect_to_tor(launch_tor, circuit_build_timeout)
64+
ctx.obj.tor_state = connect_to_tor(launch_tor, circuit_build_timeout,
65+
ctx.obj.tor_dir)
6266

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

6670

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

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

94-
scan.tor_state.addCallback(BwScan, reactor, scan_data_dir,
103+
log.debug('adding callback to tor_state')
104+
scan.tor_state.addCallback(BwScan, reactor, scan_data_dir, baseurl,
95105
request_timeout=timeout,
96106
request_limit=request_limit,
97107
partitions=partitions,
98108
this_partition=current_partition)
109+
# NOTE: in measurements?
110+
log.debug('adding callback to tor_state')
99111
scan.tor_state.addCallback(lambda scanner: scanner.run_scan())
112+
# NOTE: why lambda here?
113+
log.debug('adding callback to tor_state')
100114
scan.tor_state.addCallback(lambda _: reactor.stop())
115+
log.debug('adding callback to tor_state')
101116
scan.tor_state.addCallback(rename_finished_scan)
117+
def signal_handler(signal, frame):
118+
print "signal caught, stopping probe"
119+
reactor.stop()
120+
signal.signal(signal.SIGINT, signal_handler)
121+
signal.signal(signal.SIGTERM, signal_handler)
102122

123+
log.debug('reactor run')
103124
reactor.run()
104125

105126

0 commit comments

Comments
 (0)