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

Commit 553d5bb

Browse files
committed
Add config file parsing
1 parent 943e003 commit 553d5bb

3 files changed

Lines changed: 77 additions & 8 deletions

File tree

bwscanner/configutil.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os.path
2+
from shutil import copyfile
3+
from ConfigParser import SafeConfigParser
4+
5+
from bwscanner.logger import log
6+
7+
8+
def read_config(cfg_path):
9+
log.debug('reading config %s' % cfg_path)
10+
if not config_exists(cfg_path):
11+
copy_config(cfg_path)
12+
parser = SafeConfigParser()
13+
parser.read([cfg_path])
14+
# FIXME: handle section names
15+
section = 'default'
16+
return dict(parser.items(section))
17+
18+
19+
def config_exists(cfg_path):
20+
return os.path.isfile(cfg_path)
21+
22+
23+
def copy_config(cfg_path, cfg_default_path=None):
24+
if cfg_default_path is None:
25+
cfg_default_path = os.path.join(os.path.dirname(os.path.dirname(
26+
os.path.abspath(__file__))), 'data', 'config.ini')
27+
log.debug("cfg_default_path %s" % cfg_default_path)
28+
copyfile(cfg_default_path, cfg_path)

bwscanner/scanner.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
from twisted.internet import reactor
77

88
from bwscanner.attacher import connect_to_tor
9+
from bwscanner.configutil import read_config
910
from bwscanner.logger import setup_logging, log
1011
from bwscanner.measurement import BwScan
1112
from bwscanner.aggregate import write_aggregate_data
1213

1314

1415
BWSCAN_VERSION = '0.0.1'
16+
APP_NAME = 'bwscanner'
17+
DATA_DIR = os.environ.get("BWSCANNER_DATADIR", click.get_app_dir(APP_NAME))
18+
CONFIG_FILE = 'config.ini'
19+
LOG_FILE = 'bwscanner.log'
20+
21+
CTX = dict(
22+
default_map=read_config(os.path.join(DATA_DIR, CONFIG_FILE))
23+
)
1524

1625

1726
class ScanInstance(object):
@@ -29,18 +38,27 @@ def __repr__(self):
2938
pass_scan = click.make_pass_decorator(ScanInstance)
3039

3140

32-
@click.group()
41+
# FIXME: change all options to take defaults from CTX, ie config file?
42+
@click.group(context_settings=CTX)
3343
@click.option('--data-dir', type=click.Path(),
34-
default=os.environ.get("BWSCANNER_DATADIR", click.get_app_dir('bwscanner')),
44+
default=os.environ.get("BWSCANNER_DATADIR",
45+
CTX.get('data_dir',
46+
click.get_app_dir(APP_NAME))),
3547
help='Directory where bwscan should stores its measurements and '
3648
'other data.')
37-
@click.option('-l', '--loglevel', help='The logging level the scanner will use (default: info)',
38-
default='info', type=click.Choice(['debug', 'info', 'warn', 'error', 'critical']))
39-
@click.option('-f', '--logfile', type=click.Path(), help='The file the log will be written to',
40-
default=os.environ.get("BWSCANNER_LOGFILE", 'bwscanner.log'))
41-
@click.option('--launch-tor/--no-launch-tor', default=False,
49+
@click.option('-l', '--loglevel',
50+
help='The logging level the scanner will use (default: info)',
51+
default=CTX.get('loglevel', 'info'),
52+
type=click.Choice(['debug', 'info', 'warn', 'error', 'critical']))
53+
@click.option('-f', '--logfile', type=click.Path(),
54+
help='The file the log will be written to',
55+
default=os.environ.get("BWSCANNER_LOGFILE",
56+
CTX.get('logfile', LOG_FILE)))
57+
@click.option('--launch-tor/--no-launch-tor',
58+
default=CTX.get('launch_tor', False),
4259
help='Launch Tor or try to connect to an existing Tor instance.')
43-
@click.option('--circuit-build-timeout', default=20,
60+
@click.option('--circuit-build-timeout',
61+
default=CTX.get('circuit_build_timeout', 20),
4462
help='Option passed when launching Tor.')
4563
@click.version_option(BWSCAN_VERSION)
4664
@click.pass_context

data/config.ini

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[default]
2+
data_dir = $HOME/.config/bwscanner
3+
measurement_dir = $HOME/.config/bwscanner/measurements
4+
tor_dir = $HOME/.config/bwscanner/tordata
5+
loglevel = debug
6+
logfile = bwscanner.log
7+
baseurl = https://bwauth.torproject.org/bwauth.torproject.org
8+
bw_files = {
9+
64*1024: ("64M", "913b3c5df256d62235f955fa936e7a4e2d5e0cb6"),
10+
32*1024: ("32M", "a536076ef51c2cfff607fec2d362671e031d6b48"),
11+
16*1024: ("16M", "e91690ed2abf05e347b61aafaa23abf2a2b3292f"),
12+
8*1024: ("8M", "c690229b300945ec4ba872b80e8c443e2e1750f0"),
13+
4*1024: ("4M", "94f7bc6679a4419b080debd70166c2e43e80533d"),
14+
2*1024: ("2M", "9793cc92932598898d22497acdd5d732037b1a13"),
15+
}
16+
17+
launch_tor = True
18+
circuit_build_timeout = 20
19+
20+
partitions = 1
21+
current_partition = 1
22+
timeout = 120
23+
request_limit = 10

0 commit comments

Comments
 (0)