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

[tor-commits] [nyx/master] Move get_relay_address() to ConsensusTracker



commit 6d0f8d928340f1ae053d62576b2e48c082fa9c01
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Jul 13 09:12:40 2015 -0700

    Move get_relay_address() to ConsensusTracker
    
    Moving this helper method not only tidies things up, but greatly reduces the
    number of GETINFO requests we make (we already process all router status
    entries, so why query them again?).
    
    Also, we weren't updating the address/port of relays if they changed in a new
    consensus. This fixes that.
---
 nyx/connections/circ_entry.py |   42 +++--------------------------------------
 nyx/util/tracker.py           |   27 +++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/nyx/connections/circ_entry.py b/nyx/connections/circ_entry.py
index 636464d..529242c 100644
--- a/nyx/connections/circ_entry.py
+++ b/nyx/connections/circ_entry.py
@@ -10,15 +10,13 @@ followed by an entry for each hop in the circuit. For instance:
 
 import curses
 
+import nyx.util.tracker
 import nyx.util.ui_tools
 
 from nyx.connections import entries, conn_entry
-from nyx.util import tor_controller
 
 from stem.util import str_tools
 
-ADDRESS_LOOKUP_CACHE = {}
-
 
 class CircEntry(conn_entry.ConnectionEntry):
   def __init__(self, circuit_id, status, purpose, path):
@@ -54,15 +52,14 @@ class CircEntry(conn_entry.ConnectionEntry):
 
     self.status = status
     self.lines = [self.lines[0]]
-    controller = tor_controller()
 
     if status == 'BUILT' and not self.lines[0].is_built:
-      exit_ip, exit_port = get_relay_address(controller, path[-1], ('192.168.0.1', '0'))
+      exit_ip, exit_port = nyx.util.tracker.get_consensus_tracker().get_relay_address(path[-1], ('192.168.0.1', 0))
       self.lines[0].set_exit(exit_ip, exit_port, path[-1])
 
     for i in range(len(path)):
       relay_fingerprint = path[i]
-      relay_ip, relay_port = get_relay_address(controller, relay_fingerprint, ('192.168.0.1', '0'))
+      relay_ip, relay_port = nyx.util.tracker.get_consensus_tracker().get_relay_address(relay_fingerprint, ('192.168.0.1', 0))
 
       if i == len(path) - 1:
         if status == 'BUILT':
@@ -213,36 +210,3 @@ class CircLine(conn_entry.ConnectionLine):
     return ((dst + etc, line_format),
             (' ' * (width - baseline_space - len(dst) - len(etc) + 5), line_format),
             ('%-14s' % self.placement_label, line_format))
-
-
-def get_relay_address(controller, relay_fingerprint, default = None):
-  """
-  Provides the (IP Address, ORPort) tuple for a given relay. If the lookup
-  fails then this returns the default.
-
-  Arguments:
-    relay_fingerprint - fingerprint of the relay
-  """
-
-  result = default
-
-  if controller.is_alive():
-    # query the address if it isn't yet cached
-    if relay_fingerprint not in ADDRESS_LOOKUP_CACHE:
-      if relay_fingerprint == controller.get_info('fingerprint', None):
-        # this is us, simply check the config
-        my_address = controller.get_info('address', None)
-        my_or_port = controller.get_conf('ORPort', None)
-
-        if my_address and my_or_port:
-          ADDRESS_LOOKUP_CACHE[relay_fingerprint] = (my_address, my_or_port)
-      else:
-        # check the consensus for the relay
-        relay = controller.get_network_status(relay_fingerprint, None)
-
-        if relay:
-          ADDRESS_LOOKUP_CACHE[relay_fingerprint] = (relay.address, relay.or_port)
-
-    result = ADDRESS_LOOKUP_CACHE.get(relay_fingerprint, default)
-
-  return result
diff --git a/nyx/util/tracker.py b/nyx/util/tracker.py
index 72bfcf2..638768b 100644
--- a/nyx/util/tracker.py
+++ b/nyx/util/tracker.py
@@ -32,7 +32,8 @@ Background tasks for gathering information about the tor process.
     |- update - updates the consensus information we're based on
     |- get_relay_nickname - provides the nickname for a given relay
     |- get_relay_fingerprint - provides the relay running at a location
-    +- get_all_relay_fingerprints - provides all relays running at a location
+    |- get_all_relay_fingerprints - provides all relays running at a location
+    +- get_relay_address - provides the address a relay is running at
 
 .. data:: Resources
 
@@ -696,6 +697,7 @@ class ConsensusTracker(object):
   def __init__(self):
     self._fingerprint_cache = {}  # {address => [(port, fingerprint), ..]} for relays
     self._nickname_cache = {}  # fingerprint => nickname lookup cache
+    self._address_cache = {}
 
     tor_controller().add_event_listener(self._new_consensus_event, stem.control.EventType.NEWCONSENSUS)
 
@@ -711,11 +713,14 @@ class ConsensusTracker(object):
     """
 
     new_fingerprint_cache = {}
+    new_address_cache = {}
 
     for desc in router_status_entries:
       new_fingerprint_cache.setdefault(desc.address, []).append((desc.or_port, desc.fingerprint))
+      new_address_cache[desc.fingerprint] = (desc.address, desc.or_port)
 
     self._fingerprint_cache = new_fingerprint_cache
+    self._address_cache = new_address_cache
 
   def get_relay_nickname(self, fingerprint):
     """
@@ -781,3 +786,23 @@ class ConsensusTracker(object):
     """
 
     return self._fingerprint_cache.get(address, [])
+
+  def get_relay_address(self, fingerprint, default):
+    """
+    Provides the (address, port) tuple where a relay is running.
+
+    :param str fingerprint: fingerprint to be checked
+
+    :returns: **tuple** with a **str** address and **int** port
+    """
+
+    controller = tor_controller()
+
+    if fingerprint == controller.get_info('fingerprint', None):
+      my_address = controller.get_info('address', None)
+      my_or_ports = controller.get_ports(stem.control.Listener.OR, [])
+
+      if my_address and len(my_or_ports) == 1:
+        return (my_address, my_or_ports[0])
+
+    return self._address_cache.get(fingerprint, default)



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