Commit 225d880b authored by juga's avatar juga
Browse files

Add HTTP headers to send in every request

create a constant, set the headers in the session.
parent 55009364
__version__ = '1.0.3-dev0'
from . import globals # noqa
class Settings:
def __init__(self):
# update this dict from globals (but only for ALL_CAPS settings)
for setting in dir(globals):
if setting.isupper():
setattr(self, setting, getattr(globals, setting))
def init_http_headers(self, nickname, uuid, tor_version):
self.HTTP_HEADERS['Tor-Bandwidth-Scanner-Nickname'] = nickname
self.HTTP_HEADERS['Tor-Bandwidth-Scanner-UUID'] = uuid
self.HTTP_HEADERS['User-Agent'] += tor_version
settings = Settings() # noqa
''' Measure the relays. '''
import uuid
from ..lib.circuitbuilder import GapsCircuitBuilder as CB
from ..lib.resultdump import ResultDump
from ..lib.resultdump import ResultSuccess, ResultErrorCircuit
......@@ -21,6 +23,8 @@ import logging
import requests
import random
from sbws import settings
rng = random.SystemRandom()
end_event = Event()
......@@ -335,6 +339,14 @@ def run_speedtest(args, conf):
'even lead to messed up results.',
conf.getpath('tor', 'control_socket'))
time.sleep(15)
# When there will be a refactor where conf is global, this can be removed
# from here.
state = State(conf.getpath('paths', 'state_fname'))
# Call only once to initialize http_headers
settings.init_http_headers(conf.get('scanner', 'nickname'), state['uuid'],
str(controller.get_version()))
rl = RelayList(args, conf, controller)
cb = CB(args, conf, controller, rl)
rd = ResultDump(args, conf, end_event)
......@@ -394,6 +406,9 @@ def main(args, conf):
state = State(conf.getpath('paths', 'state_fname'))
state['scanner_started'] = now_isodt_str()
# Generate an unique identifier for each scanner
if 'uuid' not in state:
state['uuid'] = str(uuid.uuid4())
try:
run_speedtest(args, conf)
......
import os
import logging
import platform
from requests import __version__ as requests_version
from stem import __version__ as stem_version
from sbws import __version__
log = logging.getLogger(__name__)
......@@ -53,6 +60,27 @@ MAX_BW_DIFF_PERC = 50
BW_LINE_SIZE = 510
# Metadata to send in every requests, so that data servers can know which
# scanners are using them.
# In Requests these keys are case insensitive.
HTTP_HEADERS = {
# This would be ignored if changing to HTTP/2
'Connection': 'keep-alive',
# Needs to get Tor version from the controller
'User-Agent': 'sbws/{} ({}) Python/{} Requests/{} Stem/{} Tor/'.format(
__version__, platform.platform(),
platform.python_version(),
requests_version, stem_version),
# Organization defined names (:rfc:`7239`)
# Needs to get the nickname from the user config file.
'Tor-Bandwidth-Scanner-Nickname': '{}',
'Tor-Bandwidth-Scanner-UUID': '{}',
# In case of including IP address.
# 'Forwarded': 'for={}' # IPv6 part, if there's
}
# In the case of having ipv6 it's concatenated to forwarder.
IPV6_FORWARDED = ', for="[{}]"'
HTTP_GET_HEADERS = {
'Range': '{}',
'Accept-Encoding': 'identity',
......
import requests
from sbws import settings
import sbws.util.stem as stem_utils
......@@ -10,4 +12,5 @@ def make_session(controller, timeout):
'https': 'socks5h://{}:{}'.format(*socks_info),
}
s.timeout = timeout
s.headers = settings.HTTP_HEADERS
return s
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