globals.py 2.22 KB
Newer Older
Matt Traudt's avatar
Matt Traudt committed
1
import os
2
import logging
Matt Traudt's avatar
Matt Traudt committed
3

4
log = logging.getLogger(__name__)
Matt Traudt's avatar
Matt Traudt committed
5

6
RESULT_VERSION = 4
7
WIRE_VERSION = 1
juga's avatar
juga committed
8
SPEC_VERSION = '1.2.0'
9

10
11
12
13
14
15
16
17
18
19
# This is a dictionary of torrc options we always want to set when launching
# Tor and that do not depend on any runtime configuration
TORRC_STARTING_POINT = {
    # We will find out via the ControlPort and not setting something static
    # means a lower chance of conflict
    'SocksPort': 'auto',
    # Easier than password authentication
    'CookieAuthentication': '1',
    # To avoid path bias warnings
    'UseEntryGuards': '0',
20
21
22
    # Because we need things from full server descriptors (namely for now: the
    # bandwidth line)
    'UseMicrodescriptors': '0',
juga's avatar
juga committed
23
24
25
26
    # useful logging options for clients that don't care about anonymity
    'SafeLogging': '0',
    'LogTimeGranularity': '1',
    'ProtocolWarnings': '1',
27
28
}

juga's avatar
juga committed
29
30
31
32
PKG_DIR = os.path.abspath(os.path.dirname(__file__))
DEFAULT_CONFIG_PATH = os.path.join(PKG_DIR, 'config.default.ini')
DEFAULT_LOG_CONFIG_PATH = os.path.join(PKG_DIR, 'config.log.default.ini')
USER_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.sbws.ini')
33
SUPERVISED_USER_CONFIG_PATH = "/etc/sbws/sbws.ini"
34
35
SUPERVISED_RUN_DPATH = "/run/sbws/tor"

juga's avatar
juga committed
36
SOCKET_TIMEOUT = 60  # seconds
juga's avatar
juga committed
37
38
39
40
41
42
43
44

SBWS_SCALE_CONSTANT = 7500
TORFLOW_SCALING = 1
SBWS_SCALING = 2
TORFLOW_BW_MARGIN = 0.05
TORFLOW_OBS_LAST = 0
TORFLOW_OBS_MEAN = 1
TORFLOW_OBS_DECAYING = 3
45
TORFLOW_ROUND_DIG = 3
46
PROP276_ROUND_DIG = 2
juga's avatar
juga committed
47
48
DAY_SECS = 86400
NUM_MIN_RESULTS = 2
49
MIN_REPORT = 60
juga's avatar
juga committed
50
51
52
# Maximum difference between the total consensus bandwidth and the total in
# in the bandwidth lines in percentage
MAX_BW_DIFF_PERC = 50
juga's avatar
juga committed
53

juga's avatar
juga committed
54
BW_LINE_SIZE = 510
juga's avatar
juga committed
55

Matt Traudt's avatar
Matt Traudt committed
56

57
def fail_hard(*a, **kw):
Matt Traudt's avatar
Matt Traudt committed
58
    ''' Log something ... and then exit as fast as possible '''
Matt Traudt's avatar
Matt Traudt committed
59
    log.critical(*a, **kw)
60
    exit(1)
Matt Traudt's avatar
Matt Traudt committed
61
62


63
64
65
66
67
68
69
70
71
72
73
74
75
def touch_file(fname, times=None):
    '''
    If **fname** exists, update its last access and modified times to now. If
    **fname** does not exist, create it. If **times** are specified, pass them
    to os.utime for use.

    :param str fname: Name of file to update or create
    :param tuple times: 2-tuple of floats for access time and modified time
        respectively
    '''
    log.debug('Touching %s', fname)
    with open(fname, 'a') as fd:
        os.utime(fd.fileno(), times=times)