[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/master] Add HTTP headers to send in every request



commit 225d880b24a5cf4860a406169ae803a7ae866fba
Author: juga0 <juga@xxxxxxxxxx>
Date:   Wed Dec 12 14:39:26 2018 +0000

    Add HTTP headers to send in every request
    
    create a constant, set the headers in the session.
---
 sbws/__init__.py      | 17 +++++++++++++++++
 sbws/core/scanner.py  | 15 +++++++++++++++
 sbws/globals.py       | 28 ++++++++++++++++++++++++++++
 sbws/util/requests.py |  3 +++
 4 files changed, 63 insertions(+)

diff --git a/sbws/__init__.py b/sbws/__init__.py
index 4256404..eea7006 100644
--- a/sbws/__init__.py
+++ b/sbws/__init__.py
@@ -1 +1,18 @@
 __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
diff --git a/sbws/core/scanner.py b/sbws/core/scanner.py
index f461824..a6e46a4 100644
--- a/sbws/core/scanner.py
+++ b/sbws/core/scanner.py
@@ -1,5 +1,7 @@
 ''' 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)
diff --git a/sbws/globals.py b/sbws/globals.py
index 7196736..cda6a54 100644
--- a/sbws/globals.py
+++ b/sbws/globals.py
@@ -1,5 +1,12 @@
 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',
diff --git a/sbws/util/requests.py b/sbws/util/requests.py
index 103182a..0b1ece2 100644
--- a/sbws/util/requests.py
+++ b/sbws/util/requests.py
@@ -1,4 +1,6 @@
 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



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits