[or-cvs] r23951: {arm} Applying patches from Fabian Keil. fix: dropping the locale (in arm/trunk: . src src/interface src/util)

Damian Johnson atagar1 at gmail.com
Sun Dec 19 03:59:15 UTC 2010


Author: atagar
Date: 2010-12-19 03:59:14 +0000 (Sun, 19 Dec 2010)
New Revision: 23951

Modified:
   arm/trunk/TODO
   arm/trunk/src/interface/connPanel.py
   arm/trunk/src/test.py
   arm/trunk/src/util/connections.py
Log:
Applying patches from Fabian Keil.
fix: dropping the locale for internal connections
fix: modifying lsof resolution to work with additional platforms



Modified: arm/trunk/TODO
===================================================================
--- arm/trunk/TODO	2010-12-18 20:22:12 UTC (rev 23950)
+++ arm/trunk/TODO	2010-12-19 03:59:14 UTC (rev 23951)
@@ -30,6 +30,9 @@
         - pick apart applications like iftop and pktstat to see how they get
           per-connection bandwidth usage. Forum thread discussing it:
           https://bbs.archlinux.org/viewtopic.php?pid=715906
+        - include an option to show both the internal and external ips for the
+          local connection, ie:
+          myInternal --> myExternal --> foreign
       [ ] control port interpreter (interactive prompt)
           Panel and startup option (-t maybe?) for providing raw control port
           access along with usability improvements (piggybacking on the arm

Modified: arm/trunk/src/interface/connPanel.py
===================================================================
--- arm/trunk/src/interface/connPanel.py	2010-12-18 20:22:12 UTC (rev 23950)
+++ arm/trunk/src/interface/connPanel.py	2010-12-19 03:59:14 UTC (rev 23951)
@@ -105,6 +105,20 @@
     if sortLabel == label: return type
   raise ValueError(sortLabel)
 
+def ipAddressIsPrivate(ipAddr):
+  """
+  Provides true if the IP address belongs on the local network or belongs to
+  loopback, false otherwise. These include:
+  Private ranges: 10.*, 172.16.* - 172.31.*, 192.168.*
+  Loopback: 127.*
+  
+  Arguments:
+    ipAddr - IP address to be checked
+  """
+  
+  # TODO: being lazy right now and just declaring all of 172.* as private
+  return ipAddr.startswith("10.") or ipAddr.startswith("192.168.") or ipAddr.startswith("172.") or ipAddr.startswith("127.")
+
 class ConnPanel(TorCtl.PostEventListener, panel.Panel):
   """
   Lists tor related connection data.
@@ -357,20 +371,20 @@
         
         # replace nat address with external version if available and the
         # external address isn't a private IP
-        # TODO: range should restrict to the following address ranges:
-        #   10.*, 172.16.* - 172.31.*, 192.168.*
-        # being lazy right now - fix the 172.* range when rewriting
-        isPrivateIp = fIp.startswith("10.") or fIp.startswith("192.168.") or fIp.startswith("172.")
+        isPrivateIp = ipAddressIsPrivate(fIp)
         if self.address and type != "control" and not isPrivateIp: lIp = self.address
         
-        try:
-          countryCodeQuery = "ip-to-country/%s" % fIp
-          countryCode = self.conn.get_info(countryCodeQuery)[countryCodeQuery]
-        except (socket.error, TorCtl.ErrorReply, TorCtl.TorCtlClosed):
+        if isPrivateIp:
           countryCode = "??"
-          if not self.providedGeoipWarning:
-            log.log(log.WARN, "Tor geoip database is unavailable.")
-            self.providedGeoipWarning = True
+        else:
+          try:
+            countryCodeQuery = "ip-to-country/%s" % fIp
+            countryCode = self.conn.get_info(countryCodeQuery)[countryCodeQuery]
+          except (socket.error, TorCtl.ErrorReply, TorCtl.TorCtlClosed):
+            countryCode = "??"
+            if not self.providedGeoipWarning:
+              log.log(log.WARN, "Tor geoip database is unavailable.")
+              self.providedGeoipWarning = True
         
         if (fIp, fPort) in connTimes: connTime = connTimes[(fIp, fPort)]
         else: connTime = time.time()
@@ -545,7 +559,9 @@
             if self.listingType == LIST_IP:
               # base data requires 73 characters
               src = "%s:%s" % (entry[CONN_L_IP], entry[CONN_L_PORT])
-              dst = "%s:%s %s" % (entry[CONN_F_IP], entry[CONN_F_PORT], "" if type == "control" else "(%s)" % entry[CONN_COUNTRY])
+              dst = "%s:%s" % (entry[CONN_F_IP], entry[CONN_F_PORT])
+              if not ipAddressIsPrivate(entry[CONN_F_IP]):
+                dst += " (%s)" % entry[CONN_COUNTRY]
               
               if isPrivate: dst = "<scrubbed>"
               
@@ -578,9 +594,12 @@
                 foreignHostnameSpace -= 22
                 
                 if isPrivate: ipEntry = "<scrubbed>"
-                else: ipEntry = "%s %s" % (entry[CONN_F_IP], "" if type == "control" else "(%s)" % entry[CONN_COUNTRY])
+                else:
+                  ipEntry = "%s:%s" % (entry[CONN_F_IP], entry[CONN_F_PORT])
+                  if ipAddressIsPrivate(entry[CONN_F_IP]):
+                    ipEntry += " (%s)" % entry[CONN_COUNTRY]
+                
                 etc += "%-20s  " % ipEntry
-              
               if width > 134 + xOffset:
                 # show fingerprint (column width: 42 characters)
                 foreignHostnameSpace -= 42
@@ -626,7 +645,11 @@
               if width > 125 + xOffset:
                 # shows ip/port/locale (column width: 28 characters)
                 if isPrivate: ipEntry = "<scrubbed>"
-                else: ipEntry = "%s:%s %s" % (entry[CONN_F_IP], entry[CONN_F_PORT], "" if type == "control" else "(%s)" % entry[CONN_COUNTRY])
+                else:
+                  ipEntry = "%s:%s" % (entry[CONN_F_IP], entry[CONN_F_PORT])
+                  if ipAddressIsPrivate(entry[CONN_F_IP]):
+                    ipEntry += " (%s)" % entry[CONN_COUNTRY]
+                
                 etc += "%-26s  " % ipEntry
             else:
               # base data uses whatever extra room's available (using minimun of 50 characters)
@@ -648,7 +671,11 @@
                 foreignNicknameSpace -= 28
                 
                 if isPrivate: ipEntry = "<scrubbed>"
-                else: ipEntry = "%s:%s %s" % (entry[CONN_F_IP], entry[CONN_F_PORT], "" if type == "control" else "(%s)" % entry[CONN_COUNTRY])
+                else:
+                  ipEntry = "%s:%s" % (entry[CONN_F_IP], entry[CONN_F_PORT])
+                  if ipAddressIsPrivate(entry[CONN_F_IP]):
+                    ipEntry += " (%s)" % entry[CONN_COUNTRY]
+                
                 etc += "%-26s  " % ipEntry
               
               dst = ("%%-%is" % foreignNicknameSpace) % dst

Modified: arm/trunk/src/test.py
===================================================================
--- arm/trunk/src/test.py	2010-12-18 20:22:12 UTC (rev 23950)
+++ arm/trunk/src/test.py	2010-12-19 03:59:14 UTC (rev 23951)
@@ -98,7 +98,7 @@
   elif userInput == "3":
     uiTools.demoGlyphs()
     
-    # Switching to a curses context and back repetedy seems to screw up the
+    # Switching to a curses context and back repeatedly seems to screw up the
     # terminal. Just to be safe this ends the process after the demo.
     break
   else:

Modified: arm/trunk/src/util/connections.py
===================================================================
--- arm/trunk/src/util/connections.py	2010-12-18 20:22:12 UTC (rev 23950)
+++ arm/trunk/src/util/connections.py	2010-12-19 03:59:14 UTC (rev 23951)
@@ -3,8 +3,8 @@
 process. This sort of data can be retrieved via a variety of common *nix
 utilities:
 - netstat   netstat -np | grep "ESTABLISHED <pid>/<process>"
-- sockstat  sockstat | egrep "<process>\s*<pid>.*ESTABLISHED"
-- lsof      lsof -nPi | egrep "^<process>\s*<pid>.*((UDP.*)|(\(ESTABLISHED\)))"
+- sockstat  sockstat | egrep "<process> *<pid>.*ESTABLISHED"
+- lsof      lsof -nPi | egrep "^<process> *<pid>.*((UDP.*)|(\(ESTABLISHED\)))"
 - ss        ss -nptu | grep "ESTAB.*\"<process>\",<pid>"
 
 all queries dump its stderr (directing it to /dev/null). Results include UDP
@@ -60,12 +60,12 @@
 # oddly, using the -p flag via:
 # lsof      lsof -nPi -p <pid> | grep "^<process>.*(ESTABLISHED)"
 # is much slower (11-28% in tests I ran)
-RUN_LSOF = "lsof -nPi | egrep \"^%s\\s*%s.*((UDP.*)|(\\(ESTABLISHED\\)))\""
+RUN_LSOF = "lsof -nPi | egrep \"^%s *%s.*((UDP.*)|(\\(ESTABLISHED\\)))\""
 
 # output:
 # atagar  tor  3475  tcp4  127.0.0.1:9051  127.0.0.1:38942  ESTABLISHED
 # *note: this isn't available by default under ubuntu
-RUN_SOCKSTAT = "sockstat | egrep \"%s\s*%s.*ESTABLISHED\""
+RUN_SOCKSTAT = "sockstat | egrep \"%s *%s.*ESTABLISHED\""
 
 RUN_BSD_SOCKSTAT = "sockstat -4c | grep '%s *%s'"
 RUN_BSD_PROCSTAT = "procstat -f %s | grep TCP | grep -v 0.0.0.0:0"
@@ -139,7 +139,12 @@
   # parses results for the resolution command
   conn = []
   for line in results:
-    comp = line.split()
+    if resolutionCmd == CMD_LSOF:
+      # Different versions of lsof have different numbers of columns, so
+      # stripping off the optional 'established' entry so we can just use
+      # the last one.
+      comp = line.replace("(ESTABLISHED)", "").strip().split()
+    else: comp = line.split()
     
     if resolutionCmd == CMD_NETSTAT:
       localIp, localPort = comp[3].split(":")
@@ -148,7 +153,7 @@
       localIp, localPort = comp[4].split(":")
       foreignIp, foreignPort = comp[5].split(":")
     elif resolutionCmd == CMD_LSOF:
-      local, foreign = comp[8].split("->")
+      local, foreign = comp[-1].split("->")
       localIp, localPort = local.split(":")
       foreignIp, foreignPort = foreign.split(":")
     elif resolutionCmd == CMD_SOCKSTAT:



More information about the tor-commits mailing list