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

[tor-commits] [nyx/master] Drop Endpoint class



commit 2f3fb3c4f1de830ec739a11adbae8a6cc5c309d8
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Jul 25 20:08:22 2015 -0700

    Drop Endpoint class
    
    Only bits left were get_finterprint() and get_nickname(). Both were only
    relevant to remote addresses so just as well as a line method. This also lets
    us get rid of a couple hacky variables (overwrite and 'is ORPort').
---
 nyx/connections/circ_entry.py |   20 +++++++---
 nyx/connections/conn_entry.py |   85 ++++++++++++++---------------------------
 nyx/connections/conn_panel.py |    2 +-
 nyx/connections/entries.py    |    4 +-
 nyx/util/tracker.py           |    2 +-
 5 files changed, 48 insertions(+), 65 deletions(-)

diff --git a/nyx/connections/circ_entry.py b/nyx/connections/circ_entry.py
index c49dbc4..94a0196 100644
--- a/nyx/connections/circ_entry.py
+++ b/nyx/connections/circ_entry.py
@@ -30,11 +30,18 @@ class CircHeaderLine(conn_entry.ConnectionLine):
     self.purpose = circ.purpose.capitalize()
     self.is_built = False
     self._timestamp = entries.to_unix_time(circ.created)
+    self._remote_fingerprint = None
 
   def set_exit(self, exit_address, exit_port, exit_fingerprint):
     conn_entry.ConnectionLine.__init__(self, nyx.util.tracker.Connection(self._timestamp, False, '127.0.0.1', 0, exit_address, exit_port, 'tcp'), False, False)
     self.is_built = True
-    self.foreign.fingerprint_overwrite = exit_fingerprint
+    self._remote_fingerprint = exit_fingerprint
+
+  def get_fingerprint(self, default = None):
+    if self._remote_fingerprint:
+      return self._remote_fingerprint
+    else:
+      return conn_entry.ConnectionLine.get_fingerprint(self, default)
 
   def get_type(self):
     return conn_entry.Category.CIRCUIT
@@ -78,7 +85,7 @@ class CircLine(conn_entry.ConnectionLine):
 
   def __init__(self, remote_address, remote_port, remote_fingerprint, placement_label, timestamp):
     conn_entry.ConnectionLine.__init__(self, nyx.util.tracker.Connection(timestamp, False, '127.0.0.1', 0, remote_address, remote_port, 'tcp'))
-    self.foreign.fingerprint_overwrite = remote_fingerprint
+    self._remote_fingerprint = remote_fingerprint
     self.placement_label = placement_label
     self.include_port = False
 
@@ -86,6 +93,9 @@ class CircLine(conn_entry.ConnectionLine):
 
     self.is_last = False
 
+  def get_fingerprint(self, default = None):
+    self._remote_fingerprint
+
   def get_type(self):
     return conn_entry.Category.CIRCUIT
 
@@ -132,21 +142,21 @@ class CircLine(conn_entry.ConnectionLine):
 
       # fills the nickname into the empty space here
 
-      dst = '%s%-25s   ' % (dst[:25], str_tools.crop(self.foreign.get_nickname('UNKNOWN'), 25, 0))
+      dst = '%s%-25s   ' % (dst[:25], str_tools.crop(self.get_nickname('UNKNOWN'), 25, 0))
 
       etc = self.get_etc_content(width - baseline_space - len(dst), listing_type)
     elif listing_type == entries.ListingType.FINGERPRINT:
       # dst width is derived as:
       # src (9) + dst (40) + divider (7) + right gap (2) - bracket (3) = 55 char
 
-      dst = '%-55s' % self.foreign.get_fingerprint('UNKNOWN')
+      dst = '%-55s' % self.get_fingerprint('UNKNOWN')
       etc = self.get_etc_content(width - baseline_space - len(dst), listing_type)
     else:
       # min space for the nickname is 56 characters
 
       etc = self.get_etc_content(width - baseline_space - 56, listing_type)
       dst_layout = '%%-%is' % (width - baseline_space - len(etc))
