commit bca09d443ff450113f7d46a0da204e500ef26f4e
Author: Damian Johnson <ata...@torproject.org>
Date:   Thu Jul 30 08:10:07 2015 -0700

    Differentiate between app resolution by local/remote ports
    
    Our helper for resolving applications by their port lets us differentiate
    between the local and remote port. When using it we said ports were both, 
but
    the connection lines know the difference. Plumbing it through to get more
    accurate results.
---
 nyx/connections/conn_panel.py           |   24 ++++++++----------------
 nyx/util/tracker.py                     |   27 +++++++++++++++++----------
 test/util/tracker/port_usage_tracker.py |    6 +++---
 3 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/nyx/connections/conn_panel.py b/nyx/connections/conn_panel.py
index 5163ec9..b8f97ad 100644
--- a/nyx/connections/conn_panel.py
+++ b/nyx/connections/conn_panel.py
@@ -494,21 +494,13 @@ class ConnectionPanel(panel.Panel, threading.Thread):
       self.set_sort_order()
       self._last_resource_fetch = current_resolution_count
 
-      self._resolve_apps()
+      if CONFIG['features.connection.resolveApps']:
+        local_ports, remote_ports = []
 
-  def _resolve_apps(self):
-    """
-    Triggers an asynchronous query for all unresolved SOCKS, HIDDEN, and
-    CONTROL entries.
-    """
-
-    if not CONFIG['features.connection.resolveApps']:
-      return
-
-    app_ports = []
-
-    for line in self._entry_lines:
-      if line.get_type() in (conn_entry.Category.SOCKS, 
conn_entry.Category.HIDDEN, conn_entry.Category.CONTROL):
-        app_ports.append(line.connection.local_port if line.get_type() == 
conn_entry.Category.HIDDEN else line.connection.remote_port)
+        for line in self._entry_lines:
+          if line.get_type() == conn_entry.Category.HIDDEN:
+            local_ports.append(line.connection.local_port)
+          elif line.get_type() in (conn_entry.Category.SOCKS, 
conn_entry.Category.CONTROL):
+            remote_ports.append(line.connection.remote_port)
 
-    nyx.util.tracker.get_port_usage_tracker().query(app_ports)
+        nyx.util.tracker.get_port_usage_tracker().query(local_ports, 
remote_ports)
diff --git a/nyx/util/tracker.py b/nyx/util/tracker.py
index 5d9c81e..57562f5 100644
--- a/nyx/util/tracker.py
+++ b/nyx/util/tracker.py
@@ -666,7 +666,8 @@ class PortUsageTracker(Daemon):
   def __init__(self, rate):
     super(PortUsageTracker, self).__init__(rate)
 
-    self._last_requested_ports = []
+    self._last_requested_local_ports = []
+    self._last_requested_remote_ports = []
     self._processes_for_ports = {}
     self._failure_count = 0  # number of times in a row we've failed to get 
results
 
@@ -683,25 +684,28 @@ class PortUsageTracker(Daemon):
 
     return self._processes_for_ports.get(port)
 
-  def query(self, ports):
+  def query(self, local_ports, remote_ports):
     """
     Registers a given set of ports for further lookups, and returns the last
     set of 'port => process' mappings we retrieved. Note that this means that
     we will not return the requested ports unless they're requested again after
     a successful lookup has been performed.
 
-    :param list ports: port numbers to look up
+    :param list local_ports: local port numbers to look up
+    :param list remote_ports: remote port numbers to look up
 
     :returns: **dict** mapping port numbers to the **Process** using it
     """
 
-    self._last_requested_ports = ports
+    self._last_requested_local_ports = local_ports
+    self._last_requested_remote_ports = remote_ports
     return self._processes_for_ports
 
   def _task(self, process_pid, process_name):
-    ports = self._last_requested_ports
+    local_ports = self._last_requested_local_ports
+    remote_ports = self._last_requested_remote_ports
 
-    if not ports:
+    if not local_ports and not remote_ports:
       return True
 
     result = {}
@@ -709,13 +713,16 @@ class PortUsageTracker(Daemon):
     # Use cached results from our last lookup if available.
 
     for port, process in self._processes_for_ports.items():
-      if port in ports:
+      if port in local_ports:
         result[port] = process
-        ports.remove(port)
+        local_ports.remove(port)
+      elif port in remote_ports:
+        result[port] = process
+        remote_ports.remove(port)
 
     try:
-      if ports:
-        result.update(_process_for_ports(ports, ports))
+      if local_ports or remote_ports:
+        result.update(_process_for_ports(local_ports, remote_ports))
 
       self._processes_for_ports = result
       self._failure_count = 0
diff --git a/test/util/tracker/port_usage_tracker.py 
b/test/util/tracker/port_usage_tracker.py
index 41d88d0..64c4c09 100644
--- a/test/util/tracker/port_usage_tracker.py
+++ b/test/util/tracker/port_usage_tracker.py
@@ -91,10 +91,10 @@ class TestPortUsageTracker(unittest.TestCase):
     with PortUsageTracker(0.02) as daemon:
       time.sleep(0.01)
 
-      self.assertEqual({}, daemon.query([37277, 51849]))
+      self.assertEqual({}, daemon.query([37277, 51849], []))
       time.sleep(0.04)
 
-      self.assertEqual({37277: 'python', 51849: 'tor'}, daemon.query([37277, 
51849]))
+      self.assertEqual({37277: 'python', 51849: 'tor'}, daemon.query([37277, 
51849], []))
 
   @patch('nyx.util.tracker.tor_controller')
   @patch('nyx.util.tracker._process_for_ports')
@@ -110,7 +110,7 @@ class TestPortUsageTracker(unittest.TestCase):
       time.sleep(0.05)
       self.assertEqual(0, daemon._failure_count)
 
-      daemon.query([37277, 51849])
+      daemon.query([37277, 51849], [])
       time.sleep(0.03)
       self.assertTrue(daemon.is_alive())
       time.sleep(0.1)



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

Reply via email to