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

[tor-commits] [nyx/master] Don't track start_time in the Entry



commit 45558ac8118d78a04c5e560843d5bd54c91e04d7
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Aug 30 10:50:22 2015 -0700

    Don't track start_time in the Entry
    
    The entry has access to the connection so no reason for it to track the start
    time separately. This also lets us move the to_unix_time() with the circuit
    which is nice since that's the only spot where it's now needed.
---
 nyx/connections/circ_entry.py |   19 +++++++++++--------
 nyx/connections/entries.py    |   33 ++++++++++++++-------------------
 2 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/nyx/connections/circ_entry.py b/nyx/connections/circ_entry.py
index b1d95c7..673777d 100644
--- a/nyx/connections/circ_entry.py
+++ b/nyx/connections/circ_entry.py
@@ -9,6 +9,7 @@ followed by an entry for each hop in the circuit. For instance:
 """
 
 import curses
+import datetime
 
 import nyx.util.tracker
 import nyx.util.ui_tools
@@ -18,6 +19,10 @@ from nyx.connections import entries, conn_entry
 from stem.util import str_tools
 
 
+def to_unix_time(dt):
+  return (dt - datetime.datetime(1970, 1, 1)).total_seconds()
+
+
 class CircHeaderLine(conn_entry.ConnectionLine):
   """
   Initial line of a client entry. This has the same basic format as connection
@@ -34,7 +39,7 @@ class CircHeaderLine(conn_entry.ConnectionLine):
       self.is_built = False
       self._remote_fingerprint = None
 
-    conn_entry.ConnectionLine.__init__(self, entry, nyx.util.tracker.Connection(entries.to_unix_time(circ.created), False, '127.0.0.1', 0, exit_address, exit_port, 'tcp'), False, False)
+    conn_entry.ConnectionLine.__init__(self, entry, nyx.util.tracker.Connection(to_unix_time(circ.created), False, '127.0.0.1', 0, exit_address, exit_port, 'tcp'), False, False)
     self.circuit = circ
 
   def get_fingerprint(self, default = None):
@@ -77,16 +82,18 @@ class CircLine(conn_entry.ConnectionLine):
   caching, etc).
   """
 
-  def __init__(self, entry, circ, fingerprint, timestamp):
+  def __init__(self, entry, circ, fingerprint):
     relay_ip, relay_port = nyx.util.tracker.get_consensus_tracker().get_relay_address(fingerprint, ('192.168.0.1', 0))
-    conn_entry.ConnectionLine.__init__(self, entry, nyx.util.tracker.Connection(timestamp, False, '127.0.0.1', 0, relay_ip, relay_port, 'tcp'), False)
+    conn_entry.ConnectionLine.__init__(self, entry, nyx.util.tracker.Connection(to_unix_time(circ.created), False, '127.0.0.1', 0, relay_ip, relay_port, 'tcp'), False)
     self._fingerprint = fingerprint
+    self._is_last = False
 
     circ_path = [path_entry[0] for path_entry in circ.path]
     circ_index = circ_path.index(fingerprint)
 
     if circ_index == len(circ_path) - 1:
       placement_type = 'Exit' if circ.status == 'BUILT' else 'Extending'
+      self._is_last = True
     elif circ_index == 0:
       placement_type = 'Guard'
     else:
@@ -94,15 +101,11 @@ class CircLine(conn_entry.ConnectionLine):
 
     self.placement_label = '%i / %s' % (circ_index + 1, placement_type)
 
-    # determines the sort of left hand bracketing we use
-
-    self.is_last = circ_index == len(circ_path) - 1
-
   def get_fingerprint(self, default = None):
     self._fingerprint
 
   def get_listing_prefix(self):
-    if self.is_last:
+    if self._is_last:
       return (ord(' '), curses.ACS_LLCORNER, curses.ACS_HLINE, ord(' '))
     else:
       return (ord(' '), curses.ACS_VLINE, ord(' '), ord(' '))
diff --git a/nyx/connections/entries.py b/nyx/connections/entries.py
index 00b0a0a..fb49f98 100644
--- a/nyx/connections/entries.py
+++ b/nyx/connections/entries.py
@@ -4,8 +4,6 @@ entry itself (ie, Tor connection, client circuit, etc) and the lines it
 consists of in the listing.
 """
 
