[tor-commits] [stem/master] Expanding /info output

atagar at torproject.org atagar at torproject.org
Mon May 26 02:12:44 UTC 2014


commit cec8c1f0d573160e0fa51d2fece254b45316e647
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun May 25 14:16:34 2014 -0700

    Expanding /info output
    
    Greatly enriching our /info output to include all relay information the user is
    likely to find helpful. This revises the summary a bit, but more importantly
    now includes the relay's hostname and all descriptor content (server,
    extrainfo, microdescriptor, and router status entry).
    
    Fetching both the hostname and descriptor content involves active lookups, but
    while this was a no-go for arm it's perfectly fine for an interactive
    interpreter like this.
---
 docs/change_log.rst           |   10 ++-----
 stem/interpreter/commands.py  |   59 ++++++++++++++++++++++++++++++++++-------
 stem/interpreter/settings.cfg |    4 +--
 3 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index ef9005d..0bff1b5 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -64,15 +64,9 @@ The following are only available within Stem's `git repository
   * Added :func:`stem.util.connection.port_usage`
   * Added :func:`stem.util.system.files_with_suffix`
 
- * **Interpretor**
+ * **Interpreter**
 
-  * Initial release of a Tor interactive interpreter. This included...
-
-   * irc-style functions such as '/help' and '/info'
-   * history scroll-back by pressing up/down
-   * tab completion
-   * terminal coloring for improved readability
-   * support for listing the asynchronous events we've received
+  * Initial release of our `interactive Tor interpreter <tutorials/down_the_rabbit_hole.html>`_!
 
  * **Website**
 
diff --git a/stem/interpreter/commands.py b/stem/interpreter/commands.py
index 6731949..f7de765 100644
--- a/stem/interpreter/commands.py
+++ b/stem/interpreter/commands.py
@@ -3,9 +3,11 @@ Handles making requests and formatting the responses.
 """
 
 import code
+import socket
 
 import stem
 import stem.control
+import stem.descriptor.remote
 import stem.interpreter.help
 import stem.util.connection
 import stem.util.tor_tools
@@ -160,9 +162,10 @@ class ControlInterpretor(code.InteractiveConsole):
     except ValueError as exc:
       return format(str(exc), *ERROR_OUTPUT)
 
-    micro_desc = self._controller.get_microdescriptor(fingerprint, None)
-    server_desc = self._controller.get_server_descriptor(fingerprint, None)
     ns_desc = self._controller.get_network_status(fingerprint, None)
+    server_desc = self._controller.get_server_descriptor(fingerprint, None)
+    extrainfo_desc = None
+    micro_desc = self._controller.get_microdescriptor(fingerprint, None)
 
     # We'll mostly rely on the router status entry. Either the server
     # descriptor or microdescriptor will be missing, so we'll treat them as
@@ -171,25 +174,47 @@ class ControlInterpretor(code.InteractiveConsole):
     if not ns_desc:
       return format("Unable to find consensus information for %s" % fingerprint, *ERROR_OUTPUT)
 
-    locale = self._controller.get_info('ip-to-country/%s' % ns_desc.address, None)
-    locale_label = ' (%s)' % locale if locale else ''
+    # More likely than not we'll have the microdescriptor but not server and
+    # extrainfo descriptors. If so then fetching them.
+
+    downloader = stem.descriptor.remote.DescriptorDownloader(timeout = 5)
+    server_desc_query = downloader.get_server_descriptors(fingerprint)
+    extrainfo_desc_query = downloader.get_extrainfo_descriptors(fingerprint)
+
+    for desc in server_desc_query:
+      server_desc = desc
+
+    for desc in extrainfo_desc_query:
+      extrainfo_desc = desc
+
+    address_extrainfo = []
+
+    try:
+      address_extrainfo.append(socket.gethostbyaddr(ns_desc.address)[0])
+    except:
+      pass
+
+    try:
+      address_extrainfo.append(self._controller.get_info('ip-to-country/%s' % ns_desc.address))
+    except:
+      pass
+
+    address_extrainfo_label = ' (%s)' % ', '.join(address_extrainfo) if address_extrainfo else ''
 
     if server_desc:
-      exit_policy_label = server_desc.exit_policy.summary()
+      exit_policy_label = str(server_desc.exit_policy)
     elif micro_desc:
-      exit_policy_label = micro_desc.exit_policy.summary()
+      exit_policy_label = str(micro_desc.exit_policy)
     else:
       exit_policy_label = 'Unknown'
 
     lines = [
       '%s (%s)' % (ns_desc.nickname, fingerprint),
-      format('address: ', *BOLD_OUTPUT) + '%s:%s%s' % (ns_desc.address, ns_desc.or_port, locale_label),
-      format('published: ', *BOLD_OUTPUT) + ns_desc.published.strftime('%H:%M:%S %d/%m/%Y'),
+      format('address: ', *BOLD_OUTPUT) + '%s:%s%s' % (ns_desc.address, ns_desc.or_port, address_extrainfo_label),
     ]
 
     if server_desc:
-      lines.append(format('os: ', *BOLD_OUTPUT) + server_desc.platform.decode('utf-8', 'replace'))
-      lines.append(format('version: ', *BOLD_OUTPUT) + str(server_desc.tor_version))
+      lines.append(format('tor version: ', *BOLD_OUTPUT) + str(server_desc.tor_version))
 
     lines.append(format('flags: ', *BOLD_OUTPUT) + ', '.join(ns_desc.flags))
     lines.append(format('exit policy: ', *BOLD_OUTPUT) + exit_policy_label)
@@ -207,6 +232,20 @@ class ControlInterpretor(code.InteractiveConsole):
 
       lines.append(format('contact: ', *BOLD_OUTPUT) + contact)
 
+    descriptor_section = [
+      ('Server Descriptor:', server_desc),
+      ('Extrainfo Descriptor:', extrainfo_desc),
+      ('Microdescriptor:', micro_desc),
+      ('Router Status Entry:', ns_desc),
+    ]
+
+    div = format('-' * 80, *STANDARD_OUTPUT)
+
+    for label, desc in descriptor_section:
+      if desc:
+        lines += ['', div, format(label, *BOLD_OUTPUT), div, '']
+        lines += [format(l, *STANDARD_OUTPUT) for l in str(desc).splitlines()]
+
     return '\n'.join(lines)
 
   def do_python(self, arg):
diff --git a/stem/interpreter/settings.cfg b/stem/interpreter/settings.cfg
index 5e85a5e..ce78ba7 100644
--- a/stem/interpreter/settings.cfg
+++ b/stem/interpreter/settings.cfg
@@ -135,8 +135,8 @@ help.description.events
 |received.
 
 help.description.info
-|Provides general information for a relay that's currently in the consensus.
-|If no relay is specified then this provides information on ourselves.
+|Provides information for a relay that's currently in the consensus. If no
+|relay is specified then this provides information on ourselves.
 
 help.description.python
 |Enables or disables support for running python commands. This determines how



More information about the tor-commits mailing list