-      dst = dst_layout % self.foreign.get_nickname('UNKNOWN')
+      dst = dst_layout % self.get_nickname('UNKNOWN')
 
     return ((dst + etc, line_format),
             (' ' * (width - baseline_space - len(dst) - len(etc) + 5), line_format),
diff --git a/nyx/connections/conn_entry.py b/nyx/connections/conn_entry.py
index fd066e5..e9f9297 100644
--- a/nyx/connections/conn_entry.py
+++ b/nyx/connections/conn_entry.py
@@ -53,47 +53,6 @@ CONFIG = conf.config_dict('nyx', {
 })
 
 
-class Endpoint:
-  """
-  Connection endpoint, with basic consensus information if a relay.
-  """
-
-  def __init__(self, address, port):
-    self._address = address
-    self._port = port
-    self.is_or_port = False  # if set, consider the port to possibly be an ORPort
-
-    # if set then this overwrites fingerprint lookups
-
-    self.fingerprint_overwrite = None
-
-  def get_fingerprint(self, default = None):
-    """
-    Provides the fingerprint of this relay.
-    """
-
-    if self.fingerprint_overwrite:
-      return self.fingerprint_overwrite
-
-    my_fingerprint = nyx.util.tracker.get_consensus_tracker().get_relay_fingerprint(self._address, self._port if self.is_or_port else None)
-    return my_fingerprint if my_fingerprint else default
-
-  def get_nickname(self, default = None):
-    """
-    Provides the nickname of this relay.
-    """
-
-    fingerprint = self.get_fingerprint()
-
-    if fingerprint:
-      nickname = nyx.util.tracker.get_consensus_tracker().get_relay_nickname(fingerprint)
-
-      if nickname:
-        return nickname
-
-    return default
-
-
 class ConnectionLine(entries.ConnectionPanelLine):
   """
   Display component of the ConnectionEntry.
@@ -102,14 +61,11 @@ class ConnectionLine(entries.ConnectionPanelLine):
   def __init__(self, conn, include_port=True, include_expanded_addresses=True):
     entries.ConnectionPanelLine.__init__(self)
 
-    self.local = Endpoint(conn.local_address, int(conn.local_port))
-    self.foreign = Endpoint(conn.remote_address, int(conn.remote_port))
     self.connection = conn
 
     # overwrite the local fingerprint with ours
 
     controller = tor_controller()
-    self.local.fingerprint_overwrite = controller.get_info('fingerprint', None)
 
     # True if the connection has matched the properties of a client/directory
     # connection every time we've checked. The criteria we check is...
@@ -147,7 +103,6 @@ class ConnectionLine(entries.ConnectionPanelLine):
 
     if conn.local_port in (my_or_port, my_dir_port):
       self.base_type = Category.INBOUND
-      self.local.is_or_port = True
     elif conn.local_port == my_socks_port:
       self.base_type = Category.SOCKS
     elif conn.remote_port in my_hidden_service_ports:
@@ -156,7 +111,6 @@ class ConnectionLine(entries.ConnectionPanelLine):
       self.base_type = Category.CONTROL
     else:
       self.base_type = Category.OUTBOUND
-      self.foreign.is_or_port = True
 
     self.cached_type = None
 
@@ -184,6 +138,25 @@ class ConnectionLine(entries.ConnectionPanelLine):
 
     return tor_controller().get_info('ip-to-country/%s' % self.connection.remote_address, default)
 
+  def get_fingerprint(self, default = None):
+    """
+    Provides the fingerprint of this relay.
+    """
+
+    if self.base_type == Category.OUTBOUND:
+      my_fingerprint = nyx.util.tracker.get_consensus_tracker().get_relay_fingerprint(self.connection.remote_address, self.connection.remote_port)
+      return my_fingerprint if my_fingerprint else default
+    else:
+      return default  # inbound connections don't have an ORPort we can resolve
+
+  def get_nickname(self, default = None):
+    """
+    Provides the nickname of this relay.
+    """
+
+    nickname = nyx.util.tracker.get_consensus_tracker().get_relay_nickname(self.get_fingerprint())
+    return nickname if nickname else default
+
   def get_listing_entry(self, width, current_time, listing_type):
     """
     Provides the tuple list for this connection's listing. Lines are composed
@@ -331,7 +304,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
         # tor_tools util keeping this a quick lookup.
 
         controller = tor_controller()
-        destination_fingerprint = self.foreign.get_fingerprint()
+        destination_fingerprint = self.get_fingerprint()
 
         if not destination_fingerprint:
           # Not a known relay. This might be an exit connection.
@@ -416,14 +389,14 @@ class ConnectionLine(entries.ConnectionPanelLine):
       if width > used_space + 42 and CONFIG['features.connection.showColumn.fingerprint']:
         # show fingerprint (column width: 42 characters)
 
-        etc += '%-40s  ' % self.foreign.get_fingerprint('UNKNOWN')
+        etc += '%-40s  ' % self.get_fingerprint('UNKNOWN')
         used_space += 42
 
       if width > used_space + 10 and CONFIG['features.connection.showColumn.nickname']:
         # show nickname (column width: remainder)
 
         nickname_space = width - used_space
-        nickname_label = str_tools.crop(self.foreign.get_nickname('UNKNOWN'), nickname_space, 0)
+        nickname_label = str_tools.crop(self.get_nickname('UNKNOWN'), nickname_space, 0)
         etc += ('%%-%is  ' % nickname_space) % nickname_label
         used_space += nickname_space + 2
     elif listing_type == entries.ListingType.FINGERPRINT:
@@ -442,7 +415,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
           nickname_space -= 28
 
         if CONFIG['features.connection.showColumn.nickname']:
-          nickname_label = str_tools.crop(self.foreign.get_nickname('UNKNOWN'), nickname_space, 0)
+          nickname_label = str_tools.crop(self.get_nickname('UNKNOWN'), nickname_space, 0)
           etc += ('%%-%is  ' % nickname_space) % nickname_label
           used_space += nickname_space + 2
 
@@ -452,7 +425,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
     else:
       if width > used_space + 42 and CONFIG['features.connection.showColumn.fingerprint']:
         # show fingerprint (column width: 42 characters)
-        etc += '%-40s  ' % self.foreign.get_fingerprint('UNKNOWN')
+        etc += '%-40s  ' % self.get_fingerprint('UNKNOWN')
         used_space += 42
 
       if width > used_space + 28 and CONFIG['features.connection.showColumn.destination']:
@@ -551,7 +524,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
       if my_type == Category.CONTROL:
         dst = 'localhost'
       else:
-        dst = self.foreign.get_fingerprint('UNKNOWN')
+        dst = self.get_fingerprint('UNKNOWN')
 
       dst = '%-40s' % dst
 
@@ -561,12 +534,12 @@ class ConnectionLine(entries.ConnectionPanelLine):
       used_space += len(etc)
     else:
       # base data requires 50 min characters
-      src = self.local.get_nickname('UNKNOWN')
+      src = controller.get_conf('nickname', 'UNKNOWN')
 
       if my_type == Category.CONTROL:
-        dst = self.local.get_nickname('UNKNOWN')
+        dst = controller.get_conf('nickname', 'UNKNOWN')
       else:
-        dst = self.foreign.get_nickname('UNKNOWN')
+        dst = self.get_nickname('UNKNOWN')
 
       min_base_space = 50
 
@@ -610,7 +583,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
     # - if no consensus data is available then say so (probably a client or
     #   exit connection)
 
-    fingerprint = self.foreign.get_fingerprint()
+    fingerprint = self.get_fingerprint()
     controller = tor_controller()
 
     if fingerprint:
diff --git a/nyx/connections/conn_panel.py b/nyx/connections/conn_panel.py
index 51f22a8..b977e7d 100644
--- a/nyx/connections/conn_panel.py
+++ b/nyx/connections/conn_panel.py
@@ -272,7 +272,7 @@ class ConnectionPanel(panel.Panel, threading.Thread):
             break
 
           color = nyx.connections.conn_entry.CATEGORY_COLOR[selection.get_type()]
-          fingerprint = selection.foreign.get_fingerprint()
+          fingerprint = selection.get_fingerprint()
           is_close_key = lambda key: key.is_selection() or key.match('d') or key.match('left') or key.match('right')
           key = descriptor_popup.show_descriptor_popup(fingerprint, color, self.max_x, is_close_key)
 
diff --git a/nyx/connections/entries.py b/nyx/connections/entries.py
index 228b172..c4db1ea 100644
--- a/nyx/connections/entries.py
+++ b/nyx/connections/entries.py
@@ -132,9 +132,9 @@ class ConnectionPanelEntry:
     elif attr == SortAttr.PORT:
       return connection_line.sort_port
     elif attr == SortAttr.FINGERPRINT:
-      return connection_line.foreign.get_fingerprint('UNKNOWN')
+      return connection_line.get_fingerprint('UNKNOWN')
     elif attr == SortAttr.NICKNAME:
-      my_nickname = connection_line.foreign.get_nickname()
+      my_nickname = connection_line.get_nickname()
 
       if my_nickname:
         return my_nickname.lower()
diff --git a/nyx/util/tracker.py b/nyx/util/tracker.py
index 2471aff..b122d4b 100644
--- a/nyx/util/tracker.py
+++ b/nyx/util/tracker.py
@@ -748,7 +748,7 @@ class ConsensusTracker(object):
 
     controller = tor_controller()
 
-    if fingerprint not in self._nickname_cache:
+    if fingerprint and fingerprint not in self._nickname_cache:
       if fingerprint == controller.get_info('fingerprint', None):
         self._nickname_cache[fingerprint] = controller.get_conf('Nickname', 'Unnamed')
       else:



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