-import datetime
-
 from nyx.util import tor_controller
 
 from stem.control import Listener
@@ -39,22 +37,17 @@ CONFIG = conf.config_dict('nyx', {
 })
 
 
-def to_unix_time(dt):
-  return (dt - datetime.datetime(1970, 1, 1)).total_seconds()
-
-
 class Entry(object):
-  def __init__(self, connection_type, start_time):
-    self.lines = []
+  def __init__(self, connection_type):
+    self._lines = []
     self._connection_type = connection_type
-    self._start_time = start_time
 
   @staticmethod
   def from_connection(conn):
     import nyx.connections.conn_entry
 
-    entry = Entry(get_type(conn), conn.start_time)
-    entry.lines = [nyx.connections.conn_entry.ConnectionLine(entry, conn)]
+    entry = Entry(get_type(conn))
+    entry._lines = [nyx.connections.conn_entry.ConnectionLine(entry, conn)]
     return entry
 
   @staticmethod
@@ -62,11 +55,11 @@ class Entry(object):
     import nyx.connections.circ_entry
     import nyx.connections.conn_entry
 
-    entry = Entry(nyx.connections.conn_entry.Category.CIRCUIT, to_unix_time(circ.created))
-    entry.lines = [nyx.connections.circ_entry.CircHeaderLine(entry, circ)]
+    entry = Entry(nyx.connections.conn_entry.Category.CIRCUIT)
+    entry._lines = [nyx.connections.circ_entry.CircHeaderLine(entry, circ)]
 
     for fingerprint, _ in circ.path:
-      entry.lines.append(nyx.connections.circ_entry.CircLine(entry, circ, fingerprint, to_unix_time(circ.created)))
+      entry._lines.append(nyx.connections.circ_entry.CircLine(entry, circ, fingerprint))
 
     return entry
 
@@ -95,16 +88,18 @@ class Entry(object):
     if not CONFIG['features.connection.showIps']:
       return True
 
+    connection = self._lines[0].connection
+
     if self.get_type() == nyx.connections.conn_entry.Category.INBOUND:
       controller = tor_controller()
 
       if controller.is_user_traffic_allowed().inbound:
-        return len(nyx.util.tracker.get_consensus_tracker().get_all_relay_fingerprints(self.connection.remote_address)) == 0
+        return len(nyx.util.tracker.get_consensus_tracker().get_all_relay_fingerprints(connection.remote_address)) == 0
     elif self.get_type() == nyx.connections.conn_entry.Category.EXIT:
       # DNS connections exiting us aren't private (since they're hitting our
       # resolvers). Everything else is.
 
-      return self.connection.remote_port != 53 or self.connection.protocol != 'udp'
+      return connection.remote_port != 53 or connection.protocol != 'udp'
 
     return False  # for everything else this isn't a concern
 
@@ -115,7 +110,7 @@ class Entry(object):
     :returns: **list** of **ConnectionLine** concerning this entry
     """
 
-    return self.lines
+    return self._lines
 
   @lru_cache()
   def get_sort_value(self, attr):
@@ -127,7 +122,7 @@ class Entry(object):
     :returns: comparable object by the given attribute
     """
 
-    connection_line = self.lines[0]
+    connection_line = self._lines[0]
 
     if attr == SortAttr.IP_ADDRESS:
       if self.is_private():
@@ -149,7 +144,7 @@ class Entry(object):
       import nyx.connections.conn_entry
       return nyx.connections.conn_entry.Category.index_of(self.get_type())
     elif attr == SortAttr.UPTIME:
-      return self._start_time
+      return connection_line.connection.start_time
     elif attr == SortAttr.COUNTRY:
       return '' if self.is_private() else connection_line.get_locale('')
     else:



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