Commit e5ed6d34 authored by Matt Traudt's avatar Matt Traudt Committed by Matt Traudt
Browse files

Collect globals and standardize their names

parent 738af9de
......@@ -16,7 +16,7 @@ import os
def find_version():
with open(os.path.join("..", "..", "sbws", "__init__.py")) as fp:
for line in fp:
if "version" in line.strip():
if "SBWS_VERSION" in line.strip():
version = line.split("=", 1)[1].strip().strip("'")
return version
......
version = '0.1.0'
res_proto_ver = 1
wire_proto_ver = 1
import os
SBWS_VERSION = '0.1.0'
RESULT_VERSION = 1
WIRE_VERSION = 1
SPEC_VERSION = '1.1.0'
PKG_DIR = os.path.abspath(os.path.dirname(__file__))
SOCKET_TIMEOUT = 60 # seconds
# Minimum and maximum number of bytes a scanner is allowed to request from a
# server. If these are changed, a WIRE_VERSION bump is required, which also
# happens to require an sbws major version bump.
#
# Note for smart people and people who pull out Wireshark: Even if the scanner
# requests 1 byte, that request and the 1 byte response will each be carried
# over the Internet in 514 byte Tor cells. Theoretically we could bump the
# minimum request size up to ~498 bytes, but I see no reason why we should.
# Trying to hit the maximum cell size just makes sbws server send more, us read
# more, and it runs the risk of standards changing underneath us and sbws
# suddenly creating more than one cell.
MIN_REQ_BYTES = 1
MAX_REQ_BYTES = 1 * 1024 * 1024 * 1024 # 1 GiB
......@@ -11,7 +11,7 @@ from ..lib.helperrelay import HelperRelayList
from ..util.simpleauth import authenticate_to_server
from ..util.sockio import (make_socket, close_socket)
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES)
from sbws import MIN_REQ_BYTES, MAX_REQ_BYTES
import sbws.util.stem as stem_utils
from argparse import ArgumentDefaultsHelpFormatter
from multiprocessing.dummy import Pool
......
from ..util.simpleauth import authenticate_scanner
from ..util.sockio import read_line
from sbws.globals import (fail_hard, is_initted)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES, SOCKET_TIMEOUT)
from sbws import MIN_REQ_BYTES, MAX_REQ_BYTES, SOCKET_TIMEOUT
from argparse import ArgumentDefaultsHelpFormatter
from functools import lru_cache
from threading import Thread
......
......@@ -3,25 +3,6 @@ import logging
log = logging.getLogger(__name__)
G_PKG_DIR = os.path.abspath(os.path.dirname(__file__))
# Minimum and maximum number of bytes a scanner is allowed to request from a
# server. If these are changed, a WIRE_PROTO_VER bump is required, which also
# happens to require an sbws major version bump.
#
# Note for smart people and people who pull out Wireshark: Even if the scanner
# requests 1 byte, that request and the 1 byte response will each be carried
# over the Internet in 514 byte Tor cells. Theoretically we could bump the
# minimum request size up to ~498 bytes, but I see no reason why we should.
# Trying to hit the maximum cell size just makes sbws server send more, us read
# more, and it runs the risk of standards changing underneath us and sbws
# suddenly creating more than one cell.
MIN_REQ_BYTES = 1
MAX_REQ_BYTES = 1 * 1024 * 1024 * 1024 # 1 GiB
SOCKET_TIMEOUT = 60 # seconds
SPEC_VERSION = '1.1.0'
def is_initted(d):
if not os.path.isdir(d):
......
......@@ -12,7 +12,7 @@ from datetime import datetime
from datetime import timedelta
from enum import Enum
from stem.descriptor.router_status_entry import RouterStatusEntryV3
from sbws import res_proto_ver
from sbws import RESULT_VERSION
from sbws.util.filelock import DirectoryLock
log = logging.getLogger(__name__)
......@@ -187,7 +187,7 @@ class Result:
@property
def version(self):
return res_proto_ver
return RESULT_VERSION
def to_dict(self):
return {
......@@ -210,7 +210,7 @@ class Result:
NotImplementedError. If we can't parse the dict for some other reason,
return None. '''
assert 'version' in d
if d['version'] != res_proto_ver:
if d['version'] != RESULT_VERSION:
return None
assert 'type' in d
if d['type'] == _ResultType.Success.value:
......
......@@ -4,8 +4,7 @@
import time
import logging
from sbws import version
from sbws.globals import SPEC_VERSION
from sbws import SBWS_VERSION, SPEC_VERSION
log = logging.getLogger(__name__)
......@@ -22,7 +21,7 @@ class V3BwHeader(object):
:param str software_version: the version of the software
"""
def __init__(self, timestamp=None, version=SPEC_VERSION, software='sbws',
software_version=version):
software_version=SBWS_VERSION):
self.timestamp = timestamp or int(time.time())
self.version = version
self.software = software
......
......@@ -4,7 +4,7 @@ import logging
import logging.config
from string import Template
from tempfile import NamedTemporaryFile
from sbws.globals import G_PKG_DIR
from sbws import PKG_DIR
_ALPHANUM = 'abcdefghijklmnopqrstuvwxyz'
_ALPHANUM += _ALPHANUM.upper()
......@@ -25,7 +25,7 @@ def _read_config_file(conf, fname):
def _get_default_config():
conf = ConfigParser(interpolation=ExtendedInterpolation())
fname = os.path.join(G_PKG_DIR, 'config.default.ini')
fname = os.path.join(PKG_DIR, 'config.default.ini')
assert os.path.isfile(fname)
conf = _read_config_file(conf, fname)
return conf
......@@ -60,7 +60,7 @@ def _get_default_logging_config(args, conf=None):
conf = ConfigParser(interpolation=ExtendedInterpolation())
else:
assert isinstance(conf, ConfigParser)
fname = os.path.join(G_PKG_DIR, 'config.log.default.ini')
fname = os.path.join(PKG_DIR, 'config.log.default.ini')
assert os.path.isfile(fname)
conf = _read_config_file(conf, fname)
return conf
......@@ -76,7 +76,7 @@ def get_config(args):
def get_user_example_config():
conf = ConfigParser(interpolation=ExtendedInterpolation())
fname = os.path.join(G_PKG_DIR, 'config.example.ini')
fname = os.path.join(PKG_DIR, 'config.example.ini')
assert os.path.isfile(fname)
conf = _read_config_file(conf, fname)
return conf
......
......@@ -5,7 +5,7 @@ import sbws.core.init
import sbws.core.pwgen
import sbws.core.server
import sbws.core.stats
from sbws import version
from sbws import SBWS_VERSION
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import os
......@@ -20,7 +20,7 @@ def create_parser():
p = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
p.add_argument(
'--version', action='version', help='sbws version',
version='%(prog)s {}'.format(version))
version='%(prog)s {}'.format(SBWS_VERSION))
p.add_argument('--log-level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Override the sbws log level')
......
from ..util.sockio import read_line
import socket
import logging
from sbws import wire_proto_ver
from sbws import WIRE_VERSION
MAGIC_BYTES = b'SBWS'
SUCCESS_BYTES = b'.'
......@@ -35,9 +35,9 @@ def authenticate_scanner(sock, conf_section):
return None
line = read_line(sock, max_len=4)
if line != str(wire_proto_ver):
if line != str(WIRE_VERSION):
log.warning('Scanner gave protocol version %s but we support %d', line,
wire_proto_ver)
WIRE_VERSION)
return None
try:
......@@ -79,7 +79,7 @@ def authenticate_to_server(sock, pw):
assert len(pw) == PW_LEN
try:
sock.send(MAGIC_BYTES)
sock.send(bytes('{}\n'.format(wire_proto_ver), 'utf-8'))
sock.send(bytes('{}\n'.format(WIRE_VERSION), 'utf-8'))
sock.send(bytes(pw, 'utf-8'))
msg = sock.recv(len(SUCCESS_BYTES))
except (socket.timeout, ConnectionResetError, BrokenPipeError) as e:
......@@ -89,7 +89,7 @@ def authenticate_to_server(sock, pw):
log.warning('Didn\'t get success code from server. Most likely the '
'password we are giving it is wrong. It\'s also possible '
'the server doesn\'t support wire protocol version %d.',
wire_proto_ver)
WIRE_VERSION)
return False
return True
......
from sbws.globals import SOCKET_TIMEOUT
from sbws import SOCKET_TIMEOUT
import socket
import socks
import logging
......
......@@ -35,7 +35,7 @@ def get_data_files():
def find_version():
with open(os.path.join("sbws", "__init__.py")) as fp:
for line in fp:
if "version" in line.strip():
if "SBWS_VERSION" in line.strip():
version = line.split("=", 1)[1].strip().strip("'")
return version
......
from unittest.mock import patch
from sbws import res_proto_ver
from sbws import RESULT_VERSION
from sbws.lib.resultdump import Result
from sbws.lib.resultdump import ResultSuccess
from sbws.lib.resultdump import ResultError
......@@ -39,7 +39,7 @@ def test_Result_from_dict_bad_version():
The first thing that is checked is the version field, and a wrong one
should return None
'''
d = {'version': res_proto_ver + 1}
d = {'version': RESULT_VERSION + 1}
r = Result.from_dict(d)
assert r is None
......@@ -49,7 +49,7 @@ def test_Result_from_dict_bad_type():
If the result type string doesn't match any of the known types, then it
should throw NotImplementedError
'''
d = {'version': res_proto_ver, 'type': 'NotARealType'}
d = {'version': RESULT_VERSION, 'type': 'NotARealType'}
try:
Result.from_dict(d)
except NotImplementedError as e:
......@@ -85,7 +85,7 @@ def test_ResultSuccess(time_mock):
assert r1.address == relay_ip
assert r1.circ == circ
assert r1.server_host == server_host
assert r1.version == res_proto_ver
assert r1.version == RESULT_VERSION
assert str(r1) == str(r2)
......@@ -108,7 +108,7 @@ def test_ResultSuccess_from_dict(time_mock):
'rtts': rtts, 'downloads': downloads, 'fingerprint': fp1,
'nickname': nick, 'address': relay_ip, 'circ': circ,
'server_host': server_host, 'scanner': scanner_nick,
'version': res_proto_ver, 'type': _ResultType.Success, 'time': t,
'version': RESULT_VERSION, 'type': _ResultType.Success, 'time': t,
}
r2 = Result.from_dict(d)
assert isinstance(r1, ResultSuccess)
......@@ -140,7 +140,7 @@ def test_ResultError(time_mock):
assert r1.address == relay_ip
assert r1.circ == circ
assert r1.server_host == server_host
assert r1.version == res_proto_ver
assert r1.version == RESULT_VERSION
assert str(r1) == str(r2)
......@@ -162,7 +162,7 @@ def test_ResultError_from_dict(time_mock):
'msg': msg, 'fingerprint': fp1,
'nickname': nick, 'address': relay_ip, 'circ': circ,
'server_host': server_host, 'scanner': scanner_nick,
'version': res_proto_ver, 'type': _ResultType.Error, 'time': t,
'version': RESULT_VERSION, 'type': _ResultType.Error, 'time': t,
}
r2 = Result.from_dict(d)
assert isinstance(r1, ResultError)
......@@ -195,7 +195,7 @@ def test_ResultErrorCircuit(time_mock):
assert r1.address == relay_ip
assert r1.circ == circ
assert r1.server_host == server_host
assert r1.version == res_proto_ver
assert r1.version == RESULT_VERSION
assert str(r1) == str(r2)
......@@ -217,7 +217,7 @@ def test_ResultErrorCircuit_from_dict(time_mock):
'msg': msg, 'fingerprint': fp1,
'nickname': nick, 'address': relay_ip, 'circ': circ,
'server_host': server_host, 'scanner': scanner_nick,
'version': res_proto_ver, 'type': _ResultType.ErrorCircuit, 'time': t,
'version': RESULT_VERSION, 'type': _ResultType.ErrorCircuit, 'time': t,
}
r2 = Result.from_dict(d)
assert isinstance(r1, ResultErrorCircuit)
......@@ -250,7 +250,7 @@ def test_ResultErrorStream(time_mock):
assert r1.address == relay_ip
assert r1.circ == circ
assert r1.server_host == server_host
assert r1.version == res_proto_ver
assert r1.version == RESULT_VERSION
assert str(r1) == str(r2)
......@@ -272,7 +272,7 @@ def test_ResultErrorStream_from_dict(time_mock):
'msg': msg, 'fingerprint': fp1,
'nickname': nick, 'address': relay_ip, 'circ': circ,
'server_host': server_host, 'scanner': scanner_nick,
'version': res_proto_ver, 'type': _ResultType.ErrorStream, 'time': t,
'version': RESULT_VERSION, 'type': _ResultType.ErrorStream, 'time': t,
}
r2 = Result.from_dict(d)
assert isinstance(r1, ResultErrorStream)
......@@ -305,7 +305,7 @@ def test_ResultErrorAuth(time_mock):
assert r1.address == relay_ip
assert r1.circ == circ
assert r1.server_host == server_host
assert r1.version == res_proto_ver
assert r1.version == RESULT_VERSION
assert str(r1) == str(r2)
......@@ -327,7 +327,7 @@ def test_ResultErrorAuth_from_dict(time_mock):
'msg': msg, 'fingerprint': fp1,
'nickname': nick, 'address': relay_ip, 'circ': circ,
'server_host': server_host, 'scanner': scanner_nick,
'version': res_proto_ver, 'type': _ResultType.ErrorAuth, 'time': t,
'version': RESULT_VERSION, 'type': _ResultType.ErrorAuth, 'time': t,
}
r2 = Result.from_dict(d)
assert isinstance(r1, ResultErrorAuth)
......
from sbws.util.simpleauth import authenticate_scanner
from sbws.util.simpleauth import authenticate_to_server
from sbws.util.simpleauth import (MAGIC_BYTES, PW_LEN, SUCCESS_BYTES)
from sbws import wire_proto_ver
from sbws import WIRE_VERSION
from configparser import ConfigParser
import socket
import logging
......@@ -121,7 +121,7 @@ def test_simpleauth_authscanner_timeout_version(caplog):
def test_simpleauth_authscanner_goodversion(caplog):
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
sock = MockServerSideSocket(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
assert ret is None
......@@ -129,7 +129,7 @@ def test_simpleauth_authscanner_goodversion(caplog):
def test_simpleauth_authscanner_nonunicodepassword(caplog):
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
in_data += b'\x80' * PW_LEN
sock = MockServerSideSocket(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
......@@ -138,7 +138,7 @@ def test_simpleauth_authscanner_nonunicodepassword(caplog):
def test_simpleauth_authscanner_timeout_password(caplog):
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
in_data += b'a' * (PW_LEN - 1)
sock = MockServerSideSocketDelayedTimeout(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
......@@ -147,7 +147,7 @@ def test_simpleauth_authscanner_timeout_password(caplog):
def test_simpleauth_authscanner_badpassword(caplog):
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
in_data += b'c' * PW_LEN
sock = MockServerSideSocket(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
......@@ -157,7 +157,7 @@ def test_simpleauth_authscanner_badpassword(caplog):
def test_simpleauth_authscanner_goodpassword(caplog):
caplog.set_level(logging.DEBUG)
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
in_data += b'a' * PW_LEN
sock = MockServerSideSocket(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
......@@ -167,7 +167,7 @@ def test_simpleauth_authscanner_goodpassword(caplog):
def test_simpleauth_authscanner_cantsend(caplog):
in_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') + b'\n'
in_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') + b'\n'
in_data += b'a' * PW_LEN
sock = MockServerSideSocketTimeoutOnSend(in_data)
ret = authenticate_scanner(sock, server_conf['server.passwords'])
......@@ -189,7 +189,7 @@ class MockScannerSideSocket(socket.socket):
def recv(self, amount):
assert isinstance(amount, int)
correct_sent_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') \
correct_sent_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') \
+ b'\n' + bytes(MockScannerSideSocket.allowed_password, 'utf-8')
if self.testing_get_sent_data() != correct_sent_data:
raise ConnectionResetError('connresetmock')
......@@ -207,7 +207,7 @@ class MockScannerSideSocketTimeout(MockScannerSideSocket):
class MockScannerSideSocketWrongSuccessCode(MockScannerSideSocket):
def recv(self, amount):
assert isinstance(amount, int)
correct_sent_data = MAGIC_BYTES + bytes(str(wire_proto_ver), 'utf-8') \
correct_sent_data = MAGIC_BYTES + bytes(str(WIRE_VERSION), 'utf-8') \
+ b'\n' + bytes(MockScannerSideSocket.allowed_password, 'utf-8')
if self.testing_get_sent_data() != correct_sent_data:
raise ConnectionResetError('connresetmock')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment