[tor-commits] [stem/master] Provide a User-Agent when downloading descriptors

atagar at torproject.org atagar at torproject.org
Thu Apr 12 19:25:38 UTC 2018


commit bc58a28bf07a646d3e1ced41891a03e31d023a7d
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Apr 12 12:17:01 2018 -0700

    Provide a User-Agent when downloading descriptors
    
    By default urllib provides a user agent that simply says that we're using
    python. On reflection lets specify that the download is coming from Stem. This
    will give our dirauths a bit more visibility into DirPort requests they're
    servicing, and Stem versions that are actively being used to download
    descriptors.
---
 docs/change_log.rst       | 1 +
 stem/descriptor/remote.py | 9 ++++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 9be7912d..ac827bdf 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -58,6 +58,7 @@ The following are only available within Stem's `git repository
   * Added zstd and lzma compression support (:spec:`1cb56af`)
   * Added server descriptor's new is_hidden_service_dir attribute
   * Don't retry downloading descriptors when we've timed out
+  * Supplying a User-Agent when downloading descriptors.
   * Reduced maximum descriptors fetched by the remote module to match tor's new limit (:trac:`24743`)
   * Consensus **shared_randomness_*_reveal_count** attributes undocumented, and unavailable if retrieved before their corresponding shared_randomness_*_value attribute (:trac:`25046`)
   * Allow 'proto' line to have blank values (:spec:`a8455f4`)
diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py
index 3faace4c..30ae388b 100644
--- a/stem/descriptor/remote.py
+++ b/stem/descriptor/remote.py
@@ -106,11 +106,11 @@ import threading
 import time
 import zlib
 
+import stem
 import stem.descriptor
 import stem.prereq
 import stem.util.enum
 
-from stem import Flag
 from stem.util import _hash_attr, connection, log, str_tools, tor_tools
 
 try:
@@ -529,7 +529,10 @@ class Query(object):
       response = urllib.urlopen(
         urllib.Request(
           self.download_url,
-          headers = {'Accept-Encoding': ', '.join(self.compression)},
+          headers = {
+            'Accept-Encoding': ', '.join(self.compression),
+            'User-Agent': 'Stem/%s' % stem.__version__,
+          }
         ),
         timeout = timeout,
       )
@@ -618,7 +621,7 @@ class DescriptorDownloader(object):
     consensus = list(self.get_consensus(document_handler = stem.descriptor.DocumentHandler.DOCUMENT).run())[0]
 
     for desc in consensus.routers.values():
-      if Flag.V2DIR in desc.flags:
+      if stem.Flag.V2DIR in desc.flags:
         new_endpoints.add((desc.address, desc.dir_port))
 
     # we need our endpoints to be a list rather than set for random.choice()



More information about the tor-commits mailing list