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

Commit

Permalink
Add config file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
juga0 committed Feb 4, 2018
1 parent 943e003 commit 553d5bb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
28 changes: 28 additions & 0 deletions bwscanner/configutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os.path
from shutil import copyfile
from ConfigParser import SafeConfigParser

from bwscanner.logger import log


def read_config(cfg_path):
log.debug('reading config %s' % cfg_path)
if not config_exists(cfg_path):
copy_config(cfg_path)
parser = SafeConfigParser()
parser.read([cfg_path])
# FIXME: handle section names
section = 'default'
return dict(parser.items(section))


def config_exists(cfg_path):
return os.path.isfile(cfg_path)


def copy_config(cfg_path, cfg_default_path=None):
if cfg_default_path is None:
cfg_default_path = os.path.join(os.path.dirname(os.path.dirname(
os.path.abspath(__file__))), 'data', 'config.ini')
log.debug("cfg_default_path %s" % cfg_default_path)
copyfile(cfg_default_path, cfg_path)
34 changes: 26 additions & 8 deletions bwscanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
from twisted.internet import reactor

from bwscanner.attacher import connect_to_tor
from bwscanner.configutil import read_config
from bwscanner.logger import setup_logging, log
from bwscanner.measurement import BwScan
from bwscanner.aggregate import write_aggregate_data


BWSCAN_VERSION = '0.0.1'
APP_NAME = 'bwscanner'
DATA_DIR = os.environ.get("BWSCANNER_DATADIR", click.get_app_dir(APP_NAME))
CONFIG_FILE = 'config.ini'
LOG_FILE = 'bwscanner.log'

CTX = dict(
default_map=read_config(os.path.join(DATA_DIR, CONFIG_FILE))
)


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


@click.group()
# FIXME: change all options to take defaults from CTX, ie config file?
@click.group(context_settings=CTX)
@click.option('--data-dir', type=click.Path(),
default=os.environ.get("BWSCANNER_DATADIR", click.get_app_dir('bwscanner')),
default=os.environ.get("BWSCANNER_DATADIR",
CTX.get('data_dir',
click.get_app_dir(APP_NAME))),
help='Directory where bwscan should stores its measurements and '
'other data.')
@click.option('-l', '--loglevel', help='The logging level the scanner will use (default: info)',
default='info', type=click.Choice(['debug', 'info', 'warn', 'error', 'critical']))
@click.option('-f', '--logfile', type=click.Path(), help='The file the log will be written to',
default=os.environ.get("BWSCANNER_LOGFILE", 'bwscanner.log'))
@click.option('--launch-tor/--no-launch-tor', default=False,
@click.option('-l', '--loglevel',
help='The logging level the scanner will use (default: info)',
default=CTX.get('loglevel', 'info'),
type=click.Choice(['debug', 'info', 'warn', 'error', 'critical']))
@click.option('-f', '--logfile', type=click.Path(),
help='The file the log will be written to',
default=os.environ.get("BWSCANNER_LOGFILE",
CTX.get('logfile', LOG_FILE)))
@click.option('--launch-tor/--no-launch-tor',
default=CTX.get('launch_tor', False),
help='Launch Tor or try to connect to an existing Tor instance.')
@click.option('--circuit-build-timeout', default=20,
@click.option('--circuit-build-timeout',
default=CTX.get('circuit_build_timeout', 20),
help='Option passed when launching Tor.')
@click.version_option(BWSCAN_VERSION)
@click.pass_context
Expand Down
23 changes: 23 additions & 0 deletions data/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[default]
data_dir = $HOME/.config/bwscanner
measurement_dir = $HOME/.config/bwscanner/measurements
tor_dir = $HOME/.config/bwscanner/tordata
loglevel = debug
logfile = bwscanner.log
baseurl = https://bwauth.torproject.org/bwauth.torproject.org
bw_files = {
64*1024: ("64M", "913b3c5df256d62235f955fa936e7a4e2d5e0cb6"),
32*1024: ("32M", "a536076ef51c2cfff607fec2d362671e031d6b48"),
16*1024: ("16M", "e91690ed2abf05e347b61aafaa23abf2a2b3292f"),
8*1024: ("8M", "c690229b300945ec4ba872b80e8c443e2e1750f0"),
4*1024: ("4M", "94f7bc6679a4419b080debd70166c2e43e80533d"),
2*1024: ("2M", "9793cc92932598898d22497acdd5d732037b1a13"),
}

launch_tor = True
circuit_build_timeout = 20

partitions = 1
current_partition = 1
timeout = 120
request_limit = 10

0 comments on commit 553d5bb

Please sign in to comment.